use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use anyhow::Result;
pub(crate) mod os_response;
fn check_scout_cancelled(cancel: Option<&AtomicBool>) -> Result<()> {
if cancel.is_some_and(|flag| flag.load(Ordering::Acquire)) {
anyhow::bail!("embedded reload cancelled during scout");
}
Ok(())
}
#[cfg(feature = "__test-internals")]
struct ScoutGateState {
pause_after_files: usize,
seen: std::sync::atomic::AtomicUsize,
blocked: std::sync::Mutex<bool>,
reached: std::sync::Condvar,
released: AtomicBool,
}
#[cfg(feature = "__test-internals")]
static SCOUT_GATES: std::sync::OnceLock<
std::sync::Mutex<std::collections::HashMap<PathBuf, std::sync::Arc<ScoutGateState>>>,
> = std::sync::OnceLock::new();
#[cfg(feature = "__test-internals")]
pub struct ScoutGateForTest {
root: PathBuf,
state: std::sync::Arc<ScoutGateState>,
}
#[cfg(feature = "__test-internals")]
pub fn hold_scout_after_files_for_test(root: &Path, pause_after_files: usize) -> ScoutGateForTest {
assert!(
pause_after_files > 0,
"scout gate needs a positive file count"
);
let root = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
let state = std::sync::Arc::new(ScoutGateState {
pause_after_files,
seen: std::sync::atomic::AtomicUsize::new(0),
blocked: std::sync::Mutex::new(false),
reached: std::sync::Condvar::new(),
released: AtomicBool::new(false),
});
let gates = SCOUT_GATES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()));
let previous = gates
.lock()
.expect("scout gate registry")
.insert(root.clone(), std::sync::Arc::clone(&state));
assert!(previous.is_none(), "a root can hold only one scout gate");
ScoutGateForTest { root, state }
}
#[cfg(feature = "__test-internals")]
impl ScoutGateForTest {
pub fn wait_until_blocked(&self, timeout: std::time::Duration) -> bool {
let blocked = self.state.blocked.lock().expect("scout gate state");
let (blocked, _) = self
.state
.reached
.wait_timeout_while(blocked, timeout, |blocked| !*blocked)
.expect("scout gate state");
*blocked
}
pub fn files_seen(&self) -> usize {
self.state.seen.load(Ordering::Acquire)
}
pub fn release(&self) {
self.state.released.store(true, Ordering::Release);
self.state.reached.notify_all();
}
}
#[cfg(feature = "__test-internals")]
impl Drop for ScoutGateForTest {
fn drop(&mut self) {
self.release();
let gates =
SCOUT_GATES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()));
let mut gates = gates.lock().expect("scout gate registry");
if gates
.get(&self.root)
.is_some_and(|current| std::sync::Arc::ptr_eq(current, &self.state))
{
gates.remove(&self.root);
}
}
}
#[cfg(feature = "__test-internals")]
fn pause_scout_after_files_for_test(root: &Path, cancel: Option<&AtomicBool>) {
let Some(state) = SCOUT_GATES
.get()
.and_then(|gates| gates.lock().ok()?.get(root).cloned())
else {
return;
};
if state.seen.fetch_add(1, Ordering::AcqRel) + 1 != state.pause_after_files {
return;
}
let mut blocked = state.blocked.lock().expect("scout gate state");
*blocked = true;
state.reached.notify_all();
while !state.released.load(Ordering::Acquire)
&& !cancel.is_some_and(|flag| flag.load(Ordering::Acquire))
{
let (next, _) = state
.reached
.wait_timeout(blocked, std::time::Duration::from_millis(5))
.expect("scout gate state");
blocked = next;
}
}
#[cfg(not(feature = "__test-internals"))]
fn pause_scout_after_files_for_test(_root: &Path, _cancel: Option<&AtomicBool>) {}
use crate::domain::{
AccessErrorKind, AccessStage, CatalogPath, CoverageStatus, FileClassification, FileStamp,
FreshnessReason, HardSkipReason, IndexTargets, LanguageId, ManifestResourceUsage,
MetadataOnlyReason, ProjectId, RootBinding, RootCandidateSource, RootClass, RootRefusalReason,
RootRequestMode, RootResolution, ScoutDecision, ScoutIssue, ScoutIssueKind, ScoutedEntry,
SourceAccessMode, StateFailure, StateLocationKind, StatePlacement, UnboundReason,
UserLocalPlacementReason,
};
#[derive(Debug, Clone)]
pub struct DiscoveredFile {
pub relative_path: String,
pub absolute_path: PathBuf,
pub language: LanguageId,
pub classification: FileClassification,
}
#[derive(Debug, Clone)]
pub struct DiscoveredEntry {
pub relative_path: String,
pub relative_os_path: PathBuf,
pub absolute_path: PathBuf,
pub file_size: u64,
pub language: Option<LanguageId>,
pub classification: FileClassification,
}
#[derive(Debug, Clone)]
pub struct ScoutPlan {
pub coverage: CoverageStatus,
pub entries: Vec<ScoutedEntry>,
pub issues: Vec<ScoutIssue>,
pub usage: ManifestResourceUsage,
}
pub(crate) fn refresh_scout_plan(plan: &mut ScoutPlan) -> Result<()> {
plan.entries.sort_by_cached_key(|entry| {
scout_order_key(
entry.path.normalized_utf8.as_deref(),
Some(entry.path.public_id.as_str()),
)
});
plan.issues.sort_by_cached_key(|issue| {
scout_order_key(issue.safe_path.as_deref(), issue.path_id.as_deref())
});
let limits = DiscoveryLimits::from_env();
let mut catalog_metadata_bytes = 2u64;
for issue in &plan.issues {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", issue),
limits.max_catalog_metadata_bytes,
)?;
}
let mut admitted_content_bytes = 0u64;
for entry in &plan.entries {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&(
"entry",
&entry.path,
entry.stamp.size,
&entry.language,
entry.classification,
&entry.decision,
),
limits.max_catalog_metadata_bytes,
)?;
if matches!(entry.decision, ScoutDecision::Ingest { .. }) {
admitted_content_bytes = admitted_content_bytes.saturating_add(entry.stamp.size);
}
}
plan.coverage = if plan.issues.is_empty()
&& plan
.entries
.iter()
.all(|entry| !matches!(entry.decision, ScoutDecision::Unavailable { .. }))
{
CoverageStatus::Complete
} else {
CoverageStatus::Degraded
};
plan.usage = ManifestResourceUsage {
catalog_entries: plan.entries.len() as u64,
catalog_metadata_bytes,
admitted_content_bytes,
};
Ok(())
}
#[derive(Debug)]
pub struct ScoutCapacityError {
reason: FreshnessReason,
safe_message: String,
}
impl ScoutCapacityError {
fn new(reason: FreshnessReason, safe_message: String) -> Self {
Self {
reason,
safe_message,
}
}
pub fn reason(&self) -> FreshnessReason {
self.reason
}
}
impl std::fmt::Display for ScoutCapacityError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.safe_message)
}
}
impl std::error::Error for ScoutCapacityError {}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SourceExclusions {
relative_subtrees: Vec<PathBuf>,
}
impl SourceExclusions {
pub(crate) fn for_state_placement(root: &Path, placement: &StatePlacement) -> Self {
let directory = match placement {
StatePlacement::ProjectLocal { directory }
| StatePlacement::UserLocal { directory, .. } => directory.as_path(),
StatePlacement::MemoryOnly { .. } => return Self::default(),
};
let canonical_root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
let canonical_directory =
dunce::canonicalize(directory).unwrap_or_else(|_| directory.to_path_buf());
let relative = canonical_directory
.strip_prefix(&canonical_root)
.or_else(|_| directory.strip_prefix(root));
let Ok(relative) = relative else {
return Self::default();
};
let mut relative_subtrees = vec![relative.to_path_buf()];
relative_subtrees.sort();
relative_subtrees.dedup();
Self { relative_subtrees }
}
pub(crate) fn excludes_relative(&self, relative_path: &Path) -> bool {
self.relative_subtrees
.iter()
.any(|subtree| relative_path.starts_with(subtree))
}
}
const MAX_INDEX_FILES_ENV: &str = "SYMFORGE_MAX_INDEX_FILES";
const MAX_INDEX_BYTES_ENV: &str = "SYMFORGE_MAX_INDEX_BYTES";
const MAX_CATALOG_METADATA_BYTES_ENV: &str = "SYMFORGE_MAX_CATALOG_METADATA_BYTES";
const DEFAULT_MAX_INDEX_FILES: u64 = 200_000;
const DEFAULT_MAX_INDEX_BYTES: u64 = 16 * 1024 * 1024 * 1024;
const DEFAULT_MAX_CATALOG_METADATA_BYTES: u64 = 512 * 1024 * 1024;
const MAX_CATALOG_SAFE_PATH_BYTES: usize = 64 * 1024;
#[derive(Debug, Clone, Copy)]
pub struct DiscoveryLimits {
pub max_files: u64,
pub max_bytes: u64,
pub max_catalog_metadata_bytes: u64,
}
impl DiscoveryLimits {
pub fn from_env() -> Self {
let max_files = parse_positive_env(MAX_INDEX_FILES_ENV).unwrap_or(DEFAULT_MAX_INDEX_FILES);
let max_bytes = parse_positive_env(MAX_INDEX_BYTES_ENV).unwrap_or(DEFAULT_MAX_INDEX_BYTES);
let max_catalog_metadata_bytes = parse_positive_env(MAX_CATALOG_METADATA_BYTES_ENV)
.unwrap_or(DEFAULT_MAX_CATALOG_METADATA_BYTES);
Self {
max_files,
max_bytes,
max_catalog_metadata_bytes,
}
}
}
impl Default for DiscoveryLimits {
fn default() -> Self {
Self {
max_files: DEFAULT_MAX_INDEX_FILES,
max_bytes: DEFAULT_MAX_INDEX_BYTES,
max_catalog_metadata_bytes: DEFAULT_MAX_CATALOG_METADATA_BYTES,
}
}
}
fn parse_positive_env(name: &str) -> Option<u64> {
std::env::var(name)
.ok()?
.trim()
.parse::<u64>()
.ok()
.filter(|&value| value > 0)
}
const CARGO_TARGET_DIR_ENV: &str = "CARGO_TARGET_DIR";
fn is_cargo_build_dir_name(name: &str) -> bool {
let Some(rest) = name.strip_prefix("target") else {
return false;
};
match rest.strip_prefix('-') {
None if rest.is_empty() => true,
None => false,
Some(suffix) => {
!suffix.is_empty()
&& suffix
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_')
}
}
}
fn cargo_target_dir_root_child(root: &Path) -> Option<String> {
let raw = std::env::var_os(CARGO_TARGET_DIR_ENV)?;
if raw.is_empty() {
return None;
}
let target = PathBuf::from(&raw);
let target_abs = if target.is_absolute() {
target
} else {
root.join(&target)
};
let canon_target = std::fs::canonicalize(&target_abs).unwrap_or(target_abs);
let parent = canon_target.parent()?;
if parent != root {
return None;
}
canon_target
.file_name()
.and_then(|n| n.to_str())
.map(|s| s.to_string())
}
pub(crate) fn is_under_repo_root_build_dir(
relative_path: &str,
target_dir_child: Option<&str>,
) -> bool {
let Some(first) = relative_path.split('/').next() else {
return false;
};
if first == relative_path {
return false;
}
if is_cargo_build_dir_name(first) {
return true;
}
matches!(target_dir_child, Some(child) if child == first)
}
pub(crate) fn path_is_root_build_output(root: &Path, relative_path: &str) -> bool {
is_under_repo_root_build_dir(relative_path, cargo_target_dir_root_child(root).as_deref())
}
pub(crate) fn path_is_hard_scope_excluded(relative_path: &Path) -> bool {
relative_path.components().any(|component| {
let std::path::Component::Normal(name) = component else {
return false;
};
name.to_str().is_some_and(|name| {
let name = crate::paths::fold_case(name);
matches!(name.as_str(), ".git" | ".symforge") || name.starts_with(".symforge-curation-")
})
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PathRefusal {
OutsideRoot,
Unresolvable(String),
#[cfg_attr(not(windows), allow(dead_code))]
WindowsAlias,
CredentialAlias(&'static str),
SpellingDiffers(Option<String>),
}
pub(crate) fn resolve_repo_path(
root: &Path,
relative_path: &str,
) -> std::result::Result<Option<PathBuf>, PathRefusal> {
use std::path::Component;
let requested = Path::new(relative_path);
if requested.is_absolute() {
return Err(absolute_beneath_root(root, requested)
.map_or(PathRefusal::OutsideRoot, refuse_as_spelling_of));
}
if requested.components().any(|component| {
matches!(
component,
Component::ParentDir | Component::RootDir | Component::Prefix(_)
)
}) {
return Err(PathRefusal::OutsideRoot);
}
#[cfg(windows)]
refuse_windows_alias_components(requested)?;
let clean = clean_spelling(requested);
let spelled = if cfg!(windows) {
relative_path.replace('\\', "/")
} else {
relative_path.to_string()
};
if !clean.is_empty() && spelled != clean {
return Err(refuse_as_spelling_of(clean));
}
let joined = dunce::simplified(root).join(requested);
match std::fs::symlink_metadata(&joined) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => {
return Err(PathRefusal::Unresolvable(format!(
"cannot resolve path '{relative_path}': {error}"
)));
}
Ok(_) => {}
}
let canon_root = root
.canonicalize()
.map_err(|error| PathRefusal::Unresolvable(format!("cannot resolve repo root: {error}")))?;
let canon_path = joined.canonicalize().map_err(|error| {
PathRefusal::Unresolvable(format!("cannot resolve path '{relative_path}': {error}"))
})?;
let Ok(canonical) = canon_path.strip_prefix(&canon_root) else {
return Err(PathRefusal::OutsideRoot);
};
let spelled = requested
.components()
.filter(|component| !matches!(component, Component::CurDir));
if canonical.components().eq(spelled) {
#[cfg(not(any(windows, target_os = "macos")))]
match listed_spelling(&canon_root, canonical) {
Some(listed) if listed == canonical => {}
Some(listed) => {
return Err(refuse_as_spelling_of(listed.to_string_lossy().into_owned()));
}
None => return Err(PathRefusal::SpellingDiffers(None)),
}
return Ok(Some(canon_path));
}
Err(refuse_as_spelling_of(
canonical.to_string_lossy().replace('\\', "/"),
))
}
#[cfg_attr(any(windows, target_os = "macos"), allow(dead_code))]
fn listed_spelling(root: &Path, relative: &Path) -> Option<PathBuf> {
let mut parent = root.to_path_buf();
let mut listed = PathBuf::new();
for component in relative.components() {
let std::path::Component::Normal(name) = component else {
continue;
};
let folded = crate::paths::fold_case(&name.to_string_lossy());
let mut fold_match = None;
let mut exact = false;
for entry in std::fs::read_dir(&parent).ok()?.flatten() {
let entry_name = entry.file_name();
if entry_name == name {
exact = true;
break;
}
if fold_match.is_none()
&& crate::paths::fold_case(&entry_name.to_string_lossy()) == folded
{
fold_match = Some(entry_name);
}
}
let name = if exact {
name.to_os_string()
} else {
fold_match?
};
parent.push(&name);
listed.push(&name);
}
Some(listed)
}
fn refuse_as_spelling_of(name: String) -> PathRefusal {
if let Some(rule_id) = crate::knowledge::sensitive_path_rule(&name) {
PathRefusal::CredentialAlias(rule_id)
} else if path_is_hard_scope_excluded(Path::new(&name)) {
PathRefusal::SpellingDiffers(None)
} else {
PathRefusal::SpellingDiffers(Some(name))
}
}
fn clean_spelling(path: &Path) -> String {
path.components()
.filter_map(|component| match component {
std::path::Component::Normal(name) => Some(name.to_string_lossy()),
_ => None,
})
.collect::<Vec<_>>()
.join("/")
}
fn absolute_beneath_root(root: &Path, requested: &Path) -> Option<String> {
let relative = dunce::simplified(requested)
.strip_prefix(dunce::simplified(root))
.ok()
.map(Path::to_path_buf)
.or_else(|| {
let canon_root = root.canonicalize().ok()?;
let canon_path = requested.canonicalize().ok()?;
canon_path
.strip_prefix(&canon_root)
.ok()
.map(Path::to_path_buf)
})?;
let escapes = relative
.components()
.any(|component| !matches!(component, std::path::Component::Normal(_)));
let clean = clean_spelling(&relative);
(!escapes && !clean.is_empty()).then_some(clean)
}
#[cfg(windows)]
fn refuse_windows_alias_components(requested: &Path) -> std::result::Result<(), PathRefusal> {
let mut is_alias = false;
let mut stripped = Vec::new();
for component in requested.components() {
let std::path::Component::Normal(name) = component else {
continue;
};
let name = name.to_string_lossy();
is_alias |= name.contains(':') || name.ends_with('.') || name.ends_with(' ');
let base = name.split(':').next().unwrap_or_default();
stripped.push(base.trim_end_matches(['.', ' ']).to_string());
}
if !is_alias {
return Ok(());
}
if let Some(rule_id) = crate::knowledge::sensitive_path_rule(&stripped.join("/")) {
return Err(PathRefusal::CredentialAlias(rule_id));
}
Err(PathRefusal::WindowsAlias)
}
fn repository_walk(root: &Path, exclusions: &SourceExclusions) -> ignore::Walk {
let filter_root = root.to_path_buf();
let exclusions = exclusions.clone();
let mut builder = ignore::WalkBuilder::new(root);
builder.hidden(false).filter_entry(move |entry| {
entry
.path()
.strip_prefix(&filter_root)
.is_ok_and(|relative| {
!path_is_hard_scope_excluded(relative) && !exclusions.excludes_relative(relative)
})
});
builder.build()
}
fn tree_too_large_message(files: u64, bytes: u64, limits: &DiscoveryLimits) -> String {
format!(
"tree too large to index ({files} files / {bytes} bytes exceeds limit of \
{max_files} files / {max_bytes} bytes); set {MAX_INDEX_FILES_ENV} or \
{MAX_INDEX_BYTES_ENV} to override",
max_files = limits.max_files,
max_bytes = limits.max_bytes,
)
}
fn catalog_entry_capacity_error(files: u64, bytes: u64, limits: &DiscoveryLimits) -> anyhow::Error {
ScoutCapacityError::new(
FreshnessReason::CatalogEntryCapacityExceeded,
tree_too_large_message(files, bytes, limits),
)
.into()
}
fn admitted_content_capacity_error(
files: u64,
bytes: u64,
limits: &DiscoveryLimits,
) -> anyhow::Error {
anyhow::anyhow!(tree_too_large_message(files, bytes, limits))
}
fn catalog_metadata_capacity_error(bytes: u64, limit: u64) -> anyhow::Error {
ScoutCapacityError::new(
FreshnessReason::CatalogMetadataCapacityExceeded,
format!(
"catalog metadata capacity exceeded ({bytes} bytes exceeds limit of {limit} bytes); \
set {MAX_CATALOG_METADATA_BYTES_ENV} to override"
),
)
.into()
}
pub fn discover_files(root: &Path) -> Result<Vec<DiscoveredFile>> {
discover_files_with_exclusions(root, &SourceExclusions::default())
}
pub fn discover_files_with_exclusions(
root: &Path,
exclusions: &SourceExclusions,
) -> Result<Vec<DiscoveredFile>> {
let root = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
let limits = DiscoveryLimits::from_env();
let target_dir_child = cargo_target_dir_root_child(&root);
let mut files: Vec<DiscoveredFile> = Vec::new();
for entry_result in repository_walk(&root, exclusions) {
let Ok(entry) = entry_result else { continue };
let Ok(path) = std::fs::canonicalize(entry.path()) else {
continue;
};
if !entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
continue;
}
let Ok(relative) = path.strip_prefix(&root) else {
continue;
};
let relative_path = relative.to_string_lossy().replace('\\', "/");
let Some(language) = LanguageId::from_path(&relative_path) else {
continue;
};
let targets = IndexTargets::for_path(&relative_path, Some(&language));
if is_under_repo_root_build_dir(&relative_path, target_dir_child.as_deref()) {
continue;
}
if files.len() as u64 >= limits.max_files {
return Err(catalog_entry_capacity_error(
files.len() as u64 + 1,
0,
&limits,
));
}
files.push(DiscoveredFile {
classification: FileClassification::for_indexed_path(&relative_path, targets),
relative_path,
absolute_path: path,
language,
});
}
files.sort_by_cached_key(|file| file.relative_path.to_lowercase());
Ok(files)
}
pub fn discover_all_files(root: &Path) -> Result<Vec<DiscoveredEntry>> {
discover_all_files_with_exclusions(root, &SourceExclusions::default())
}
pub fn discover_all_files_with_exclusions(
root: &Path,
exclusions: &SourceExclusions,
) -> Result<Vec<DiscoveredEntry>> {
discover_all_files_with_exclusions_and_issues(root, exclusions).map(|(entries, _)| entries)
}
fn discover_all_files_with_exclusions_and_issues(
root: &Path,
exclusions: &SourceExclusions,
) -> Result<(Vec<DiscoveredEntry>, Vec<ScoutIssue>)> {
discover_all_files_with_exclusions_and_issues_cancellable(root, exclusions, None)
}
fn discover_all_files_with_exclusions_and_issues_cancellable(
root: &Path,
exclusions: &SourceExclusions,
cancel: Option<&AtomicBool>,
) -> Result<(Vec<DiscoveredEntry>, Vec<ScoutIssue>)> {
let root = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
let limits = DiscoveryLimits::from_env();
let target_dir_child = cargo_target_dir_root_child(&root);
let mut total_bytes: u64 = 0;
let mut entries: Vec<DiscoveredEntry> = Vec::new();
let mut issues = Vec::new();
let mut tracked_for_build_dirs: Option<Option<std::collections::HashSet<String>>> = None;
for entry_result in repository_walk(&root, exclusions) {
check_scout_cancelled(cancel)?;
let entry = match entry_result {
Ok(entry) => entry,
Err(error) => {
issues.push(walk_issue_for_error(
&root,
ignore_error_path(&error),
error.io_error(),
));
continue;
}
};
let Ok(path) = std::fs::canonicalize(entry.path()) else {
continue;
};
if !entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
continue;
}
pause_scout_after_files_for_test(&root, cancel);
check_scout_cancelled(cancel)?;
let file_size = entry
.metadata()
.ok()
.map(|m| m.len())
.unwrap_or_else(|| std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0));
let Ok(relative) = path.strip_prefix(&root) else {
continue;
};
let relative_path = relative.to_string_lossy().replace('\\', "/");
if is_under_repo_root_build_dir(&relative_path, target_dir_child.as_deref()) {
let tracked = tracked_for_build_dirs
.get_or_insert_with(|| tracked_path_set_for_build_dir_rescue(&root));
check_scout_cancelled(cancel)?;
let rescued = tracked
.as_ref()
.is_some_and(|set| set.contains(relative_path.as_str()));
if !rescued {
continue;
}
}
let language = LanguageId::from_path(&relative_path);
let targets = IndexTargets::for_path(&relative_path, language.as_ref());
let classification = FileClassification::for_indexed_path(&relative_path, targets);
let admitted_bytes = match classify_admission(&path, file_size, None).tier {
AdmissionTier::Normal => file_size,
AdmissionTier::MetadataOnly | AdmissionTier::HardSkip => 0,
};
let projected_files = entries.len() as u64 + 1;
let projected_bytes = total_bytes.saturating_add(admitted_bytes);
if projected_files > limits.max_files {
return Err(catalog_entry_capacity_error(
projected_files,
projected_bytes,
&limits,
));
}
if projected_bytes > limits.max_bytes {
return Err(admitted_content_capacity_error(
projected_files,
projected_bytes,
&limits,
));
}
total_bytes = projected_bytes;
entries.push(DiscoveredEntry {
relative_path,
relative_os_path: relative.to_path_buf(),
absolute_path: path,
file_size,
language,
classification,
});
}
entries.sort_by_cached_key(|entry| entry.relative_path.to_lowercase());
Ok((entries, issues))
}
pub fn scout_repository(root: &Path) -> Result<ScoutPlan> {
scout_repository_with_metadata(root, |path| std::fs::metadata(path))
}
pub fn scout_repository_with_exclusions(
root: &Path,
exclusions: &SourceExclusions,
) -> Result<ScoutPlan> {
scout_repository_with_io_and_exclusions(
root,
exclusions,
|path| std::fs::metadata(path),
read_binary_probe,
)
}
pub(crate) fn scout_repository_with_exclusions_cancellable(
root: &Path,
exclusions: &SourceExclusions,
cancel: &AtomicBool,
) -> Result<ScoutPlan> {
scout_repository_with_io_and_exclusions_cancellable(
root,
exclusions,
|path| std::fs::metadata(path),
read_binary_probe,
Some(cancel),
)
}
fn scout_repository_with_metadata<F>(root: &Path, metadata_reader: F) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
{
scout_repository_with_io(root, metadata_reader, read_binary_probe)
}
fn scout_repository_with_io<F, P>(
root: &Path,
metadata_reader: F,
probe_reader: P,
) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
scout_repository_with_io_and_exclusions(
root,
&SourceExclusions::default(),
metadata_reader,
probe_reader,
)
}
pub(crate) fn scout_single_path_with_io<F, P>(
relative_path: &str,
absolute_path: &Path,
metadata_reader: F,
probe_reader: P,
) -> Result<ScoutedEntry>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
let language = LanguageId::from_path(relative_path);
let targets = IndexTargets::for_path(relative_path, language.as_ref());
let discovered = DiscoveredEntry {
relative_path: relative_path.to_string(),
relative_os_path: PathBuf::from(relative_path),
absolute_path: absolute_path.to_path_buf(),
file_size: 0,
language,
classification: FileClassification::for_indexed_path(relative_path, targets),
};
let plan = scout_entries_with_io(
vec![discovered],
metadata_reader,
probe_reader,
Vec::new(),
None,
)?;
plan.entries
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("single-path scout produced no entry for {relative_path}"))
}
pub(crate) fn scout_single_path(relative_path: &str, absolute_path: &Path) -> Result<ScoutedEntry> {
scout_single_path_with_io(
relative_path,
absolute_path,
crate::paths::path_must_be_regular_non_link,
read_binary_probe,
)
}
fn scout_repository_with_io_and_exclusions<F, P>(
root: &Path,
exclusions: &SourceExclusions,
metadata_reader: F,
probe_reader: P,
) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
scout_repository_with_io_and_exclusions_cancellable(
root,
exclusions,
metadata_reader,
probe_reader,
None,
)
}
fn scout_repository_with_io_and_exclusions_cancellable<F, P>(
root: &Path,
exclusions: &SourceExclusions,
metadata_reader: F,
probe_reader: P,
cancel: Option<&AtomicBool>,
) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
let (discovered, walk_issues) =
discover_all_files_with_exclusions_and_issues_cancellable(root, exclusions, cancel)?;
check_scout_cancelled(cancel)?;
scout_entries_with_io(
discovered,
metadata_reader,
probe_reader,
walk_issues,
cancel,
)
}
fn scout_entries_with_io<F, P>(
discovered: Vec<DiscoveredEntry>,
metadata_reader: F,
probe_reader: P,
mut issues: Vec<ScoutIssue>,
cancel: Option<&AtomicBool>,
) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
let mut admitted_content_bytes = 0u64;
let limits = DiscoveryLimits::from_env();
let mut catalog_metadata_bytes = 2u64;
let mut entries = Vec::with_capacity(discovered.len());
let mut degraded = !issues.is_empty();
for issue in &issues {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", issue),
limits.max_catalog_metadata_bytes,
)?;
}
let retry_budget = os_response::current_budget();
let observations = crate::live_index::store::ordered_parallel_map(&discovered, |entry| {
observe_scout_entry(
entry,
&metadata_reader,
&probe_reader,
&retry_budget,
cancel,
)
});
for (mut entry, observation) in discovered.into_iter().zip(observations) {
check_scout_cancelled(cancel)?;
let Some(observation) = observation else {
anyhow::bail!("embedded reload cancelled during scout");
};
let catalog_path = observation.catalog_path;
let public_id = catalog_path.public_id.clone();
let (metadata, decision) = match observation.outcome {
Ok(observed) => observed,
Err(kind) => {
degraded = true;
let issue = ScoutIssue {
path_id: Some(public_id),
safe_path: catalog_path.normalized_utf8.clone(),
kind: ScoutIssueKind::DirectoryEntryUnreadable { kind },
safe_message: "metadata unavailable".to_string(),
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", &issue),
limits.max_catalog_metadata_bytes,
)?;
issues.push(issue);
let scouted_entry = ScoutedEntry {
path: catalog_path,
absolute_path: Some(entry.absolute_path),
stamp: FileStamp {
size: entry.file_size,
created_hint: None,
modified_hint: None,
platform_id: None,
},
language: entry.language,
classification: entry.classification,
decision: ScoutDecision::Unavailable {
stage: AccessStage::Metadata,
kind,
},
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&(
"entry",
&scouted_entry.path,
scouted_entry.stamp.size,
&scouted_entry.language,
scouted_entry.classification,
&scouted_entry.decision,
),
limits.max_catalog_metadata_bytes,
)?;
entries.push(scouted_entry);
continue;
}
};
entry.file_size = metadata.len();
if let Some(cause) = &observation.probe_failure {
degraded = true;
os_response::record_failure(&entry.absolute_path, cause);
}
check_scout_cancelled(cancel)?;
if matches!(decision, ScoutDecision::Ingest { .. }) {
admitted_content_bytes = admitted_content_bytes.saturating_add(entry.file_size);
}
let scouted_entry = ScoutedEntry {
path: catalog_path,
absolute_path: Some(entry.absolute_path),
stamp: FileStamp {
size: entry.file_size,
created_hint: metadata.created().ok(),
modified_hint: metadata.modified().ok(),
platform_id: None,
},
language: entry.language,
classification: entry.classification,
decision,
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&(
"entry",
&scouted_entry.path,
scouted_entry.stamp.size,
&scouted_entry.language,
scouted_entry.classification,
&scouted_entry.decision,
),
limits.max_catalog_metadata_bytes,
)?;
entries.push(scouted_entry);
}
entries.sort_by_cached_key(|entry| {
scout_order_key(
entry.path.normalized_utf8.as_deref(),
Some(entry.path.public_id.as_str()),
)
});
let collision_issues = path_identity_collision_issues(&entries);
if !collision_issues.is_empty() {
degraded = true;
for issue in collision_issues {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", &issue),
limits.max_catalog_metadata_bytes,
)?;
issues.push(issue);
}
}
issues.sort_by_cached_key(|issue| {
scout_order_key(issue.safe_path.as_deref(), issue.path_id.as_deref())
});
Ok(ScoutPlan {
coverage: if degraded {
CoverageStatus::Degraded
} else {
CoverageStatus::Complete
},
usage: ManifestResourceUsage {
catalog_entries: entries.len() as u64,
catalog_metadata_bytes,
admitted_content_bytes,
},
entries,
issues,
})
}
fn account_catalog_metadata_record<T: serde::Serialize + ?Sized>(
current_bytes: u64,
record: &T,
limit: u64,
) -> Result<u64> {
let encoded_bytes = serde_json::to_vec(record)?.len() as u64;
let separator_bytes = u64::from(current_bytes > 2);
let projected_bytes = current_bytes
.saturating_add(separator_bytes)
.saturating_add(encoded_bytes);
if projected_bytes > limit {
Err(catalog_metadata_capacity_error(projected_bytes, limit))
} else {
Ok(projected_bytes)
}
}
fn catalog_path_for_relative(path: &Path) -> CatalogPath {
catalog_path_projection(path).0
}
fn catalog_path_projection(path: &Path) -> (CatalogPath, Option<MetadataOnlyReason>) {
let (normalized_utf8, reason) = match path.to_str() {
None => (None, Some(MetadataOnlyReason::UnsupportedPathEncoding)),
Some(value) if value.len() > MAX_CATALOG_SAFE_PATH_BYTES => {
(None, Some(MetadataOnlyReason::PathMetadataTooLarge))
}
Some(value) if !is_safe_catalog_path(path, value) => {
(None, Some(MetadataOnlyReason::UnsupportedPathEncoding))
}
Some(value) => (Some(normalize_catalog_path(value)), None),
};
let mut identity = Vec::new();
if let Some(safe_path) = normalized_utf8.as_deref() {
identity.extend_from_slice(b"symforge-catalog-path-v1:utf8\0");
identity.extend_from_slice(safe_path.as_bytes());
} else {
identity.extend_from_slice(b"symforge-catalog-path-v1:native\0");
identity.extend_from_slice(&native_path_identity_bytes(path));
}
(
CatalogPath {
public_id: crate::hash::digest_hex(&identity),
normalized_utf8,
},
reason,
)
}
fn is_safe_catalog_path(path: &Path, value: &str) -> bool {
!value.is_empty()
&& !value.chars().any(char::is_control)
&& path
.components()
.all(|component| matches!(component, std::path::Component::Normal(_)))
}
#[cfg(windows)]
fn normalize_catalog_path(value: &str) -> String {
value.replace('\\', "/")
}
#[cfg(not(windows))]
fn normalize_catalog_path(value: &str) -> String {
value.to_string()
}
#[cfg(unix)]
fn native_path_identity_bytes(path: &Path) -> Vec<u8> {
use std::os::unix::ffi::OsStrExt;
path.as_os_str().as_bytes().to_vec()
}
#[cfg(windows)]
fn native_path_identity_bytes(path: &Path) -> Vec<u8> {
use std::os::windows::ffi::OsStrExt;
path.as_os_str()
.encode_wide()
.flat_map(u16::to_le_bytes)
.collect()
}
#[cfg(not(any(unix, windows)))]
fn native_path_identity_bytes(path: &Path) -> Vec<u8> {
path.as_os_str().as_encoded_bytes().to_vec()
}
fn scout_order_key(
safe_path: Option<&str>,
public_id: Option<&str>,
) -> (u8, String, Vec<u8>, String) {
match safe_path {
Some(path) => (
0,
path.to_lowercase(),
path.as_bytes().to_vec(),
public_id.unwrap_or_default().to_string(),
),
None => (
1,
String::new(),
Vec::new(),
public_id.unwrap_or_default().to_string(),
),
}
}
fn path_identity_collision_issues(entries: &[ScoutedEntry]) -> Vec<ScoutIssue> {
let mut issues = Vec::new();
let mut start = 0usize;
while start < entries.len() {
let Some(path) = entries[start].path.normalized_utf8.as_deref() else {
start += 1;
continue;
};
let folded = path.to_lowercase();
let mut end = start + 1;
while end < entries.len()
&& entries[end]
.path
.normalized_utf8
.as_deref()
.is_some_and(|candidate| candidate.to_lowercase() == folded)
{
end += 1;
}
let has_distinct_spellings = entries[start..end]
.windows(2)
.any(|pair| pair[0].path.normalized_utf8 != pair[1].path.normalized_utf8);
if has_distinct_spellings {
for entry in &entries[start..end] {
issues.push(ScoutIssue {
path_id: Some(entry.path.public_id.clone()),
safe_path: entry.path.normalized_utf8.clone(),
kind: ScoutIssueKind::PathIdentityCollision,
safe_message: "case-fold path identity collision".to_string(),
});
}
}
start = end;
}
issues
}
fn walk_issue_for_error(
root: &Path,
failed_path: Option<&Path>,
error: Option<&std::io::Error>,
) -> ScoutIssue {
let catalog_path = failed_path
.and_then(|path| path.strip_prefix(root).ok())
.map(catalog_path_for_relative);
ScoutIssue {
path_id: catalog_path.as_ref().map(|path| path.public_id.clone()),
safe_path: catalog_path.and_then(|path| path.normalized_utf8),
kind: ScoutIssueKind::DirectoryEntryUnreadable {
kind: error.map_or(
AccessErrorKind::Other,
crate::discovery::os_response::stored_kind_of,
),
},
safe_message: "directory entry unavailable".to_string(),
}
}
fn ignore_error_path(error: &ignore::Error) -> Option<&Path> {
match error {
ignore::Error::Partial(errors) => errors.iter().find_map(ignore_error_path),
ignore::Error::WithLineNumber { err, .. } | ignore::Error::WithDepth { err, .. } => {
ignore_error_path(err)
}
ignore::Error::WithPath { path, .. } => Some(path.as_path()),
ignore::Error::Loop { child, .. } => Some(child.as_path()),
ignore::Error::Io(_)
| ignore::Error::Glob { .. }
| ignore::Error::UnrecognizedFileType(_)
| ignore::Error::InvalidDefinition => None,
}
}
fn read_binary_probe(path: &Path, max_bytes: usize) -> std::io::Result<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::with_capacity(max_bytes);
std::fs::File::open(path)?
.take(max_bytes as u64)
.read_to_end(&mut bytes)?;
Ok(bytes)
}
struct ScoutObservation {
catalog_path: CatalogPath,
outcome: std::result::Result<(std::fs::Metadata, ScoutDecision), AccessErrorKind>,
probe_failure: Option<os_response::OsCause>,
}
fn observe_scout_entry<F, P>(
entry: &DiscoveredEntry,
metadata_reader: &F,
probe_reader: &P,
retry_budget: &os_response::RetryBudget,
cancel: Option<&AtomicBool>,
) -> Option<ScoutObservation>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata> + Sync,
P: Fn(&Path, usize) -> std::io::Result<Vec<u8>> + Sync,
{
if check_scout_cancelled(cancel).is_err() {
return None;
}
let (catalog_path, path_reason) = catalog_path_projection(&entry.relative_os_path);
let metadata = match metadata_reader(&entry.absolute_path) {
Ok(metadata) => metadata,
Err(error) => {
return Some(ScoutObservation {
catalog_path,
outcome: Err(os_response::stored_kind_of(&error)),
probe_failure: None,
});
}
};
let file_size = metadata.len();
let mut probe_failure = None;
let decision = if let Some(reason) = path_reason {
ScoutDecision::MetadataOnly { reason }
} else if let Some(rule_id) =
crate::knowledge::sensitive_path_rule_at(&entry.relative_path, &entry.absolute_path)
{
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::SensitivePath {
rule_id: rule_id.to_string(),
},
}
} else {
match scout_decision_for_discovered(entry, file_size, None) {
ScoutDecision::Ingest { .. } => {
match os_response::retry_transient(retry_budget, Some(&entry.absolute_path), || {
probe_reader(
&entry.absolute_path,
crate::domain::index::BINARY_SNIFF_BYTES,
)
.map_err(os_response::AccessFailure::capture)
}) {
Ok(sample) => scout_decision_for_discovered(entry, file_size, Some(&sample)),
Err(cause) => {
let decision = ScoutDecision::Unavailable {
stage: AccessStage::Probe,
kind: cause.stored_kind(),
};
probe_failure = Some(cause);
decision
}
}
}
terminal => terminal,
}
};
Some(ScoutObservation {
catalog_path,
outcome: Ok((metadata, decision)),
probe_failure,
})
}
fn scout_decision_for_discovered(
entry: &DiscoveredEntry,
file_size: u64,
content_sample: Option<&[u8]>,
) -> ScoutDecision {
let admission = classify_admission(&entry.absolute_path, file_size, content_sample);
match admission.tier {
AdmissionTier::Normal => ScoutDecision::Ingest {
targets: IndexTargets::for_path(&entry.relative_path, entry.language.as_ref()),
},
AdmissionTier::HardSkip => ScoutDecision::HardSkip {
reason: match admission.reason {
Some(SkipReason::SizeCeiling) => HardSkipReason::PerFileCeiling,
_ => HardSkipReason::ArtifactType,
},
},
AdmissionTier::MetadataOnly => ScoutDecision::MetadataOnly {
reason: match admission.reason {
Some(SkipReason::DependencyLockfile) => MetadataOnlyReason::Lockfile,
Some(SkipReason::BinaryContent) => MetadataOnlyReason::Binary,
Some(SkipReason::SizeThreshold) => MetadataOnlyReason::OversizedData,
Some(SkipReason::DenylistedExtension)
| Some(SkipReason::Untracked)
| Some(SkipReason::GeneratedOutput) => MetadataOnlyReason::GeneratedOrVendor,
Some(SkipReason::SizeCeiling) => MetadataOnlyReason::OversizedData,
Some(SkipReason::UnsupportedTextEncoding)
| Some(SkipReason::PolicyWithheld)
| Some(SkipReason::LfsPointer)
| Some(SkipReason::UnsupportedPath)
| Some(SkipReason::Unreadable) => MetadataOnlyReason::UnsupportedTextEncoding,
Some(SkipReason::UnsupportedLanguage) | None => {
MetadataOnlyReason::UnsupportedTextEncoding
}
},
},
}
}
pub fn load_gitignore(root: &Path) -> Option<ignore::gitignore::Gitignore> {
use ignore::gitignore::GitignoreBuilder;
use std::collections::VecDeque;
let mut builder = GitignoreBuilder::new(root);
let max_depth: usize = 6;
let mut queue: VecDeque<(PathBuf, usize)> = VecDeque::new();
queue.push_back((root.to_path_buf(), 0));
while let Some((dir, depth)) = queue.pop_front() {
let gitignore_path = dir.join(".gitignore");
if gitignore_path.is_file() {
add_gitignore_file(&mut builder, root, &dir, &gitignore_path);
}
if depth < max_depth
&& let Ok(entries) = std::fs::read_dir(&dir)
{
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.starts_with('.') && name_str != ".github" {
continue;
}
queue.push_back((path, depth + 1));
}
}
}
}
match builder.build() {
Ok(gi) => {
if gi.is_empty() { None } else { Some(gi) }
}
Err(e) => {
tracing::debug!("failed to build gitignore matcher: {}", e);
None
}
}
}
fn add_gitignore_file(
builder: &mut ignore::gitignore::GitignoreBuilder,
root: &Path,
dir: &Path,
file: &Path,
) {
let Ok(relative) = dir.strip_prefix(root) else {
return;
};
if relative.as_os_str().is_empty() {
if let Some(err) = builder.add(file) {
tracing::debug!("failed to load {:?}: {}", file, err);
}
return;
}
let contents = match std::fs::read_to_string(file) {
Ok(contents) => contents,
Err(err) => {
tracing::debug!("failed to load {:?}: {}", file, err);
return;
}
};
add_gitignore_contents(builder, relative, file, &contents);
}
pub(crate) fn add_gitignore_contents(
builder: &mut ignore::gitignore::GitignoreBuilder,
relative_dir: &Path,
file: &Path,
contents: &str,
) {
let prefix = relative_dir
.components()
.filter_map(|component| match component {
std::path::Component::Normal(name) => Some(escape_glob(&name.to_string_lossy())),
_ => None,
})
.collect::<Vec<_>>()
.join("/");
let contents = contents.strip_prefix('\u{feff}').unwrap_or(contents);
for line in contents.lines() {
let pattern = if prefix.is_empty() {
Some(line.to_string())
} else {
anchor_nested_gitignore_line(&prefix, line)
};
if let Some(pattern) = pattern
&& let Err(err) = builder.add_line(Some(file.to_path_buf()), &pattern)
{
tracing::debug!("failed to load {:?}: {}", file, err);
}
}
}
fn anchor_nested_gitignore_line(prefix: &str, line: &str) -> Option<String> {
if line.starts_with('#') {
return None;
}
let line = if line.ends_with("\\ ") {
line
} else {
line.trim_end()
};
if line.is_empty() {
return None;
}
let (negation, pattern) = match line.strip_prefix('!') {
Some(rest) => ("!", rest),
None => ("", line),
};
let body = pattern.strip_suffix('/').unwrap_or(pattern);
let pattern = pattern.strip_prefix('/').unwrap_or(pattern);
Some(if body.contains('/') {
format!("{negation}/{prefix}/{pattern}")
} else {
format!("{negation}/{prefix}/**/{pattern}")
})
}
fn escape_glob(name: &str) -> String {
let mut escaped = String::with_capacity(name.len());
for character in name.chars() {
if matches!(character, '*' | '?' | '[' | ']' | '{' | '}' | '\\') {
escaped.push('\\');
}
escaped.push(character);
}
escaped
}
pub const WORKSPACE_ROOT_ENV: &str = "SYMFORGE_WORKSPACE_ROOT";
pub const CLAUDE_PROJECT_DIR_ENV: &str = "CLAUDE_PROJECT_DIR";
#[cfg(feature = "__test-internals")]
static CLAUDE_PROJECT_DIR_TEST_PERMIT: std::sync::atomic::AtomicU32 =
std::sync::atomic::AtomicU32::new(0);
#[cfg(feature = "__test-internals")]
pub struct ClaudeProjectDirTestPermit;
#[cfg(feature = "__test-internals")]
impl ClaudeProjectDirTestPermit {
pub fn enter() -> Self {
CLAUDE_PROJECT_DIR_TEST_PERMIT.fetch_add(1, Ordering::SeqCst);
Self
}
}
#[cfg(feature = "__test-internals")]
impl Drop for ClaudeProjectDirTestPermit {
fn drop(&mut self) {
CLAUDE_PROJECT_DIR_TEST_PERMIT.fetch_sub(1, Ordering::SeqCst);
}
}
enum WorkspaceRootEnvResolution {
AbsentOrRecoverable,
Resolved(PathBuf),
UnsupportedEncoding,
}
pub fn find_project_root() -> Option<PathBuf> {
find_project_root_with(true)
}
pub fn find_project_root_from_cwd() -> Option<PathBuf> {
find_project_root_with(false)
}
fn find_project_root_with(consult_claude_project_dir: bool) -> Option<PathBuf> {
match workspace_root_env_resolution() {
WorkspaceRootEnvResolution::Resolved(root) => return Some(root),
WorkspaceRootEnvResolution::UnsupportedEncoding => return None,
WorkspaceRootEnvResolution::AbsentOrRecoverable => {}
}
if consult_claude_project_dir
&& let Some(start) = claude_project_dir_start()
&& let Some(root) = root_from_walk(&start, RootCandidateSource::ClaudeProjectDir)
{
return Some(root);
}
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
root_from_walk(&cwd, RootCandidateSource::LaunchCwd)
}
#[cfg(feature = "__test-internals")]
#[allow(unsafe_code)] fn blank_inherited_claude_project_dir_once() {
use std::sync::Once;
static BLANK_INHERITED: Once = Once::new();
BLANK_INHERITED.call_once(|| {
unsafe {
std::env::remove_var(CLAUDE_PROJECT_DIR_ENV);
}
});
}
fn claude_project_dir_start() -> Option<PathBuf> {
#[cfg(feature = "__test-internals")]
{
if CLAUDE_PROJECT_DIR_TEST_PERMIT.load(Ordering::SeqCst) == 0 {
blank_inherited_claude_project_dir_once();
return None;
}
}
let raw = std::env::var_os(CLAUDE_PROJECT_DIR_ENV)?;
if raw.to_str().is_none_or(|value| value.trim().is_empty()) {
return None;
}
let path = PathBuf::from(raw);
path.is_dir().then_some(path)
}
fn root_from_walk(start: &Path, fallback_source: RootCandidateSource) -> Option<PathBuf> {
let mut current = start.to_path_buf();
loop {
if current.join(".git").exists() {
return validate_workspace_candidate(
¤t,
"git ancestor",
RootCandidateSource::GitAncestor,
);
}
match current.parent() {
Some(parent) => current = parent.to_path_buf(),
None => break,
}
}
validate_workspace_candidate(start, "walk start", fallback_source)
}
pub(crate) fn git_root_above(start: &Path) -> Option<PathBuf> {
let mut current = start.parent()?.to_path_buf();
loop {
if current.join(".git").exists() && !is_forbidden_root(¤t) {
return Some(current);
}
current = current.parent()?.to_path_buf();
}
}
pub fn workspace_root_env_override() -> Option<PathBuf> {
match workspace_root_env_resolution() {
WorkspaceRootEnvResolution::Resolved(root) => Some(root),
WorkspaceRootEnvResolution::AbsentOrRecoverable
| WorkspaceRootEnvResolution::UnsupportedEncoding => None,
}
}
pub fn workspace_root_env_is_authoritative() -> bool {
matches!(
workspace_root_env_resolution(),
WorkspaceRootEnvResolution::Resolved(_) | WorkspaceRootEnvResolution::UnsupportedEncoding
)
}
fn workspace_root_env_resolution() -> WorkspaceRootEnvResolution {
let Some(raw) = std::env::var_os(WORKSPACE_ROOT_ENV) else {
return WorkspaceRootEnvResolution::AbsentOrRecoverable;
};
if raw.is_empty() || raw.to_str().is_some_and(|value| value.trim().is_empty()) {
return WorkspaceRootEnvResolution::AbsentOrRecoverable;
}
match resolve_root_candidate(
Path::new(&raw),
RootCandidateSource::WorkspaceEnvironment,
RootRequestMode::Automatic,
) {
RootResolution::Bound(binding) => {
WorkspaceRootEnvResolution::Resolved(binding.canonical_root)
}
RootResolution::Unbound {
reason: UnboundReason::Refused(RootRefusalReason::UnsupportedPathEncoding),
..
} => WorkspaceRootEnvResolution::UnsupportedEncoding,
RootResolution::Unbound { .. } => WorkspaceRootEnvResolution::AbsentOrRecoverable,
}
}
#[cfg(windows)]
fn is_device_or_special_namespace(path: &Path) -> bool {
use std::os::windows::ffi::OsStrExt;
let mut native: Vec<u16> = path.as_os_str().encode_wide().collect();
for unit in &mut native {
if (*unit >= b'A' as u16) && (*unit <= b'Z' as u16) {
*unit += (b'a' - b'A') as u16;
}
}
let starts_with = |prefix: &str| {
let prefix: Vec<u16> = prefix.encode_utf16().collect();
native.starts_with(&prefix)
};
starts_with(r"\\.\")
|| starts_with(r"\\?\globalroot\")
|| starts_with(r"\\?\device\")
|| starts_with(r"\device\")
|| starts_with(r"\??\")
}
#[cfg(unix)]
fn is_device_or_special_namespace(path: &Path) -> bool {
let mut components = path.components();
if !matches!(components.next(), Some(std::path::Component::RootDir)) {
return false;
}
matches!(
components.next(),
Some(std::path::Component::Normal(component))
if component == "dev" || component == "proc" || component == "sys"
)
}
#[cfg(not(any(windows, unix)))]
fn is_device_or_special_namespace(_path: &Path) -> bool {
false
}
#[cfg(unix)]
fn is_special_filesystem_entry(path: &Path) -> bool {
use std::os::unix::fs::FileTypeExt;
std::fs::metadata(path).is_ok_and(|metadata| {
let kind = metadata.file_type();
kind.is_block_device() || kind.is_char_device() || kind.is_fifo() || kind.is_socket()
})
}
#[cfg(not(unix))]
fn is_special_filesystem_entry(_path: &Path) -> bool {
false
}
pub fn resolve_root_candidate(
candidate: &Path,
source: RootCandidateSource,
mode: RootRequestMode,
) -> RootResolution {
resolve_root_candidate_with(
candidate,
source,
mode,
|path| path.is_dir(),
|path| path.canonicalize(),
)
}
fn resolve_root_candidate_with<D, C>(
candidate: &Path,
source: RootCandidateSource,
mode: RootRequestMode,
is_directory: D,
canonicalize: C,
) -> RootResolution
where
D: Fn(&Path) -> bool,
C: Fn(&Path) -> std::io::Result<PathBuf>,
{
let safe_path_id = || {
format!(
"path-{}",
crate::hash::digest_hex(&native_path_identity_bytes(candidate))
)
};
let unbound = |reason| RootResolution::Unbound {
rejected_source: Some(source),
reason: UnboundReason::Refused(reason),
safe_path_id: Some(safe_path_id()),
};
if crate::paths::is_network_path(candidate) {
return unbound(RootRefusalReason::NetworkPath);
}
if is_device_or_special_namespace(candidate) || is_special_filesystem_entry(candidate) {
return unbound(RootRefusalReason::DeviceOrSpecialNamespace);
}
if crate::paths::is_credential_directory_path(candidate) {
return unbound(RootRefusalReason::CredentialDirectory);
}
let raw_is_protected =
crate::paths::is_sensitive_path(candidate) || is_forbidden_root(candidate);
if raw_is_protected
&& !matches!(
mode,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true
}
)
{
return unbound(RootRefusalReason::ProtectedRootRequiresExplicitOverride);
}
if !is_directory(candidate) {
return unbound(RootRefusalReason::MissingOrNotDirectory);
}
let canonical_root = match canonicalize(candidate) {
Ok(root) => root,
Err(_) => return unbound(RootRefusalReason::CanonicalizationFailed),
};
if crate::paths::is_network_path(&canonical_root) {
return unbound(RootRefusalReason::NetworkPath);
}
if canonical_root.to_str().is_none() {
return unbound(RootRefusalReason::UnsupportedPathEncoding);
}
if is_device_or_special_namespace(&canonical_root)
|| is_special_filesystem_entry(&canonical_root)
{
return unbound(RootRefusalReason::DeviceOrSpecialNamespace);
}
if crate::paths::is_credential_directory_path(&canonical_root) {
return unbound(RootRefusalReason::CredentialDirectory);
}
let canonical_is_protected =
crate::paths::is_sensitive_path(&canonical_root) || is_forbidden_root(&canonical_root);
let class = if raw_is_protected || canonical_is_protected {
RootClass::Protected
} else {
RootClass::Normal
};
let access_mode = match (class, mode) {
(
RootClass::Protected,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
) => SourceAccessMode::ExplicitProtected,
(RootClass::Protected, _) => {
return unbound(RootRefusalReason::ProtectedRootRequiresExplicitOverride);
}
(RootClass::Normal, _) => SourceAccessMode::NormalProject,
(RootClass::NeverIndexable, _) => unreachable!("terminal roots return before binding"),
};
RootResolution::Bound(RootBinding {
source,
root_id: project_id_for_canonical_root(&canonical_root),
canonical_root,
access_mode,
})
}
pub(crate) fn project_id_for_canonical_root(canonical_root: &Path) -> ProjectId {
#[cfg(windows)]
let platform_domain: &[u8] = b"windows";
#[cfg(unix)]
let platform_domain: &[u8] = b"unix";
#[cfg(not(any(windows, unix)))]
let platform_domain: &[u8] = b"other";
let root_identity = canonical_root_identity_bytes(canonical_root);
let mut digest_input = Vec::with_capacity(48 + root_identity.len());
digest_input.extend_from_slice(b"symforge.project-state-root\0v1\0");
digest_input.extend_from_slice(platform_domain);
digest_input.push(0);
digest_input.extend_from_slice(&(root_identity.len() as u64).to_le_bytes());
digest_input.extend_from_slice(&root_identity);
ProjectId(format!(
"project-v1-{}",
crate::hash::digest_hex(&digest_input)
))
}
#[cfg(windows)]
fn canonical_root_identity_bytes(canonical_root: &Path) -> Vec<u8> {
use std::os::windows::ffi::OsStrExt;
let canonical_root = dunce::simplified(canonical_root);
let mut identity = Vec::new();
for decoded in std::char::decode_utf16(canonical_root.as_os_str().encode_wide()) {
match decoded {
Ok(character) => {
let character = if character == '/' { '\\' } else { character };
for folded in character.to_lowercase() {
identity.push(0);
identity.extend_from_slice(&u32::from(folded).to_le_bytes());
}
}
Err(unpaired) => {
identity.push(1);
identity.extend_from_slice(&unpaired.unpaired_surrogate().to_le_bytes());
}
}
}
identity
}
#[cfg(not(windows))]
fn canonical_root_identity_bytes(canonical_root: &Path) -> Vec<u8> {
native_path_identity_bytes(canonical_root)
}
pub(crate) fn resolve_state_placement_with<F>(
binding: &RootBinding,
user_local_directory: Option<PathBuf>,
mut prepare: F,
) -> StatePlacement
where
F: FnMut(&Path) -> std::result::Result<(), AccessErrorKind>,
{
let mut failures = Vec::new();
let user_reason = if binding.access_mode == SourceAccessMode::ExplicitProtected {
UserLocalPlacementReason::ExplicitProtected
} else {
let project_directory = binding.canonical_root.join(".symforge");
match prepare_state_directory_with(&project_directory, &mut prepare) {
Ok(()) => {
return StatePlacement::ProjectLocal {
directory: crate::domain::ProjectStateDir::new(project_directory),
};
}
Err(safe_reason) => {
failures.push(StateFailure {
location: StateLocationKind::ProjectLocal,
safe_reason,
});
UserLocalPlacementReason::ProjectLocalUnavailable { safe_reason }
}
}
};
if let Some(directory) = user_local_directory {
match prepare_state_directory_with(&directory, &mut prepare) {
Ok(()) => {
return StatePlacement::UserLocal {
directory: crate::domain::ProjectStateDir::new(directory),
root_id: binding.root_id.clone(),
reason: user_reason,
};
}
Err(safe_reason) => failures.push(StateFailure {
location: StateLocationKind::UserLocal,
safe_reason,
}),
}
} else {
failures.push(StateFailure {
location: StateLocationKind::UserLocal,
safe_reason: AccessErrorKind::NotFound,
});
}
StatePlacement::MemoryOnly { failures }
}
fn prepare_state_directory_with<F>(
directory: &Path,
prepare: &mut F,
) -> std::result::Result<(), AccessErrorKind>
where
F: FnMut(&Path) -> std::result::Result<(), AccessErrorKind>,
{
match std::fs::symlink_metadata(directory) {
Ok(metadata) => {
if crate::paths::state_directory_metadata_is_unsafe(&metadata) {
return Err(AccessErrorKind::InvalidData);
}
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(crate::discovery::os_response::stored_kind_of(&error)),
}
prepare(directory)?;
let metadata = std::fs::symlink_metadata(directory)
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))?;
if crate::paths::state_directory_metadata_is_unsafe(&metadata) {
Err(AccessErrorKind::InvalidData)
} else {
Ok(())
}
}
pub fn resolve_state_placement(binding: &RootBinding) -> StatePlacement {
let user_local_directory = crate::paths::resolve_user_local_project_state_base()
.ok()
.map(|base| base.join("projects").join(&binding.root_id.0));
resolve_state_placement_with(binding, user_local_directory, |directory| {
std::fs::create_dir_all(directory)
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(directory, std::fs::Permissions::from_mode(0o700))
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))?;
}
Ok(())
})
}
fn validate_workspace_candidate(
candidate: &Path,
source: &str,
candidate_source: RootCandidateSource,
) -> Option<PathBuf> {
match resolve_root_candidate(candidate, candidate_source, RootRequestMode::Automatic) {
RootResolution::Bound(binding) => Some(binding.canonical_root),
RootResolution::Unbound {
reason,
safe_path_id,
..
} => {
let reason = match reason {
UnboundReason::NoCandidateDeclared => "no_candidate_declared",
UnboundReason::Refused(reason) => reason.code(),
};
tracing::warn!(
safe_path_id = safe_path_id.as_deref().unwrap_or("path-unavailable"),
reason,
"ignoring {source}: workspace root refused"
);
None
}
}
}
pub(crate) fn percent_decode_path(s: &str) -> Option<String> {
let bytes = s.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' {
let hi = bytes.get(i + 1).copied()?;
let lo = bytes.get(i + 2).copied()?;
let decode = |b: u8| -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
};
out.push(decode(hi)? << 4 | decode(lo)?);
i += 3;
} else {
out.push(bytes[i]);
i += 1;
}
}
String::from_utf8(out).ok()
}
pub fn parse_root_uri(uri: &str) -> Option<PathBuf> {
let trimmed = uri.trim();
if trimmed.is_empty() {
return None;
}
if let Some(scheme_end) = trimmed.find("://") {
let scheme = &trimmed[..scheme_end];
if !scheme.eq_ignore_ascii_case("file") {
if scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
{
return None;
}
}
}
let rest = if let Some(after) = trimmed.strip_prefix("file://") {
let idx = after.find('/')?;
let authority = &after[..idx];
if authority.is_empty() || authority.eq_ignore_ascii_case("localhost") {
&after[idx..]
} else {
return None;
}
} else {
trimmed
};
let decoded = percent_decode_path(rest)?;
let cleaned = {
let bytes = decoded.as_bytes();
if bytes.len() >= 3
&& bytes[0] == b'/'
&& bytes[1].is_ascii_alphabetic()
&& bytes[2] == b':'
{
decoded[1..].to_string()
} else {
decoded
}
};
Some(PathBuf::from(cleaned))
}
pub fn resolve_workspace_root(
env_root: Option<PathBuf>,
root_uris: &[String],
cwd_root: Option<PathBuf>,
) -> Option<PathBuf> {
if let Some(candidate) = env_root
&& let Some(root) = validate_workspace_candidate(
&candidate,
"workspace environment root",
RootCandidateSource::WorkspaceEnvironment,
)
{
return Some(root);
}
for uri in root_uris {
let Some(candidate) = parse_root_uri(uri) else {
continue;
};
if let Some(root) = validate_workspace_candidate(
&candidate,
"MCP client root",
RootCandidateSource::McpClientRoot,
) {
return Some(root);
}
}
cwd_root.and_then(|candidate| {
validate_workspace_candidate(
&candidate,
"launch CWD root",
RootCandidateSource::LaunchCwd,
)
})
}
fn is_forbidden_root(path: &Path) -> bool {
let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
if crate::paths::is_sensitive_path(&path) {
return true;
}
if path.parent().is_none() {
return true;
}
#[cfg(target_os = "windows")]
{
let path_str = path.to_string_lossy();
if path_str.len() <= 7 && path_str.ends_with('\\') {
return true;
}
}
if let Some(home) = home_dir() {
let home = home.canonicalize().unwrap_or(home);
if path == home {
return true;
}
}
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
let lower = name.to_lowercase();
const SYSTEM_NAMES: &[&str] = &[
"windows",
"system32",
"program files",
"program files (x86)",
"programdata",
"node_modules",
".npm",
".cargo",
];
if SYSTEM_NAMES.contains(&lower.as_str()) {
return true;
}
}
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
let lower = name.to_lowercase();
const CONTAINER_NAMES: &[&str] = &["users", "home", "tmp", "temp", "var", "appdata"];
if CONTAINER_NAMES.contains(&lower.as_str())
&& path
.parent()
.map(|p| {
p.parent().is_none() || {
#[cfg(target_os = "windows")]
{
let pstr = p.to_string_lossy();
pstr.len() <= 7 && pstr.ends_with('\\')
}
#[cfg(not(target_os = "windows"))]
{
false
}
}
})
.unwrap_or(false)
{
return true;
}
}
#[cfg(not(target_os = "windows"))]
{
if is_running_under_wsl() && is_wsl_windows_container_path(&path) {
return true;
}
}
if let Some(home) = home_dir() {
let home = home.canonicalize().unwrap_or(home);
if let Some(parent) = home.parent() {
let parent = parent
.canonicalize()
.unwrap_or_else(|_| parent.to_path_buf());
if path == parent {
return true;
}
}
}
false
}
fn home_dir() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
{
std::env::var("USERPROFILE").ok().map(PathBuf::from)
}
#[cfg(not(target_os = "windows"))]
{
std::env::var("HOME").ok().map(PathBuf::from)
}
}
#[cfg(not(target_os = "windows"))]
fn is_running_under_wsl() -> bool {
use std::sync::OnceLock;
static IS_WSL: OnceLock<bool> = OnceLock::new();
*IS_WSL.get_or_init(|| {
std::fs::read_to_string("/proc/version")
.map(|v| {
let v = v.to_ascii_lowercase();
v.contains("microsoft") || v.contains("wsl")
})
.unwrap_or(false)
})
}
#[cfg(not(target_os = "windows"))]
fn is_wsl_windows_container_path(path: &Path) -> bool {
let mut comps: Vec<&str> = Vec::new();
for component in path.components() {
match component {
std::path::Component::Normal(os) => {
if let Some(s) = os.to_str() {
comps.push(s);
}
}
std::path::Component::ParentDir if comps.pop().is_none() => return false,
std::path::Component::ParentDir => {}
_ => {}
}
}
if comps.first() != Some(&"mnt") {
return false;
}
let is_drive_letter =
|s: &str| s.len() == 1 && s.chars().next().is_some_and(|c| c.is_ascii_alphabetic());
let is_users = |s: &str| s.eq_ignore_ascii_case("Users");
match comps.as_slice() {
[_mnt, drive] => is_drive_letter(drive),
[_mnt, drive, users] => is_drive_letter(drive) && is_users(users),
[_mnt, drive, users, _name] => is_drive_letter(drive) && is_users(users),
_ => false,
}
}
pub fn is_binary_content(content: &[u8]) -> bool {
is_binary_content_sample(content, content.len() as u64)
}
fn is_binary_content_sample(content: &[u8], file_size: u64) -> bool {
if content.is_empty() {
return false;
}
let check_len = content.len().min(crate::domain::index::BINARY_SNIFF_BYTES);
let window = &content[..check_len];
if window.contains(&0) {
return true;
}
if let Err(error) = std::str::from_utf8(window) {
let boundary_cut = error.error_len().is_none() && (check_len as u64) < file_size;
if !boundary_cut {
return true;
}
}
let suspicious_controls = window
.iter()
.filter(|&&b| matches!(b, 0x01..=0x08 | 0x0E..=0x1F | 0x7F))
.count();
let ratio = suspicious_controls as f64 / window.len() as f64;
ratio > 0.30
}
use crate::domain::index::{
AdmissionDecision, AdmissionTier, HARD_SKIP_BYTES, METADATA_ONLY_BYTES, SkipReason,
};
pub fn path_admission_reason(path: &Path, size: u64) -> Option<MetadataOnlyReason> {
use crate::domain::index::{
METADATA_ONLY_CODE_BYTES, is_denylisted_extension, is_dependency_lockfile,
};
if is_dependency_lockfile(path) {
return Some(MetadataOnlyReason::Lockfile);
}
if let Some(ext) = path.extension().and_then(|e| e.to_str())
&& is_denylisted_extension(ext)
{
return Some(MetadataOnlyReason::GeneratedOrVendor);
}
let size_threshold = path
.extension()
.and_then(|e| e.to_str())
.and_then(crate::domain::LanguageId::from_extension)
.filter(crate::domain::LanguageId::is_code_language)
.map_or(METADATA_ONLY_BYTES, |_| METADATA_ONLY_CODE_BYTES);
if size > size_threshold {
return Some(MetadataOnlyReason::OversizedData);
}
None
}
pub fn classify_admission(
path: &std::path::Path,
file_size: u64,
content_sample: Option<&[u8]>,
) -> AdmissionDecision {
if file_size > HARD_SKIP_BYTES {
return AdmissionDecision::skip(AdmissionTier::HardSkip, SkipReason::SizeCeiling);
}
if let Some(reason) = path_admission_reason(path, file_size) {
let skip = match reason {
MetadataOnlyReason::Lockfile => SkipReason::DependencyLockfile,
MetadataOnlyReason::OversizedData => SkipReason::SizeThreshold,
_ => SkipReason::DenylistedExtension,
};
return AdmissionDecision::skip(AdmissionTier::MetadataOnly, skip);
}
if let Some(content) = content_sample
&& is_binary_content_sample(content, file_size)
{
return AdmissionDecision::skip(AdmissionTier::MetadataOnly, SkipReason::BinaryContent);
}
AdmissionDecision::normal()
}
pub fn unsupported_language_decision(decision: AdmissionDecision) -> AdmissionDecision {
if decision.tier == AdmissionTier::Normal {
AdmissionDecision::skip(AdmissionTier::MetadataOnly, SkipReason::UnsupportedLanguage)
} else {
decision
}
}
pub const EXCLUDE_UNTRACKED_ENV: &str = "SYMFORGE_EXCLUDE_UNTRACKED";
pub fn exclude_untracked_enabled() -> bool {
std::env::var(EXCLUDE_UNTRACKED_ENV)
.map(|v| {
let v = v.trim().to_ascii_lowercase();
matches!(v.as_str(), "1" | "true" | "yes" | "on")
})
.unwrap_or(false)
}
pub fn tracked_path_set_for_exclusion(root: &Path) -> Option<std::collections::HashSet<String>> {
if !exclude_untracked_enabled() {
return None;
}
let git_repo = crate::git::GitRepo::open(root).ok()?;
let tracked = git_repo.tracked_paths().ok()?;
if tracked.is_empty() {
return None;
}
Some(tracked.into_iter().collect())
}
fn tracked_path_set_for_build_dir_rescue(root: &Path) -> Option<std::collections::HashSet<String>> {
let git_repo = crate::git::GitRepo::open(root).ok()?;
let tracked = git_repo.tracked_paths().ok()?;
if tracked.is_empty() {
return None;
}
Some(tracked.into_iter().collect())
}
pub const INDEX_GENERATED_OUTPUT_ENV: &str = "SYMFORGE_INDEX_GENERATED_OUTPUT";
fn index_generated_output_enabled() -> bool {
std::env::var(INDEX_GENERATED_OUTPUT_ENV)
.map(|v| {
let v = v.trim().to_ascii_lowercase();
matches!(v.as_str(), "1" | "true" | "yes" | "on")
})
.unwrap_or(false)
}
pub fn is_generated_output_dir_name(name: &str) -> bool {
let n = name.to_ascii_lowercase();
matches!(
n.as_str(),
"dist" | "build" | "out" | "output" | "cache" | ".cache" | "generated"
) || n.ends_with("-out")
|| n.ends_with("-output")
}
pub fn untracked_generated_output_demotions(
root: &Path,
entries: &[DiscoveredEntry],
) -> std::collections::HashSet<String> {
if index_generated_output_enabled() {
return std::collections::HashSet::new();
}
let Some(tracked) = tracked_path_set_for_build_dir_rescue(root) else {
return std::collections::HashSet::new();
};
untracked_generated_output_demotions_inner(entries, &tracked)
}
fn shallowest_generated_output_prefix(relative_path: &str) -> Option<&str> {
let (dirs, _file) = relative_path.rsplit_once('/')?;
let mut end = 0usize;
for comp in dirs.split('/') {
end += comp.len();
if is_generated_output_dir_name(comp) {
return Some(&relative_path[..end]);
}
end += 1; }
None
}
pub(crate) fn is_untracked_generated_output_path(root: &Path, relative_path: &str) -> bool {
let Some(candidate) = shallowest_generated_output_prefix(relative_path) else {
return false;
};
if index_generated_output_enabled() {
return false;
}
let Some(tracked) = tracked_path_set_for_build_dir_rescue(root) else {
return false;
};
if tracked.contains(relative_path) {
return false;
}
let prefix = format!("{candidate}/");
!tracked.iter().any(|t| t.starts_with(&prefix))
}
fn untracked_generated_output_demotions_inner(
entries: &[DiscoveredEntry],
tracked: &std::collections::HashSet<String>,
) -> std::collections::HashSet<String> {
use std::collections::{HashMap, HashSet};
let mut dir_has_tracked: HashMap<String, bool> = HashMap::new();
let mut demoted: HashSet<String> = HashSet::new();
for entry in entries {
let rel = entry.relative_path.as_str();
if tracked.contains(rel) {
continue; }
let Some(candidate) = shallowest_generated_output_prefix(rel) else {
continue;
};
let has_tracked = *dir_has_tracked
.entry(candidate.to_string())
.or_insert_with(|| {
let prefix = format!("{candidate}/");
tracked.iter().any(|t| t.starts_with(&prefix))
});
if !has_tracked {
demoted.insert(entry.relative_path.clone());
}
}
demoted
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
fn create_file(dir: &Path, name: &str, content: &str) {
let path = dir.join(name);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(path, content).unwrap();
}
#[test]
fn test_discover_files_finds_rs_py_js() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
create_file(tmp.path(), "script.py", "def foo(): pass");
create_file(tmp.path(), "app.js", "function bar() {}");
let files = discover_files(tmp.path()).unwrap();
let extensions: Vec<&str> = files
.iter()
.map(|f| f.relative_path.rsplit('.').next().unwrap())
.collect();
assert!(extensions.contains(&"rs"), "should find .rs");
assert!(extensions.contains(&"py"), "should find .py");
assert!(extensions.contains(&"js"), "should find .js");
assert_eq!(files.len(), 3);
}
#[test]
fn test_discover_files_includes_config_files() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "config.json", "{}");
create_file(tmp.path(), "README.md", "# readme");
create_file(tmp.path(), "Cargo.toml", "[package]");
create_file(tmp.path(), "main.rs", "fn main() {}");
let files = discover_files(tmp.path()).unwrap();
assert_eq!(files.len(), 4, "should discover .rs + .json + .md + .toml");
let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();
assert!(paths.contains(&"config.json"), "should find .json");
assert!(paths.contains(&"README.md"), "should find .md");
assert!(paths.contains(&"Cargo.toml"), "should find .toml");
assert!(paths.contains(&"main.rs"), "should find .rs");
}
#[test]
fn test_discover_files_respects_gitignore() {
let tmp = TempDir::new().unwrap();
fs::create_dir(tmp.path().join(".git")).unwrap();
fs::write(tmp.path().join(".gitignore"), "ignored.rs\n").unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
create_file(tmp.path(), "ignored.rs", "fn ignored() {}");
let files = discover_files(tmp.path()).unwrap();
let paths: Vec<&str> = files
.iter()
.map(|file| file.relative_path.as_str())
.collect();
assert_eq!(
paths,
vec![".gitignore", "main.rs"],
"generic text admission must retain .gitignore while its rule excludes ignored.rs"
);
}
#[test]
fn test_discover_files_normalizes_backslashes() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "src/lib.rs", "pub fn lib() {}");
let files = discover_files(tmp.path()).unwrap();
assert_eq!(files.len(), 1);
assert!(
!files[0].relative_path.contains('\\'),
"should have no backslashes: {:?}",
files[0].relative_path
);
assert!(files[0].relative_path.contains('/') || files[0].relative_path == "src/lib.rs");
}
#[test]
fn test_discover_files_deterministic_sorted_order() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "Zoo.rs", "fn zoo() {}");
create_file(tmp.path(), "apple.rs", "fn apple() {}");
create_file(tmp.path(), "Mango.rs", "fn mango() {}");
let files = discover_files(tmp.path()).unwrap();
assert_eq!(files.len(), 3);
let names: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();
let lower: Vec<String> = names.iter().map(|n| n.to_lowercase()).collect();
let mut sorted = lower.clone();
sorted.sort();
assert_eq!(
lower, sorted,
"files should be in case-insensitive sorted order"
);
}
#[test]
fn test_discover_files_assigns_classification_tags_from_path() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "tests/unit_spec.rs", "fn spec_case() {}");
create_file(tmp.path(), "vendor/pkg/lib.rs", "fn vendored() {}");
create_file(
tmp.path(),
"src/generated/client.generated.rs",
"fn generated() {}",
);
let files = discover_files(tmp.path()).unwrap();
let by_path: std::collections::HashMap<&str, &DiscoveredFile> = files
.iter()
.map(|file| (file.relative_path.as_str(), file))
.collect();
assert!(
by_path["tests/unit_spec.rs"].classification.is_test,
"tests path should set is_test"
);
assert!(
by_path["vendor/pkg/lib.rs"].classification.is_vendor,
"vendor path should set is_vendor"
);
assert!(
by_path["src/generated/client.generated.rs"]
.classification
.is_generated,
"generated path should set is_generated"
);
}
mod cargo_build_dir_skip {
use super::*;
#[test]
fn build_dir_name_matcher_classifies_correctly() {
assert!(is_cargo_build_dir_name("target"));
assert!(is_cargo_build_dir_name("target-wsl"));
assert!(is_cargo_build_dir_name("target-debug"));
assert!(is_cargo_build_dir_name("target-x86_64"));
assert!(is_cargo_build_dir_name("target-CI_2"));
assert!(!is_cargo_build_dir_name("targets"));
assert!(!is_cargo_build_dir_name("target_dir"));
assert!(!is_cargo_build_dir_name("target-"));
assert!(!is_cargo_build_dir_name("target-foo/bar"));
assert!(!is_cargo_build_dir_name("my-target"));
assert!(!is_cargo_build_dir_name("src"));
}
#[test]
fn under_repo_root_build_dir_only_matches_root_child() {
assert!(is_under_repo_root_build_dir("target/debug/foo.rs", None));
assert!(is_under_repo_root_build_dir(
"target-wsl/release/x.rs",
None
));
assert!(!is_under_repo_root_build_dir("src/target/mod.rs", None));
assert!(!is_under_repo_root_build_dir("target", None));
assert!(!is_under_repo_root_build_dir("src/lib.rs", None));
assert!(is_under_repo_root_build_dir(
"build-out/app.rs",
Some("build-out")
));
assert!(!is_under_repo_root_build_dir(
"src/build-out/app.rs",
Some("build-out")
));
}
#[test]
fn discover_files_skips_target_wsl_keeps_source() {
let tmp = TempDir::new().unwrap();
create_file(
tmp.path(),
"target-wsl/debug/build_artifact.rs",
"fn a() {}",
);
create_file(tmp.path(), "target/debug/other.rs", "fn b() {}");
create_file(tmp.path(), "src/lib.rs", "pub fn lib() {}");
create_file(tmp.path(), "targets/config.rs", "fn cfg() {}");
create_file(tmp.path(), "src/target/mod.rs", "fn nested() {}");
let files = discover_files(tmp.path()).unwrap();
let paths: Vec<&str> = files.iter().map(|f| f.relative_path.as_str()).collect();
assert!(
!paths.contains(&"target-wsl/debug/build_artifact.rs"),
"target-wsl/ build output must be skipped: {paths:?}"
);
assert!(
!paths.contains(&"target/debug/other.rs"),
"target/ build output must be skipped: {paths:?}"
);
assert!(
paths.contains(&"src/lib.rs"),
"normal source must be indexed: {paths:?}"
);
assert!(
paths.contains(&"targets/config.rs"),
"non-build dir `targets/` must not be over-skipped: {paths:?}"
);
assert!(
paths.contains(&"src/target/mod.rs"),
"nested source dir `src/target/` must be preserved: {paths:?}"
);
}
#[test]
fn discover_all_files_skips_target_wsl_keeps_source() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "target-wsl/debug/artifact.rs", "fn a() {}");
create_file(tmp.path(), "src/main.rs", "fn main() {}");
create_file(tmp.path(), "targets/x.rs", "fn x() {}");
let entries = discover_all_files(tmp.path()).unwrap();
let paths: Vec<&str> = entries.iter().map(|e| e.relative_path.as_str()).collect();
assert!(
!paths.contains(&"target-wsl/debug/artifact.rs"),
"target-wsl/ build output must be skipped in full discovery: {paths:?}"
);
assert!(
paths.contains(&"src/main.rs"),
"normal source must be discovered: {paths:?}"
);
assert!(
paths.contains(&"targets/x.rs"),
"non-build dir `targets/` must not be over-skipped: {paths:?}"
);
}
}
mod unsupported_language {
use super::*;
#[test]
fn unsupported_language_decision_demotes_normal_to_metadata_only() {
let normal = AdmissionDecision::normal();
let demoted = unsupported_language_decision(normal);
assert_eq!(demoted.tier, AdmissionTier::MetadataOnly);
assert_eq!(demoted.reason, Some(SkipReason::UnsupportedLanguage));
}
#[test]
fn unsupported_language_decision_preserves_specific_skip_reasons() {
for original in [
AdmissionDecision::skip(AdmissionTier::HardSkip, SkipReason::SizeCeiling),
AdmissionDecision::skip(AdmissionTier::MetadataOnly, SkipReason::SizeThreshold),
AdmissionDecision::skip(AdmissionTier::MetadataOnly, SkipReason::BinaryContent),
AdmissionDecision::skip(
AdmissionTier::MetadataOnly,
SkipReason::DependencyLockfile,
),
] {
assert_eq!(
unsupported_language_decision(original),
original,
"non-Normal decision must be returned unchanged"
);
}
}
#[test]
fn unsupported_language_reason_renders_honestly() {
assert_eq!(
SkipReason::UnsupportedLanguage.to_string(),
"unsupported language"
);
}
}
fn git_ignores(root: &Path, relative_path: &str) -> bool {
let excludes = root.join(".no-global-excludes");
let output = crate::process_util::hidden_command("git")
.arg("-C")
.arg(root)
.arg("-c")
.arg(format!("core.excludesFile={}", excludes.display()))
.args(["check-ignore", "-q", "--", relative_path])
.output()
.expect("run git check-ignore");
match output.status.code() {
Some(0) => true,
Some(1) => false,
other => panic!(
"git check-ignore {relative_path} failed ({other:?}): {}",
String::from_utf8_lossy(&output.stderr)
),
}
}
#[test]
fn nested_gitignore_matches_like_git_check_ignore() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
let init = crate::process_util::hidden_command("git")
.args(["init", "-q"])
.current_dir(root)
.output()
.expect("git init");
assert!(init.status.success(), "git init failed");
create_file(root, ".gitignore", "*.tmp\n");
create_file(
root,
"sub/.gitignore",
"/needle.txt\nlocal.log\n!keep.tmp\nbuild/\ndeep/*.rs\n",
);
create_file(root, "br[a]ck/.gitignore", "/x.txt\n");
let paths = [
"needle.txt",
"sub/needle.txt",
"sub/deeper/needle.txt",
"local.log",
"sub/local.log",
"sub/deeper/local.log",
"keep.tmp",
"sub/keep.tmp",
"sub/other.tmp",
"build/x.rs",
"sub/build/x.rs",
"sub/deep/a.rs",
"sub/x/deep/a.rs",
"deep/a.rs",
"br[a]ck/x.txt",
"bra/x.txt",
"x.txt",
];
for path in paths {
create_file(root, path, "placeholder\n");
}
let gitignore = load_gitignore(root).expect("rules were loaded");
let mut disagreements = Vec::new();
for path in paths {
let ours = gitignore
.matched_path_or_any_parents(path, false)
.is_ignore();
let git = git_ignores(root, path);
if ours != git {
disagreements.push(format!("{path}: ours={ours} git={git}"));
}
}
assert!(disagreements.is_empty(), "{disagreements:#?}");
}
#[test]
fn nested_gitignore_applies_without_a_root_gitignore() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "sub/.gitignore", "/needle.txt\n");
let gitignore = load_gitignore(tmp.path()).expect("the nested rules were loaded");
assert!(
gitignore
.matched_path_or_any_parents("sub/needle.txt", false)
.is_ignore()
);
assert!(
!gitignore
.matched_path_or_any_parents("needle.txt", false)
.is_ignore()
);
}
mod hidden_path {
use super::*;
#[test]
fn detects_only_vcs_and_runtime_state_components() {
assert!(path_is_hard_scope_excluded(Path::new(
".symforge/index.bin"
)));
assert!(path_is_hard_scope_excluded(Path::new(
"nested/.symforge/tee/edit.rs"
)));
assert!(path_is_hard_scope_excluded(Path::new(".git/objects/pack")));
assert!(path_is_hard_scope_excluded(Path::new(
"nested/.SYmFoRgE/state.rs"
)));
}
#[test]
fn listed_spelling_reports_the_name_each_directory_lists() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "Dir/Secret.rs", "placeholder\n");
let listed = |spelling: &str| listed_spelling(tmp.path(), Path::new(spelling));
assert_eq!(
listed("Dir/Secret.rs"),
Some(PathBuf::from("Dir/Secret.rs"))
);
assert_eq!(
listed("dir/SECRET.rs"),
Some(PathBuf::from("Dir/Secret.rs"))
);
assert_eq!(
listed("Dir/\u{17F}ecret.rs"),
Some(PathBuf::from("Dir/Secret.rs"))
);
assert_eq!(listed("Dir/absent.rs"), None);
}
#[test]
fn detects_unicode_case_fold_spellings() {
assert!(path_is_hard_scope_excluded(Path::new(
".\u{17F}ymforge/index.bin"
)));
assert!(path_is_hard_scope_excluded(Path::new(".GIT/config")));
}
#[test]
fn curation_engine_temps_leave_the_scout_plan_unchanged() {
let tmp = TempDir::new().unwrap();
create_file(
tmp.path(),
"src/lib.rs",
"pub fn exact() -> bool { true }\n",
);
let cancel = std::sync::atomic::AtomicBool::new(false);
let scout = || {
format!(
"{:?}",
scout_repository_with_exclusions_cancellable(
tmp.path(),
&SourceExclusions::default(),
&cancel,
)
.unwrap()
)
};
let before = scout();
create_file(
tmp.path(),
".symforge-curation-probe-destination-xtA6BG",
"before",
);
create_file(
tmp.path(),
".symforge-curation-probe-replacement-Q1",
"after",
);
create_file(tmp.path(), ".symforge-curation-policy-Z9", "[policy]\n");
assert_eq!(scout(), before);
assert!(!path_is_hard_scope_excluded(Path::new(
".symforge-knowledge.toml"
)));
}
#[test]
fn allows_repository_owned_hidden_knowledge_and_traversal_segments() {
assert!(!path_is_hard_scope_excluded(Path::new(
".github/workflows/ci.yml"
)));
assert!(!path_is_hard_scope_excluded(Path::new(".codex/AGENTS.md")));
assert!(!path_is_hard_scope_excluded(Path::new(".travis.yml")));
assert!(!path_is_hard_scope_excluded(Path::new("src/main.rs")));
assert!(!path_is_hard_scope_excluded(Path::new(
"../sibling/main.rs"
)));
assert!(!path_is_hard_scope_excluded(Path::new("src/a.b.c/main.rs")));
}
#[test]
fn discover_all_files_includes_hidden_knowledge_but_excludes_runtime_internals() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), ".github/workflows/ci.yml", "name: ci\n");
create_file(tmp.path(), ".travis.yml", "language: rust\n");
create_file(tmp.path(), ".symforge/tee/snapshot.rs", "fn state() {}\n");
create_file(tmp.path(), ".git/objects/fake.rs", "fn git_state() {}\n");
create_file(tmp.path(), "src/main.rs", "fn main() {}");
let entries = discover_all_files(tmp.path()).unwrap();
let paths: Vec<&str> = entries.iter().map(|e| e.relative_path.as_str()).collect();
assert!(
paths.contains(&".github/workflows/ci.yml"),
"repository-owned hidden knowledge must be discovered: {paths:?}"
);
assert!(
paths.contains(&".travis.yml"),
"repository-owned hidden files must be discovered: {paths:?}"
);
assert!(paths.contains(&"src/main.rs"));
assert!(
!paths.contains(&".symforge/tee/snapshot.rs")
&& !paths.contains(&".git/objects/fake.rs"),
"VCS/runtime internals must be pruned independently: {paths:?}"
);
}
#[test]
fn symforge_is_hard_excluded_under_every_state_placement() {
let tmp = TempDir::new().unwrap();
create_file(
tmp.path(),
".gitignore",
"!/.symforge/\n!/.symforge/**\n!/.git/\n!/.git/**\n",
);
create_file(tmp.path(), ".github/workflows/ci.yml", "name: ci\n");
create_file(tmp.path(), ".codex/AGENTS.md", "# instructions\n");
create_file(tmp.path(), "src/main.rs", "fn main() {}\n");
create_file(
tmp.path(),
".symforge/project-local/index.rs",
"fn project_state() {}\n",
);
create_file(
tmp.path(),
".symforge/user-local/projects/project-v1/state.rs",
"fn user_state() {}\n",
);
create_file(
tmp.path(),
"nested/.symforge/memory-only/stale.rs",
"fn stale_state() {}\n",
);
create_file(tmp.path(), ".git/objects/fake.rs", "fn vcs_internal() {}\n");
let plan = scout_repository(tmp.path()).expect("scout repository");
let paths: Vec<&str> = plan
.entries
.iter()
.filter_map(|entry| entry.path.normalized_utf8.as_deref())
.collect();
assert!(
paths.contains(&".github/workflows/ci.yml") && paths.contains(&".codex/AGENTS.md"),
"repository-owned hidden knowledge is the control proving this is a hard runtime-state exclusion, not a blanket hidden-path skip: {paths:?}"
);
assert!(
paths.iter().all(|path| {
!path.split('/').any(|component| {
component.eq_ignore_ascii_case(".symforge")
|| component.eq_ignore_ascii_case(".git")
})
}),
"VCS/runtime internals must stay outside the manifest for every placement: {paths:?}"
);
assert!(paths.contains(&"src/main.rs"));
}
}
mod build_dir_tracked_rescue {
use super::*;
#[test]
fn discover_all_files_rescues_tracked_target_specs_dir() {
let tmp = TempDir::new().unwrap();
let root = tmp.path();
let run = |args: &[&str]| {
crate::process_util::hidden_command("git")
.args(args)
.current_dir(root)
.output()
.expect("git command");
};
run(&["init"]);
run(&["config", "user.email", "test@test.com"]);
run(&["config", "user.name", "Test"]);
create_file(root, "target-specs/i686.json", "{}\n");
create_file(root, "target-specs/README.md", "# specs\n");
create_file(root, "src/main.rs", "fn main() {}");
run(&["add", "target-specs", "src"]);
run(&["commit", "-m", "initial"]);
create_file(root, "target/debug/artifact.rs", "fn a() {}");
let entries = discover_all_files(root).unwrap();
let paths: Vec<&str> = entries.iter().map(|e| e.relative_path.as_str()).collect();
assert!(
paths.contains(&"target-specs/i686.json"),
"tracked target-specs/ source must be rescued: {paths:?}"
);
assert!(
paths.contains(&"target-specs/README.md"),
"tracked target-specs/ source must be rescued: {paths:?}"
);
assert!(
paths.contains(&"src/main.rs"),
"normal source must be discovered: {paths:?}"
);
assert!(
!paths.contains(&"target/debug/artifact.rs"),
"untracked build output must still be skipped: {paths:?}"
);
}
#[test]
fn discover_all_files_without_git_keeps_conservative_build_dir_skip() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "target-wsl/debug/artifact.rs", "fn a() {}");
create_file(tmp.path(), "src/main.rs", "fn main() {}");
let entries = discover_all_files(tmp.path()).unwrap();
let paths: Vec<&str> = entries.iter().map(|e| e.relative_path.as_str()).collect();
assert!(
!paths.contains(&"target-wsl/debug/artifact.rs"),
"non-git tree must still skip target-* build dirs: {paths:?}"
);
assert!(
paths.contains(&"src/main.rs"),
"normal source must be discovered: {paths:?}"
);
}
}
mod generated_output_demotion {
use super::*;
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn init_git(root: &Path) -> impl Fn(&[&str]) + '_ {
let run = move |args: &[&str]| {
crate::process_util::hidden_command("git")
.args(args)
.current_dir(root)
.output()
.expect("git command");
};
run(&["init"]);
run(&["config", "user.email", "test@test.com"]);
run(&["config", "user.name", "Test"]);
run
}
#[test]
fn generated_output_dir_name_matcher_is_conservative() {
for name in [
"dist",
"build",
"out",
"output",
"cache",
".cache",
"generated",
"graphify-out",
"codegen-output",
"Dist",
] {
assert!(is_generated_output_dir_name(name), "{name} should match");
}
for name in ["src", "docs", "outline", "scout", "distros", "builder"] {
assert!(!is_generated_output_dir_name(name), "{name} must NOT match");
}
}
#[test]
fn untracked_cache_dir_is_demoted_tracked_dist_is_not() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
let root = tmp.path();
let run = init_git(root);
create_file(root, "src/main.rs", "fn main() {}");
create_file(root, "frontend/dist/bundle.js", "var x = 1;");
run(&["add", "src", "frontend"]);
run(&["commit", "-m", "initial"]);
create_file(root, "graphify-out/cache/a.json", "{\"k\": 1}");
create_file(root, "graphify-out/cache/b.json", "{\"k\": 2}");
create_file(root, "src/new_module.rs", "fn newer() {}");
let entries = discover_all_files(root).unwrap();
let demoted = untracked_generated_output_demotions(root, &entries);
assert!(
demoted.contains("graphify-out/cache/a.json")
&& demoted.contains("graphify-out/cache/b.json"),
"untracked generated-output files must be demoted: {demoted:?}"
);
assert!(
!demoted.contains("frontend/dist/bundle.js"),
"tracked build output must NOT be demoted: {demoted:?}"
);
assert!(
!demoted.contains("src/main.rs") && !demoted.contains("src/new_module.rs"),
"normal source (tracked or not) must NOT be demoted: {demoted:?}"
);
}
#[test]
fn generated_dir_with_any_tracked_file_is_fully_admitted() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
let root = tmp.path();
let run = init_git(root);
create_file(root, "out/kept.json", "{}");
run(&["add", "out"]);
run(&["commit", "-m", "initial"]);
create_file(root, "out/generated.json", "{\"g\": true}");
let entries = discover_all_files(root).unwrap();
let demoted = untracked_generated_output_demotions(root, &entries);
assert!(
demoted.is_empty(),
"a generated-output dir containing a tracked file must not be demoted: {demoted:?}"
);
}
#[test]
fn non_git_tree_demotes_nothing() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "dist/bundle.js", "var x = 1;");
create_file(tmp.path(), "src/main.rs", "fn main() {}");
let entries = discover_all_files(tmp.path()).unwrap();
let demoted = untracked_generated_output_demotions(tmp.path(), &entries);
assert!(
demoted.is_empty(),
"no git evidence → fail open, admit everything: {demoted:?}"
);
}
#[test]
fn opt_in_env_restores_full_indexing() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
let root = tmp.path();
let run = init_git(root);
create_file(root, "src/main.rs", "fn main() {}");
run(&["add", "src"]);
run(&["commit", "-m", "initial"]);
create_file(root, "graphify-out/cache/a.json", "{}");
let entries = discover_all_files(root).unwrap();
let tracked: std::collections::HashSet<String> =
std::iter::once("src/main.rs".to_string()).collect();
let demoted = untracked_generated_output_demotions_inner(&entries, &tracked);
assert!(demoted.contains("graphify-out/cache/a.json"), "{demoted:?}");
struct EnvGuard(Option<std::ffi::OsString>);
#[allow(unsafe_code)] impl Drop for EnvGuard {
fn drop(&mut self) {
unsafe {
match &self.0 {
Some(v) => std::env::set_var(INDEX_GENERATED_OUTPUT_ENV, v),
None => std::env::remove_var(INDEX_GENERATED_OUTPUT_ENV),
}
}
}
}
let _guard = EnvGuard(std::env::var_os(INDEX_GENERATED_OUTPUT_ENV));
#[allow(unsafe_code)]
unsafe {
std::env::set_var(INDEX_GENERATED_OUTPUT_ENV, "1")
};
let demoted = untracked_generated_output_demotions(root, &entries);
assert!(
demoted.is_empty(),
"opt-in must restore full indexing: {demoted:?}"
);
}
}
#[test]
fn test_is_forbidden_root_blocks_home_dir() {
let home = home_dir();
if let Some(h) = home {
assert!(is_forbidden_root(&h), "home directory should be forbidden");
}
}
#[test]
fn test_is_forbidden_root_blocks_drive_root() {
#[cfg(target_os = "windows")]
assert!(is_forbidden_root(Path::new("C:\\")));
#[cfg(not(target_os = "windows"))]
assert!(is_forbidden_root(Path::new("/")));
}
#[test]
fn test_is_forbidden_root_blocks_system_dirs() {
assert!(is_forbidden_root(Path::new("/tmp")));
assert!(is_forbidden_root(Path::new("/home")));
}
#[test]
fn test_is_forbidden_root_allows_project_dirs() {
let tmp = TempDir::new().unwrap();
assert!(
!is_forbidden_root(tmp.path()),
"temp project dir should be allowed"
);
}
#[test]
fn test_is_forbidden_root_allows_project_named_tmp() {
let tmp = TempDir::new().unwrap();
let project = tmp.path().join("projects").join("tmp");
std::fs::create_dir_all(&project).unwrap();
assert!(
!is_forbidden_root(&project),
"project at C:\\projects\\tmp must not be rejected by basename"
);
}
#[test]
fn test_is_forbidden_root_allows_project_named_var() {
let tmp = TempDir::new().unwrap();
let project = tmp.path().join("workspace").join("var");
std::fs::create_dir_all(&project).unwrap();
assert!(
!is_forbidden_root(&project),
"project at workspace/var must not be rejected by basename"
);
}
#[test]
fn test_is_forbidden_root_still_blocks_top_level_tmp_on_unix() {
#[cfg(unix)]
{
let path = std::path::Path::new("/tmp");
if path.exists() {
assert!(
is_forbidden_root(path),
"/tmp must still be blocked as system path"
);
}
}
}
#[test]
fn test_is_forbidden_root_still_blocks_windows_system_paths() {
#[cfg(target_os = "windows")]
{
let path = std::path::Path::new(r"C:\Windows\System32");
if path.exists() {
assert!(
is_forbidden_root(path),
"C:\\Windows\\System32 must remain blocked"
);
}
}
}
#[test]
fn test_binary_sniff_detects_null_bytes() {
let content = b"hello\x00world";
assert!(is_binary_content(content));
}
#[test]
fn test_binary_sniff_allows_pure_utf8() {
let content = b"fn main() { println!(\"hello\"); }";
assert!(!is_binary_content(content));
}
#[test]
fn test_binary_sniff_empty_file() {
assert!(!is_binary_content(b""));
}
#[test]
fn test_binary_sniff_detects_invalid_utf8() {
let content: &[u8] = &[0x80, 0x81, 0x82, 0x83, 0x84];
assert!(is_binary_content(content));
}
#[test]
fn test_binary_sniff_forgives_multibyte_cut_at_window_boundary() {
let sniff = crate::domain::index::BINARY_SNIFF_BYTES;
let mut content = vec![b'a'; sniff - 2];
content.extend_from_slice("─".as_bytes());
content.extend_from_slice(b" trailing source text beyond the sniff window");
assert!(
!is_binary_content(&content),
"a multibyte char cut by the sniff window must not read as binary"
);
}
#[test]
fn test_binary_sniff_still_detects_interior_invalid_utf8() {
let sniff = crate::domain::index::BINARY_SNIFF_BYTES;
let mut content = vec![b'a'; 100];
content.push(0x80); content.extend(vec![b'b'; sniff]); assert!(
is_binary_content(&content),
"interior invalid UTF-8 must stay classified as binary"
);
}
#[test]
fn test_binary_sniff_detects_high_control_ratio() {
let mut content = Vec::new();
content.extend(std::iter::repeat_n(0x01, 80)); content.extend(std::iter::repeat_n(b'A', 20)); assert!(is_binary_content(&content));
}
#[test]
fn test_binary_sniff_allows_low_control_ratio() {
let content = b"line1\tvalue1\nline2\tvalue2\nline3\tvalue3\n";
assert!(!is_binary_content(content));
}
#[test]
fn test_binary_sniff_allows_common_whitespace_controls() {
let content = b"col1\tcol2\tcol3\r\nval1\tval2\tval3\r\n";
assert!(!is_binary_content(content));
}
#[test]
fn scout_utf8_sample_boundary_uses_declared_file_size() {
let sniff = crate::domain::index::BINARY_SNIFF_BYTES;
let bytes = format!("// {}\npub fn item() {{}}\n", "é".repeat(sniff)).into_bytes();
let sample = &bytes[..sniff];
assert!(std::str::from_utf8(sample).is_err());
assert_eq!(
classify_admission(
std::path::Path::new("large.rs"),
bytes.len() as u64,
Some(sample)
)
.tier,
AdmissionTier::Normal,
);
assert_eq!(
classify_admission(
std::path::Path::new("incomplete.rs"),
sample.len() as u64,
Some(sample)
)
.tier,
AdmissionTier::MetadataOnly,
);
}
use crate::domain::index::{AdmissionDecision, AdmissionTier, SkipReason};
#[test]
fn test_huge_text_file_is_hard_skip() {
let decision =
classify_admission(std::path::Path::new("huge.txt"), 150 * 1024 * 1024, None);
assert_eq!(decision.tier, AdmissionTier::HardSkip);
assert_eq!(decision.reason, Some(SkipReason::SizeCeiling));
}
#[test]
fn test_small_ckpt_is_metadata_only() {
let decision = classify_admission(std::path::Path::new("model.ckpt"), 50 * 1024, None);
assert_eq!(decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(decision.reason, Some(SkipReason::DenylistedExtension));
}
#[test]
fn test_huge_ckpt_is_hard_skip() {
let decision = classify_admission(std::path::Path::new("big.ckpt"), 4_200_000_000, None);
assert_eq!(decision.tier, AdmissionTier::HardSkip);
assert_eq!(decision.reason, Some(SkipReason::SizeCeiling));
}
#[test]
fn test_large_json_is_metadata_only() {
let decision = classify_admission(std::path::Path::new("big.json"), 2 * 1024 * 1024, None);
assert_eq!(decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(decision.reason, Some(SkipReason::SizeThreshold));
}
#[test]
fn test_small_txt_is_normal() {
let decision = classify_admission(std::path::Path::new("readme.txt"), 50 * 1024, None);
assert_eq!(decision, AdmissionDecision::normal());
}
#[test]
fn test_medium_rust_source_is_normal() {
let decision = classify_admission(std::path::Path::new("big_module.rs"), 500 * 1024, None);
assert_eq!(decision, AdmissionDecision::normal());
}
#[test]
fn test_oversized_code_file_under_4mb_is_normal() {
for name in ["orchestrator.rs", "big.py", "huge.ts", "large.pm"] {
let decision = classify_admission(std::path::Path::new(name), 1_200_000, None);
assert_eq!(
decision,
AdmissionDecision::normal(),
"1.2MB code file {name} must stay Tier-1"
);
}
}
#[test]
fn test_code_file_above_4mb_is_metadata_only() {
let decision =
classify_admission(std::path::Path::new("generated.rs"), 5 * 1024 * 1024, None);
assert_eq!(decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(decision.reason, Some(SkipReason::SizeThreshold));
}
#[test]
fn test_data_formats_keep_1mb_threshold() {
for name in ["big.yaml", "big.toml", "big.md", "big.html"] {
let decision = classify_admission(std::path::Path::new(name), 1_200_000, None);
assert_eq!(
decision.tier,
AdmissionTier::MetadataOnly,
"1.2MB data file {name} must stay Tier-2"
);
assert_eq!(decision.reason, Some(SkipReason::SizeThreshold));
}
}
#[test]
fn test_binary_content_is_metadata_only() {
let content = b"ELF\x00\x00\x00binary";
let decision =
classify_admission(std::path::Path::new("unknown_file"), 1024, Some(content));
assert_eq!(decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(decision.reason, Some(SkipReason::BinaryContent));
}
#[test]
fn test_svg_not_denylisted() {
let decision = classify_admission(std::path::Path::new("icon.svg"), 50 * 1024, None);
assert_eq!(decision, AdmissionDecision::normal());
}
#[test]
fn test_large_svg_is_metadata_only_by_size() {
let decision = classify_admission(std::path::Path::new("huge.svg"), 2 * 1024 * 1024, None);
assert_eq!(decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(decision.reason, Some(SkipReason::SizeThreshold));
}
#[test]
fn test_discovery_skips_denylisted_extension() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
fs::write(tmp.path().join("model.safetensors"), b"fake model bytes").unwrap();
let entries = discover_all_files(tmp.path()).unwrap();
let mut rs_found = false;
let mut safetensors_skipped = false;
let mut safetensors_reason = None;
for entry in &entries {
let size = entry.file_size;
let decision = classify_admission(&entry.absolute_path, size, None);
if entry.relative_path == "main.rs" {
assert_eq!(
decision.tier,
AdmissionTier::Normal,
".rs file should be Normal"
);
rs_found = true;
}
if entry.relative_path == "model.safetensors" {
assert_eq!(
decision.tier,
AdmissionTier::MetadataOnly,
".safetensors should be MetadataOnly"
);
safetensors_skipped = true;
safetensors_reason = decision.reason;
}
}
assert!(rs_found, ".rs file must appear in discovered entries");
assert!(
safetensors_skipped,
".safetensors must appear in discovered entries and be skipped"
);
assert_eq!(
safetensors_reason,
Some(SkipReason::DenylistedExtension),
".safetensors skip reason must be DenylistedExtension"
);
}
#[test]
fn test_discovery_deferred_binary_sniff_reclassifies() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "lib.rs", "pub fn hello() {}");
let mut binary_content = vec![0u8; 512]; binary_content.extend_from_slice(b"some trailing text");
fs::write(tmp.path().join("custom.dat"), &binary_content).unwrap();
let entries = discover_all_files(tmp.path()).unwrap();
let mut rs_normal = false;
let mut dat_skipped = false;
let mut dat_reason = None;
for entry in &entries {
let size = entry.file_size;
let pre = classify_admission(&entry.absolute_path, size, None);
if entry.relative_path == "lib.rs" {
assert_eq!(pre.tier, AdmissionTier::Normal);
rs_normal = true;
}
if entry.relative_path == "custom.dat" {
assert_eq!(
pre.tier,
AdmissionTier::Normal,
"custom.dat should be Normal before binary sniff"
);
let content = fs::read(&entry.absolute_path).unwrap();
let post = classify_admission(&entry.absolute_path, size, Some(&content));
assert_eq!(
post.tier,
AdmissionTier::MetadataOnly,
"custom.dat should be MetadataOnly after binary sniff"
);
dat_skipped = true;
dat_reason = post.reason;
}
}
assert!(rs_normal, "lib.rs must be Normal");
assert!(
dat_skipped,
"custom.dat must be discovered and reclassified"
);
assert_eq!(
dat_reason,
Some(SkipReason::BinaryContent),
"custom.dat skip reason must be BinaryContent"
);
}
mod bounded_discovery {
use super::*;
use std::ffi::OsString;
use std::sync::Mutex;
static ENV_LOCK: Mutex<()> = Mutex::new(());
struct LimitEnvGuard {
files_prev: Option<OsString>,
bytes_prev: Option<OsString>,
catalog_metadata_bytes_prev: Option<OsString>,
}
#[allow(unsafe_code)] impl LimitEnvGuard {
fn set(
max_files: Option<&str>,
max_bytes: Option<&str>,
max_catalog_metadata_bytes: Option<&str>,
) -> Self {
let files_prev = std::env::var_os(MAX_INDEX_FILES_ENV);
let bytes_prev = std::env::var_os(MAX_INDEX_BYTES_ENV);
let catalog_metadata_bytes_prev = std::env::var_os(MAX_CATALOG_METADATA_BYTES_ENV);
unsafe {
match max_files {
Some(v) => std::env::set_var(MAX_INDEX_FILES_ENV, v),
None => std::env::remove_var(MAX_INDEX_FILES_ENV),
}
match max_bytes {
Some(v) => std::env::set_var(MAX_INDEX_BYTES_ENV, v),
None => std::env::remove_var(MAX_INDEX_BYTES_ENV),
}
match max_catalog_metadata_bytes {
Some(v) => std::env::set_var(MAX_CATALOG_METADATA_BYTES_ENV, v),
None => std::env::remove_var(MAX_CATALOG_METADATA_BYTES_ENV),
}
}
Self {
files_prev,
bytes_prev,
catalog_metadata_bytes_prev,
}
}
}
#[allow(unsafe_code)] impl Drop for LimitEnvGuard {
fn drop(&mut self) {
unsafe {
match &self.files_prev {
Some(v) => std::env::set_var(MAX_INDEX_FILES_ENV, v),
None => std::env::remove_var(MAX_INDEX_FILES_ENV),
}
match &self.bytes_prev {
Some(v) => std::env::set_var(MAX_INDEX_BYTES_ENV, v),
None => std::env::remove_var(MAX_INDEX_BYTES_ENV),
}
match &self.catalog_metadata_bytes_prev {
Some(v) => std::env::set_var(MAX_CATALOG_METADATA_BYTES_ENV, v),
None => std::env::remove_var(MAX_CATALOG_METADATA_BYTES_ENV),
}
}
}
}
#[test]
fn default_limits_are_generous() {
let limits = DiscoveryLimits::default();
assert_eq!(limits.max_files, DEFAULT_MAX_INDEX_FILES);
assert_eq!(limits.max_bytes, DEFAULT_MAX_INDEX_BYTES);
assert_eq!(
limits.max_catalog_metadata_bytes,
DEFAULT_MAX_CATALOG_METADATA_BYTES
);
assert!(limits.max_files >= 200_000);
}
#[test]
fn parse_positive_env_rejects_zero_empty_and_garbage() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("0"), Some("not-a-number"), Some("0"));
assert_eq!(parse_positive_env(MAX_INDEX_FILES_ENV), None);
assert_eq!(parse_positive_env(MAX_INDEX_BYTES_ENV), None);
assert_eq!(parse_positive_env(MAX_CATALOG_METADATA_BYTES_ENV), None);
let limits = DiscoveryLimits::from_env();
assert_eq!(limits.max_files, DEFAULT_MAX_INDEX_FILES);
assert_eq!(limits.max_bytes, DEFAULT_MAX_INDEX_BYTES);
assert_eq!(
limits.max_catalog_metadata_bytes,
DEFAULT_MAX_CATALOG_METADATA_BYTES
);
}
#[test]
fn from_env_honors_valid_override() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("5"), Some("4096"), Some("8192"));
let limits = DiscoveryLimits::from_env();
assert_eq!(limits.max_files, 5);
assert_eq!(limits.max_bytes, 4096);
assert_eq!(limits.max_catalog_metadata_bytes, 8192);
}
#[test]
fn normal_repo_indexes_under_default_cap() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(None, None, None);
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
create_file(tmp.path(), "lib.rs", "pub fn f() {}");
create_file(tmp.path(), "README.md", "# hi");
let files = discover_files(tmp.path()).expect("normal repo indexes fine");
assert_eq!(files.len(), 3);
let entries = discover_all_files(tmp.path()).expect("normal repo full-discovery fine");
assert!(entries.len() >= 3);
}
#[test]
fn over_file_cap_yields_graceful_error_not_panic() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("2"), None, None);
let tmp = TempDir::new().unwrap();
for i in 0..5 {
create_file(tmp.path(), &format!("f{i}.rs"), "fn x() {}");
}
let err = discover_files(tmp.path()).expect_err("over file cap must error");
let msg = err.to_string();
assert!(
msg.contains("tree too large to index"),
"error must be the graceful over-cap message: {msg}"
);
assert!(
msg.contains(MAX_INDEX_FILES_ENV),
"error must name the override knob: {msg}"
);
let err2 = discover_all_files(tmp.path()).expect_err("full discovery over cap errors");
assert!(err2.to_string().contains("tree too large to index"));
}
#[test]
fn over_byte_cap_yields_graceful_error() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("1000000"), Some("8"), None);
let tmp = TempDir::new().unwrap();
create_file(
tmp.path(),
"big.rs",
"fn this_is_more_than_eight_bytes() {}",
);
let err = discover_all_files(tmp.path()).expect_err("over byte cap must error");
let msg = err.to_string();
assert!(
msg.contains("tree too large to index"),
"error must be the graceful over-cap message: {msg}"
);
assert!(
msg.contains(MAX_INDEX_BYTES_ENV),
"error must name the byte override knob: {msg}"
);
}
#[test]
fn scout_sparse_hard_skip_does_not_consume_ingest_budget() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("1000000"), Some("1024"), None);
let tmp = TempDir::new().unwrap();
let sparse = tmp.path().join("huge.log");
std::fs::File::create(&sparse)
.unwrap()
.set_len(HARD_SKIP_BYTES + 1)
.unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
let entries = discover_all_files(tmp.path())
.expect("metadata-terminal hard skip must not consume admitted-byte budget");
assert_eq!(entries.len(), 2);
assert!(
entries
.iter()
.any(|entry| entry.relative_path == "huge.log")
);
assert!(entries.iter().any(|entry| entry.relative_path == "main.rs"));
}
#[test]
fn catalog_entry_ceiling_never_publishes_false_complete_manifest() {
let _lock = ENV_LOCK.lock().unwrap();
let _guard = LimitEnvGuard::set(Some("1"), Some("1073741824"), None);
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
create_file(tmp.path(), "model.gguf", "catalog only");
let error = scout_repository(tmp.path())
.expect_err("an over-cap candidate must not return a partial scout plan");
assert!(error.to_string().contains("tree too large to index"));
assert!(error.to_string().contains(MAX_INDEX_FILES_ENV));
}
#[test]
fn catalog_metadata_budget_is_independent_and_never_publishes_partial_manifest() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
let artifact = tmp.path().join("model.gguf");
std::fs::File::create(&artifact)
.unwrap()
.set_len(HARD_SKIP_BYTES + 1)
.unwrap();
let exact_usage = {
let _guard = LimitEnvGuard::set(Some("10"), Some("1"), Some("1048576"));
let plan = scout_repository(tmp.path())
.expect("metadata-terminal payload size must not consume catalog metadata");
assert_eq!(plan.usage.catalog_entries, 1);
assert_eq!(plan.usage.admitted_content_bytes, 0);
assert!(plan.usage.catalog_metadata_bytes > 0);
plan.usage.catalog_metadata_bytes
};
{
let below_exact = (exact_usage - 1).to_string();
let _guard = LimitEnvGuard::set(Some("10"), Some("1"), Some(&below_exact));
let error = scout_repository(tmp.path())
.expect_err("metadata over-cap must return no partial scout plan");
assert!(
error
.to_string()
.contains("catalog metadata capacity exceeded")
);
assert!(error.to_string().contains(MAX_CATALOG_METADATA_BYTES_ENV));
}
let exact = exact_usage.to_string();
let _guard = LimitEnvGuard::set(Some("10"), Some("1"), Some(&exact));
let plan = scout_repository(tmp.path())
.expect("the exact canonical metadata byte ceiling must be accepted");
assert_eq!(plan.usage.catalog_metadata_bytes, exact_usage);
assert_eq!(plan.entries.len(), 1);
}
#[test]
fn cold_start_budget_exhaustion_yields_distinct_typed_capacity_reasons() {
let _lock = ENV_LOCK.lock().unwrap();
let entry_limited = TempDir::new().unwrap();
create_file(entry_limited.path(), "main.rs", "fn main() {}");
create_file(entry_limited.path(), "README.md", "knowledge");
let entry_error = {
let _guard = LimitEnvGuard::set(Some("1"), Some("1073741824"), None);
scout_repository(entry_limited.path())
.expect_err("entry exhaustion must publish no partial cold-start plan")
};
let entry_reason = entry_error
.downcast_ref::<ScoutCapacityError>()
.expect("entry exhaustion must remain a typed capacity refusal")
.reason();
let metadata_limited = TempDir::new().unwrap();
create_file(metadata_limited.path(), "README.md", "knowledge");
let exact_metadata_bytes = {
let _guard = LimitEnvGuard::set(Some("10"), Some("1073741824"), None);
scout_repository(metadata_limited.path())
.expect("calibration scout must fit")
.usage
.catalog_metadata_bytes
};
let metadata_error = {
let below_exact = (exact_metadata_bytes - 1).to_string();
let _guard = LimitEnvGuard::set(Some("10"), Some("1073741824"), Some(&below_exact));
scout_repository(metadata_limited.path())
.expect_err("metadata exhaustion must publish no partial cold-start plan")
};
let metadata_reason = metadata_error
.downcast_ref::<ScoutCapacityError>()
.expect("metadata exhaustion must remain a typed capacity refusal")
.reason();
assert_eq!(
entry_reason,
crate::domain::FreshnessReason::CatalogEntryCapacityExceeded
);
assert_eq!(
metadata_reason,
crate::domain::FreshnessReason::CatalogMetadataCapacityExceeded
);
assert_ne!(entry_reason, metadata_reason);
}
#[test]
fn raised_cap_lets_a_previously_over_cap_tree_index() {
let _lock = ENV_LOCK.lock().unwrap();
let tmp = TempDir::new().unwrap();
for i in 0..4 {
create_file(tmp.path(), &format!("f{i}.rs"), "fn x() {}");
}
{
let _guard = LimitEnvGuard::set(Some("2"), None, None);
assert!(discover_files(tmp.path()).is_err());
}
{
let _guard = LimitEnvGuard::set(Some("100"), None, None);
let files = discover_files(tmp.path()).expect("raised cap indexes the tree");
assert_eq!(files.len(), 4);
}
}
}
mod metadata_first_scout {
use super::*;
use crate::domain::{AccessErrorKind, CoverageStatus, ScoutIssueKind};
use std::ffi::OsString;
use std::io;
#[cfg(unix)]
fn opaque_relative_path(marker: u16) -> PathBuf {
use std::os::unix::ffi::OsStringExt;
let invalid = if marker & 1 == 0 { 0xff } else { 0xfe };
PathBuf::from(OsString::from_vec(vec![
b'o', b'p', b'a', b'q', b'u', b'e', invalid, b'.', b'r', b's',
]))
}
#[cfg(windows)]
fn opaque_relative_path(marker: u16) -> PathBuf {
use std::os::windows::ffi::OsStringExt;
PathBuf::from(OsString::from_wide(&[
b'o' as u16,
b'p' as u16,
b'a' as u16,
b'q' as u16,
b'u' as u16,
b'e' as u16,
marker,
b'.' as u16,
b'r' as u16,
b's' as u16,
]))
}
#[test]
fn scout_metadata_failure_retains_unavailable_entry_with_walk_stamp() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "main.rs", "fn main() {}");
let expected_size = std::fs::metadata(tmp.path().join("main.rs")).unwrap().len();
let plan = scout_repository_with_metadata(tmp.path(), |_path| {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"injected metadata failure",
))
})
.expect("per-entry metadata failure must degrade the plan, not abort it");
assert_eq!(plan.entries.len(), 1);
assert_eq!(
plan.entries[0].path.normalized_utf8.as_deref(),
Some("main.rs")
);
assert_eq!(plan.entries[0].stamp.size, expected_size);
assert!(matches!(
plan.entries[0].decision,
ScoutDecision::Unavailable {
stage: crate::domain::AccessStage::Metadata,
kind: AccessErrorKind::PermissionDenied
}
));
assert_eq!(plan.coverage, CoverageStatus::Degraded);
assert_eq!(plan.issues.len(), 1);
assert_eq!(plan.issues[0].safe_path.as_deref(), Some("main.rs"));
assert!(matches!(
plan.issues[0].kind,
ScoutIssueKind::DirectoryEntryUnreadable {
kind: AccessErrorKind::PermissionDenied
}
));
}
#[test]
fn walk_failure_retains_bounded_path_issue() {
let tmp = TempDir::new().unwrap();
let failed_path = tmp.path().join("blocked").join("child.rs");
let issue = walk_issue_for_error(
tmp.path(),
Some(&failed_path),
Some(&io::Error::from(io::ErrorKind::PermissionDenied)),
);
let plan = scout_entries_with_io(
Vec::new(),
|_path| -> io::Result<std::fs::Metadata> { unreachable!("no discovered entries") },
|_path, _limit| -> io::Result<Vec<u8>> { unreachable!("no discovered entries") },
vec![issue],
None,
)
.expect("walk issue must remain a bounded degraded scout result");
assert_eq!(plan.coverage, CoverageStatus::Degraded);
assert!(plan.entries.is_empty());
assert_eq!(plan.issues.len(), 1);
assert_eq!(
plan.issues[0].safe_path.as_deref(),
Some("blocked/child.rs")
);
assert!(plan.issues[0].path_id.is_some());
assert!(matches!(
plan.issues[0].kind,
ScoutIssueKind::DirectoryEntryUnreadable {
kind: AccessErrorKind::PermissionDenied
}
));
}
#[test]
fn scout_manifest_is_total_and_deterministically_sorted() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "zeta.rs", "pub fn zeta() {}");
create_file(tmp.path(), "README.md", "# Read me");
create_file(tmp.path(), "model.gguf", "catalog only");
let first = scout_repository(tmp.path()).expect("first scout must succeed");
let second = scout_repository(tmp.path()).expect("second scout must succeed");
assert_eq!(first.coverage, CoverageStatus::Complete);
assert!(first.issues.is_empty());
assert_eq!(first.usage.catalog_entries, 3);
assert_eq!(first.entries, second.entries);
let paths = first
.entries
.iter()
.map(|entry| entry.path.normalized_utf8.as_deref().unwrap())
.collect::<Vec<_>>();
assert_eq!(paths, ["model.gguf", "README.md", "zeta.rs"]);
assert!(matches!(
first.entries[0].decision,
ScoutDecision::MetadataOnly { .. }
));
assert!(matches!(
first.entries[1].decision,
ScoutDecision::Ingest {
targets: IndexTargets::Knowledge
}
));
assert!(matches!(
first.entries[2].decision,
ScoutDecision::Ingest {
targets: IndexTargets::Code
}
));
}
#[test]
fn scout_uses_authoritative_target_routing_and_text_classification() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "guide.rst", "Guide\n=====\n");
create_file(tmp.path(), "settings.toml", "enabled = true\n");
create_file(tmp.path(), "page.html", "<main>hello</main>\n");
let plan = scout_repository(tmp.path()).expect("scout must succeed");
let entry = |path: &str| {
plan.entries
.iter()
.find(|entry| entry.path.normalized_utf8.as_deref() == Some(path))
.unwrap_or_else(|| panic!("missing routed entry {path}"))
};
assert!(matches!(
entry("guide.rst").decision,
ScoutDecision::Ingest {
targets: IndexTargets::Knowledge
}
));
assert!(entry("guide.rst").classification.is_text());
assert!(matches!(
entry("settings.toml").decision,
ScoutDecision::Ingest {
targets: IndexTargets::CodeAndKnowledge
}
));
assert!(entry("settings.toml").classification.is_code());
assert!(entry("settings.toml").classification.is_config);
assert!(matches!(
entry("page.html").decision,
ScoutDecision::Ingest {
targets: IndexTargets::Code
}
));
}
#[test]
fn sensitive_path_is_terminal_before_any_content_probe() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), ".env", "placeholder=true\n");
let probed = std::sync::Mutex::new(Vec::new());
let plan = scout_repository_with_io(
tmp.path(),
|path| std::fs::metadata(path),
|path, _max_bytes| {
probed.lock().unwrap().push(path.to_path_buf());
Ok(Vec::new())
},
)
.expect("sensitive paths must remain catalog-visible");
let entry = plan
.entries
.iter()
.find(|entry| entry.path.normalized_utf8.as_deref() == Some(".env"))
.expect("sensitive path must remain in the scout catalog");
assert!(matches!(
&entry.decision,
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::SensitivePath { rule_id }
} if rule_id == "path.environment-credentials"
));
assert!(
probed.lock().unwrap().is_empty(),
"path policy must run before content I/O"
);
}
fn serial_scout_oracle<F, P>(
discovered: Vec<DiscoveredEntry>,
metadata_reader: F,
mut probe_reader: P,
mut issues: Vec<ScoutIssue>,
cancel: Option<&AtomicBool>,
) -> Result<ScoutPlan>
where
F: Fn(&Path) -> std::io::Result<std::fs::Metadata>,
P: FnMut(&Path, usize) -> std::io::Result<Vec<u8>>,
{
let mut admitted_content_bytes = 0u64;
let limits = DiscoveryLimits::from_env();
let mut catalog_metadata_bytes = 2u64;
let mut entries = Vec::with_capacity(discovered.len());
let mut degraded = !issues.is_empty();
for issue in &issues {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", issue),
limits.max_catalog_metadata_bytes,
)?;
}
let retry_budget = os_response::current_budget();
for mut entry in discovered {
check_scout_cancelled(cancel)?;
let (catalog_path, path_reason) = catalog_path_projection(&entry.relative_os_path);
let public_id = catalog_path.public_id.clone();
let metadata = match metadata_reader(&entry.absolute_path) {
Ok(metadata) => metadata,
Err(error) => {
degraded = true;
let kind = crate::discovery::os_response::stored_kind_of(&error);
let issue = ScoutIssue {
path_id: Some(public_id),
safe_path: catalog_path.normalized_utf8.clone(),
kind: ScoutIssueKind::DirectoryEntryUnreadable { kind },
safe_message: "metadata unavailable".to_string(),
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", &issue),
limits.max_catalog_metadata_bytes,
)?;
issues.push(issue);
let scouted_entry = ScoutedEntry {
path: catalog_path,
absolute_path: Some(entry.absolute_path),
stamp: FileStamp {
size: entry.file_size,
created_hint: None,
modified_hint: None,
platform_id: None,
},
language: entry.language,
classification: entry.classification,
decision: ScoutDecision::Unavailable {
stage: AccessStage::Metadata,
kind,
},
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&(
"entry",
&scouted_entry.path,
scouted_entry.stamp.size,
&scouted_entry.language,
scouted_entry.classification,
&scouted_entry.decision,
),
limits.max_catalog_metadata_bytes,
)?;
entries.push(scouted_entry);
continue;
}
};
entry.file_size = metadata.len();
let decision = if let Some(reason) = path_reason {
ScoutDecision::MetadataOnly { reason }
} else if let Some(rule_id) = crate::knowledge::sensitive_path_rule_at(
&entry.relative_path,
&entry.absolute_path,
) {
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::SensitivePath {
rule_id: rule_id.to_string(),
},
}
} else {
match scout_decision_for_discovered(&entry, entry.file_size, None) {
ScoutDecision::Ingest { .. } => match os_response::retry_transient(
&retry_budget,
Some(&entry.absolute_path),
|| {
probe_reader(
&entry.absolute_path,
crate::domain::index::BINARY_SNIFF_BYTES,
)
.map_err(os_response::AccessFailure::capture)
},
) {
Ok(sample) => scout_decision_for_discovered(
&entry,
entry.file_size,
Some(&sample),
),
Err(cause) => {
degraded = true;
os_response::record_failure(&entry.absolute_path, &cause);
ScoutDecision::Unavailable {
stage: AccessStage::Probe,
kind: cause.stored_kind(),
}
}
},
terminal => terminal,
}
};
check_scout_cancelled(cancel)?;
if matches!(decision, ScoutDecision::Ingest { .. }) {
admitted_content_bytes = admitted_content_bytes.saturating_add(entry.file_size);
}
let scouted_entry = ScoutedEntry {
path: catalog_path,
absolute_path: Some(entry.absolute_path),
stamp: FileStamp {
size: entry.file_size,
created_hint: metadata.created().ok(),
modified_hint: metadata.modified().ok(),
platform_id: None,
},
language: entry.language,
classification: entry.classification,
decision,
};
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&(
"entry",
&scouted_entry.path,
scouted_entry.stamp.size,
&scouted_entry.language,
scouted_entry.classification,
&scouted_entry.decision,
),
limits.max_catalog_metadata_bytes,
)?;
entries.push(scouted_entry);
}
entries.sort_by_cached_key(|entry| {
scout_order_key(
entry.path.normalized_utf8.as_deref(),
Some(entry.path.public_id.as_str()),
)
});
let collision_issues = path_identity_collision_issues(&entries);
if !collision_issues.is_empty() {
degraded = true;
for issue in collision_issues {
catalog_metadata_bytes = account_catalog_metadata_record(
catalog_metadata_bytes,
&("issue", &issue),
limits.max_catalog_metadata_bytes,
)?;
issues.push(issue);
}
}
issues.sort_by_cached_key(|issue| {
scout_order_key(issue.safe_path.as_deref(), issue.path_id.as_deref())
});
Ok(ScoutPlan {
coverage: if degraded {
CoverageStatus::Degraded
} else {
CoverageStatus::Complete
},
usage: ManifestResourceUsage {
catalog_entries: entries.len() as u64,
catalog_metadata_bytes,
admitted_content_bytes,
},
entries,
issues,
})
}
#[test]
fn parallel_scout_matches_the_serial_oracle() {
let tmp = TempDir::new().unwrap();
for i in 0..40 {
create_file(tmp.path(), &format!("src/m{i:02}.rs"), "fn f() {}\n");
}
create_file(tmp.path(), "nested/.ignore", "skipped.rs\n");
create_file(tmp.path(), "nested/skipped.rs", "fn s() {}\n");
create_file(tmp.path(), "nested/deep/.ignore", "*.tmp\n");
create_file(tmp.path(), "nested/deep/kept.py", "x = 1\n");
create_file(tmp.path(), "nested/deep/gone.tmp", "x\n");
create_file(tmp.path(), ".env", "placeholder=true\n");
create_file(tmp.path(), "Cargo.lock", "# lock\n");
create_file(tmp.path(), "README.md", "# readme\n");
create_file(tmp.path(), "no_metadata.rs", "fn n() {}\n");
create_file(tmp.path(), "no_probe.rs", "fn p() {}\n");
std::fs::write(tmp.path().join("blob.rs"), [0u8, 1, 2, 0, 255, 0]).unwrap();
let metadata = |path: &Path| {
if path.ends_with("no_metadata.rs") {
Err(io::Error::from(io::ErrorKind::PermissionDenied))
} else {
std::fs::metadata(path)
}
};
let probe = |path: &Path, max_bytes: usize| {
if path.ends_with("no_probe.rs") {
Err(io::Error::from(io::ErrorKind::NotFound))
} else {
read_binary_probe(path, max_bytes)
}
};
let (discovered, walk_issues) =
discover_all_files_with_exclusions_and_issues_cancellable(
tmp.path(),
&SourceExclusions::default(),
None,
)
.expect("walk");
let parallel = scout_entries_with_io(
discovered.clone(),
metadata,
probe,
walk_issues.clone(),
None,
)
.expect("parallel scout");
let serial = serial_scout_oracle(discovered, metadata, probe, walk_issues, None)
.expect("serial scout");
assert_eq!(format!("{parallel:?}"), format!("{serial:?}"));
assert_eq!(parallel.coverage, CoverageStatus::Degraded);
let decision_of = |name: &str| {
parallel
.entries
.iter()
.find(|entry| entry.path.normalized_utf8.as_deref() == Some(name))
.map(|entry| entry.decision.clone())
};
assert!(matches!(
decision_of("no_probe.rs"),
Some(ScoutDecision::Unavailable {
stage: AccessStage::Probe,
..
})
));
assert!(matches!(
decision_of("no_metadata.rs"),
Some(ScoutDecision::Unavailable {
stage: AccessStage::Metadata,
..
})
));
assert!(decision_of("nested/skipped.rs").is_none());
assert!(decision_of("nested/deep/gone.tmp").is_none());
assert!(matches!(
decision_of("src/m07.rs"),
Some(ScoutDecision::Ingest { .. })
));
}
#[test]
fn credential_file_under_a_credential_directory_root_is_withheld_by_path() {
let parent = TempDir::new().unwrap();
let root = parent.path().join(".aws");
std::fs::create_dir(&root).unwrap();
create_file(&root, "credentials", "placeholder\n");
let plan = scout_repository_with_io(
&root,
|path| std::fs::metadata(path),
|_path, _max_bytes| Ok(Vec::new()),
)
.expect("scout the modeled credential directory");
let entry = plan
.entries
.iter()
.find(|entry| entry.path.normalized_utf8.as_deref() == Some("credentials"))
.expect("the credential file must stay catalog-visible");
assert!(
matches!(
&entry.decision,
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::SensitivePath { rule_id }
} if rule_id == "path.cloud-credential-store"
),
"got {:?}",
entry.decision
);
}
#[test]
fn scout_binary_probe_never_exceeds_binary_sniff_bytes() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "notes.txt", "plain text");
create_file(tmp.path(), "model.gguf", "catalog only");
std::fs::File::create(tmp.path().join("huge.log"))
.unwrap()
.set_len(HARD_SKIP_BYTES + 1)
.unwrap();
let probes = std::sync::Mutex::new(Vec::new());
let plan = scout_repository_with_io(
tmp.path(),
|path| std::fs::metadata(path),
|path, max_bytes| {
probes.lock().unwrap().push((
path.file_name().unwrap().to_string_lossy().into_owned(),
max_bytes,
));
Ok(Vec::new())
},
)
.expect("bounded probing must produce a scout plan");
assert_eq!(plan.entries.len(), 3);
assert_eq!(
probes.into_inner().unwrap(),
vec![(
"notes.txt".to_string(),
crate::domain::index::BINARY_SNIFF_BYTES
)],
"only undecided candidates may be probed, through the exact hard bound"
);
}
#[test]
fn scout_case_fold_pair_is_total_and_failure_is_per_entry() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "lower.rs", "pub fn lower() {}");
create_file(tmp.path(), "upper.rs", "pub fn upper() {}");
let lower_path = tmp.path().join("lower.rs");
let candidates = vec![
DiscoveredEntry {
relative_path: "a.rs".to_string(),
relative_os_path: PathBuf::from("a.rs"),
absolute_path: lower_path.clone(),
file_size: 0,
language: Some(LanguageId::Rust),
classification: FileClassification::for_code_path("a.rs"),
},
DiscoveredEntry {
relative_path: "A.rs".to_string(),
relative_os_path: PathBuf::from("A.rs"),
absolute_path: tmp.path().join("upper.rs"),
file_size: 0,
language: Some(LanguageId::Rust),
classification: FileClassification::for_code_path("A.rs"),
},
];
let complete = scout_entries_with_io(
candidates.clone(),
|path| std::fs::metadata(path),
|_path, _max_bytes| Ok(Vec::new()),
Vec::new(),
None,
)
.expect("case-fold pair must remain scoutable");
let ordered_paths = complete
.entries
.iter()
.map(|entry| entry.path.normalized_utf8.as_deref().unwrap())
.collect::<Vec<_>>();
assert_eq!(ordered_paths, ["A.rs", "a.rs"]);
assert_ne!(
complete.entries[0].path.public_id,
complete.entries[1].path.public_id
);
assert_eq!(complete.coverage, CoverageStatus::Degraded);
assert_eq!(complete.issues.len(), 2);
assert!(
complete
.issues
.iter()
.all(|issue| issue.kind == ScoutIssueKind::PathIdentityCollision)
);
assert_eq!(
complete
.issues
.iter()
.filter_map(|issue| issue.safe_path.as_deref())
.collect::<Vec<_>>(),
["A.rs", "a.rs"]
);
let degraded = scout_entries_with_io(
candidates,
|path| {
if path == lower_path {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"injected per-entry failure",
))
} else {
std::fs::metadata(path)
}
},
|_path, _max_bytes| Ok(Vec::new()),
Vec::new(),
None,
)
.expect("one failed case-fold peer must not abort the other");
assert_eq!(degraded.coverage, CoverageStatus::Degraded);
assert_eq!(degraded.entries.len(), 2);
let unavailable = degraded
.entries
.iter()
.find(|entry| entry.path.normalized_utf8.as_deref() == Some("a.rs"))
.expect("metadata failure must retain the failed peer");
assert!(matches!(
unavailable.decision,
ScoutDecision::Unavailable {
stage: crate::domain::AccessStage::Metadata,
kind: crate::domain::AccessErrorKind::PermissionDenied,
}
));
assert_ne!(
degraded.entries[0].path.public_id,
degraded.entries[1].path.public_id
);
assert_eq!(
degraded
.issues
.iter()
.filter(|issue| matches!(
issue.kind,
ScoutIssueKind::DirectoryEntryUnreadable { .. }
))
.count(),
1
);
assert_eq!(
degraded
.issues
.iter()
.filter(|issue| issue.kind == ScoutIssueKind::PathIdentityCollision)
.count(),
2
);
}
#[test]
fn non_utf8_path_is_opaque_catalog_only_without_lossy_collision() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "first.rs", "pub fn first() {}");
create_file(tmp.path(), "second.rs", "pub fn second() {}");
let first_opaque = opaque_relative_path(0xd800);
let second_opaque = opaque_relative_path(0xd801);
assert_eq!(
first_opaque.to_string_lossy(),
second_opaque.to_string_lossy(),
"fixture must prove two native identities collide under lossy conversion"
);
let candidates = vec![
DiscoveredEntry {
relative_path: first_opaque.to_string_lossy().into_owned(),
relative_os_path: first_opaque,
absolute_path: tmp.path().join("first.rs"),
file_size: 0,
language: Some(LanguageId::Rust),
classification: FileClassification::for_code_path("first.rs"),
},
DiscoveredEntry {
relative_path: second_opaque.to_string_lossy().into_owned(),
relative_os_path: second_opaque,
absolute_path: tmp.path().join("second.rs"),
file_size: 0,
language: Some(LanguageId::Rust),
classification: FileClassification::for_code_path("second.rs"),
},
];
let probe_count = std::sync::atomic::AtomicUsize::new(0);
let plan = scout_entries_with_io(
candidates,
|path| std::fs::metadata(path),
|_path, _max_bytes| {
probe_count.fetch_add(1, Ordering::Relaxed);
Ok(Vec::new())
},
Vec::new(),
None,
)
.expect("opaque paths must remain catalogable");
assert_eq!(plan.entries.len(), 2);
assert_eq!(probe_count.load(Ordering::Relaxed), 0);
assert!(
plan.entries
.iter()
.all(|entry| entry.path.normalized_utf8.is_none())
);
assert_ne!(
plan.entries[0].path.public_id,
plan.entries[1].path.public_id
);
assert!(plan.entries.iter().all(|entry| matches!(
entry.decision,
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::UnsupportedPathEncoding
}
)));
}
#[test]
fn unsafe_or_oversized_path_is_opaque_without_retaining_spelling() {
let tmp = TempDir::new().unwrap();
create_file(tmp.path(), "payload.md", "# safe payload");
let absolute_path = tmp.path().join("payload.md");
let oversized = "x".repeat(MAX_CATALOG_SAFE_PATH_BYTES + 1);
let candidates = vec![
DiscoveredEntry {
relative_path: "unsafe".to_string(),
relative_os_path: PathBuf::from("line\nbreak.md"),
absolute_path: absolute_path.clone(),
file_size: 0,
language: None,
classification: FileClassification::for_code_path("payload.md"),
},
DiscoveredEntry {
relative_path: "oversized".to_string(),
relative_os_path: PathBuf::from(&oversized),
absolute_path,
file_size: 0,
language: None,
classification: FileClassification::for_code_path("payload.md"),
},
];
let probe_count = std::sync::atomic::AtomicUsize::new(0);
let plan = scout_entries_with_io(
candidates,
|path| std::fs::metadata(path),
|_path, _max_bytes| {
probe_count.fetch_add(1, Ordering::Relaxed);
Ok(Vec::new())
},
Vec::new(),
None,
)
.expect("unsafe path metadata must remain catalogable by opaque ID");
assert_eq!(probe_count.load(Ordering::Relaxed), 0);
assert_eq!(plan.entries.len(), 2);
assert!(
plan.entries
.iter()
.all(|entry| entry.path.normalized_utf8.is_none())
);
assert_ne!(
plan.entries[0].path.public_id,
plan.entries[1].path.public_id
);
assert!(plan.entries.iter().any(|entry| matches!(
entry.decision,
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::UnsupportedPathEncoding
}
)));
assert!(plan.entries.iter().any(|entry| matches!(
entry.decision,
ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::PathMetadataTooLarge
}
)));
assert!(plan.entries.iter().all(|entry| {
entry.path.normalized_utf8.as_deref() != Some(oversized.as_str())
}));
}
}
mod git_fast_path_guard {
use super::*;
#[test]
fn ordinary_project_with_git_is_not_forbidden() {
let tmp = TempDir::new().unwrap();
fs::create_dir_all(tmp.path().join(".git")).unwrap();
assert!(
!is_forbidden_root(tmp.path()),
"an ordinary project dir must remain selectable as a git root"
);
}
#[cfg(not(target_os = "windows"))]
#[test]
fn sensitive_unix_root_with_git_is_forbidden() {
assert!(
is_forbidden_root(Path::new("/etc")),
"/etc must be forbidden even if a `.git` is planted there"
);
assert!(crate::paths::is_sensitive_path(Path::new("/etc")));
}
#[cfg(target_os = "windows")]
#[test]
fn sensitive_windows_root_with_git_is_forbidden() {
assert!(is_forbidden_root(Path::new("C:\\Windows")));
assert!(crate::paths::is_sensitive_path(Path::new("C:\\Windows")));
}
}
#[cfg(not(target_os = "windows"))]
mod wsl_drvfs {
use super::*;
#[test]
fn blocks_bare_drive_root() {
assert!(is_wsl_windows_container_path(Path::new("/mnt/c")));
assert!(is_wsl_windows_container_path(Path::new("/mnt/d")));
}
#[test]
fn blocks_users_container() {
assert!(is_wsl_windows_container_path(Path::new("/mnt/c/Users")));
}
#[test]
fn blocks_bare_profile_root() {
assert!(is_wsl_windows_container_path(Path::new(
"/mnt/c/Users/poslj"
)));
}
#[test]
fn blocks_other_drive_profile() {
assert!(is_wsl_windows_container_path(Path::new(
"/mnt/d/Users/alice"
)));
}
#[test]
fn blocks_case_insensitive_users_segment() {
assert!(is_wsl_windows_container_path(Path::new("/mnt/c/users")));
assert!(is_wsl_windows_container_path(Path::new(
"/mnt/c/USERS/poslj"
)));
assert!(is_wsl_windows_container_path(Path::new(
"/mnt/c/UsErS/poslj"
)));
}
#[test]
fn allows_deep_project_under_profile() {
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/Users/poslj/dev/my-lib"
)));
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/Users/poslj/Documents/project"
)));
}
#[test]
fn allows_non_users_mount_project() {
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/code/proj"
)));
}
#[test]
fn allows_users_named_deeper() {
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/code/Users"
)));
}
#[test]
fn allows_non_mnt_paths() {
assert!(!is_wsl_windows_container_path(Path::new("/srv/Users/bob")));
assert!(!is_wsl_windows_container_path(Path::new("/home/robert")));
}
#[test]
fn allows_multichar_second_segment() {
assert!(!is_wsl_windows_container_path(Path::new("/mnt/cc/Users/x")));
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/wsl/Users/x"
)));
}
#[test]
fn allows_lookalike_prefixes() {
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/Users-data/proj"
)));
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/UserStuff/proj"
)));
}
#[test]
fn allows_bare_mnt() {
assert!(!is_wsl_windows_container_path(Path::new("/mnt")));
}
#[test]
fn parent_dir_escape_does_not_misfire() {
assert!(!is_wsl_windows_container_path(Path::new(
"/mnt/c/Users/../code/proj"
)));
assert!(!is_wsl_windows_container_path(Path::new("/mnt/c/../..")));
}
}
mod workspace_root_override {
use super::*;
use std::ffi::{OsStr, OsString};
use std::sync::Mutex;
static ENV_LOCK: Mutex<()> = Mutex::new(());
struct RootEnvGuard {
prev: Option<OsString>,
claude_prev: Option<OsString>,
}
#[allow(unsafe_code)] impl RootEnvGuard {
fn set(value: Option<&str>) -> Self {
Self::set_os(value.map(OsStr::new))
}
fn set_os(value: Option<&OsStr>) -> Self {
let prev = std::env::var_os(WORKSPACE_ROOT_ENV);
let claude_prev = std::env::var_os(CLAUDE_PROJECT_DIR_ENV);
unsafe {
match value {
Some(v) => std::env::set_var(WORKSPACE_ROOT_ENV, v),
None => std::env::remove_var(WORKSPACE_ROOT_ENV),
}
std::env::remove_var(CLAUDE_PROJECT_DIR_ENV);
}
Self { prev, claude_prev }
}
}
#[allow(unsafe_code)] impl Drop for RootEnvGuard {
fn drop(&mut self) {
unsafe {
match &self.prev {
Some(v) => std::env::set_var(WORKSPACE_ROOT_ENV, v),
None => std::env::remove_var(WORKSPACE_ROOT_ENV),
}
match &self.claude_prev {
Some(v) => std::env::set_var(CLAUDE_PROJECT_DIR_ENV, v),
None => std::env::remove_var(CLAUDE_PROJECT_DIR_ENV),
}
}
}
}
#[test]
fn honors_a_real_workspace_directory() {
let _lock = ENV_LOCK.lock().unwrap();
let workspace = TempDir::new().unwrap();
let _guard = RootEnvGuard::set(Some(&workspace.path().display().to_string()));
let resolved = workspace_root_env_override().expect("real dir must resolve");
let resolved = resolved.canonicalize().unwrap_or(resolved);
let expected = workspace.path().canonicalize().unwrap();
assert_eq!(resolved, expected);
}
#[cfg(unix)]
#[test]
fn workspace_override_preserves_trailing_space_path_identity() {
let _lock = ENV_LOCK.lock().unwrap();
let parent = TempDir::new().unwrap();
let plain = parent.path().join("repo");
let spaced = parent.path().join("repo ");
std::fs::create_dir(&plain).unwrap();
std::fs::create_dir(&spaced).unwrap();
let spaced_text = spaced.to_str().expect("UTF-8 fixture");
let _guard = RootEnvGuard::set(Some(spaced_text));
let resolved = workspace_root_env_override().expect("spaced root must resolve");
assert_eq!(resolved, spaced.canonicalize().unwrap());
assert_ne!(resolved, plain.canonicalize().unwrap());
}
#[cfg(unix)]
#[test]
fn non_utf8_workspace_override_never_falls_back_to_cwd() {
use std::os::unix::ffi::OsStringExt;
let _lock = ENV_LOCK.lock().unwrap();
let unset = RootEnvGuard::set(None);
assert!(
find_project_root().is_some(),
"anti-vacuity: the test CWD must provide a fallback workspace"
);
drop(unset);
let parent = TempDir::new().unwrap();
let opaque = parent
.path()
.join(OsString::from_vec(vec![b'r', b'e', b'p', b'o', 0xff]));
std::fs::create_dir(&opaque).unwrap();
let _guard = RootEnvGuard::set_os(Some(opaque.as_os_str()));
assert!(workspace_root_env_override().is_none());
assert!(workspace_root_env_is_authoritative());
assert!(
find_project_root().is_none(),
"an explicit non-UTF-8 root must refuse instead of selecting the CWD repository"
);
}
#[test]
fn ignores_empty_and_missing_paths() {
let _lock = ENV_LOCK.lock().unwrap();
let _empty = RootEnvGuard::set(Some(" "));
assert!(workspace_root_env_override().is_none());
let _missing = RootEnvGuard::set(Some("/no/such/symforge/workspace/xyzzy"));
assert!(workspace_root_env_override().is_none());
let _unset = RootEnvGuard::set(None);
assert!(workspace_root_env_override().is_none());
}
#[allow(unsafe_code)] struct ClaudeDirGuard {
prev: Option<OsString>,
_permit: ClaudeProjectDirTestPermit,
}
#[allow(unsafe_code)] impl ClaudeDirGuard {
fn set(value: Option<&str>) -> Self {
let permit = ClaudeProjectDirTestPermit::enter();
let prev = std::env::var_os(CLAUDE_PROJECT_DIR_ENV);
unsafe {
match value {
Some(v) => std::env::set_var(CLAUDE_PROJECT_DIR_ENV, v),
None => std::env::remove_var(CLAUDE_PROJECT_DIR_ENV),
}
}
Self {
prev,
_permit: permit,
}
}
}
#[allow(unsafe_code)] impl Drop for ClaudeDirGuard {
fn drop(&mut self) {
unsafe {
match &self.prev {
Some(v) => std::env::set_var(CLAUDE_PROJECT_DIR_ENV, v),
None => std::env::remove_var(CLAUDE_PROJECT_DIR_ENV),
}
}
}
}
fn canonical(dir: &TempDir) -> PathBuf {
dir.path().canonicalize().unwrap()
}
fn repo_with_src() -> (TempDir, PathBuf) {
let tmp = TempDir::new().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(repo.join(".git")).unwrap();
std::fs::create_dir_all(repo.join("src")).unwrap();
(tmp, repo)
}
fn cwd_only_root() -> Option<PathBuf> {
let _ws = RootEnvGuard::set(None);
find_project_root()
}
#[test]
fn claude_project_dir_starts_the_git_walk() {
let _lock = ENV_LOCK.lock().unwrap();
let (_tmp, repo) = repo_with_src();
let _ws = RootEnvGuard::set(None);
let _claude = ClaudeDirGuard::set(Some(&repo.join("src").display().to_string()));
assert_eq!(
find_project_root(),
Some(repo.canonicalize().unwrap()),
"a session opened in repo/src must still bind repo, not repo/src"
);
assert!(
!workspace_root_env_is_authoritative(),
"CLAUDE_PROJECT_DIR must not become an authoritative override"
);
}
#[test]
fn claude_project_dir_without_a_git_root_binds_itself() {
let _lock = ENV_LOCK.lock().unwrap();
let project = TempDir::new().unwrap();
let _ws = RootEnvGuard::set(None);
let _claude = ClaudeDirGuard::set(Some(&project.path().display().to_string()));
assert_eq!(find_project_root(), Some(canonical(&project)));
}
#[test]
fn workspace_env_outranks_claude_project_dir() {
let _lock = ENV_LOCK.lock().unwrap();
let explicit = TempDir::new().unwrap();
let (_tmp, repo) = repo_with_src();
let _ws = RootEnvGuard::set(Some(&explicit.path().display().to_string()));
let _claude = ClaudeDirGuard::set(Some(&repo.display().to_string()));
assert_eq!(find_project_root(), Some(canonical(&explicit)));
}
#[test]
fn unexpanded_workspace_placeholder_falls_through_to_claude_project_dir() {
let _lock = ENV_LOCK.lock().unwrap();
let (_tmp, repo) = repo_with_src();
let _ws = RootEnvGuard::set(Some("${workspaceFolder}"));
let _claude = ClaudeDirGuard::set(Some(&repo.join("src").display().to_string()));
assert_eq!(find_project_root(), Some(repo.canonicalize().unwrap()));
}
#[test]
fn unusable_claude_project_dir_falls_through_to_the_cwd_walk() {
let _lock = ENV_LOCK.lock().unwrap();
let baseline = cwd_only_root();
assert!(
baseline.is_some(),
"anti-vacuity: the test CWD must resolve"
);
let home = dirs::home_dir().expect("a home directory to refuse");
for unusable in [
"/no/such/symforge/claude/dir".to_string(),
" ".to_string(),
home.display().to_string(),
] {
let _ws = RootEnvGuard::set(None);
let _claude = ClaudeDirGuard::set(Some(&unusable));
assert_eq!(
find_project_root(),
baseline,
"CLAUDE_PROJECT_DIR={unusable:?}"
);
}
}
#[cfg(unix)]
#[allow(unsafe_code)] #[test]
fn non_utf8_claude_project_dir_falls_through_instead_of_refusing() {
use std::os::unix::ffi::OsStringExt;
let _lock = ENV_LOCK.lock().unwrap();
let baseline = cwd_only_root();
assert!(
baseline.is_some(),
"anti-vacuity: the test CWD must resolve"
);
let _ws = RootEnvGuard::set(None);
let opaque = OsString::from_vec(vec![b'/', b't', b'm', b'p', b'/', 0xff]);
let _permit = ClaudeProjectDirTestPermit::enter();
unsafe {
std::env::set_var(CLAUDE_PROJECT_DIR_ENV, &opaque);
}
let found = find_project_root();
unsafe {
std::env::remove_var(CLAUDE_PROJECT_DIR_ENV);
}
assert_eq!(
found, baseline,
"a hint that cannot be read must fall through, unlike the explicit override"
);
}
#[allow(unsafe_code)] #[test]
fn inherited_claude_project_dir_does_not_bind_or_mutate_canary() {
let _lock = ENV_LOCK.lock().unwrap();
let canary = TempDir::new().unwrap();
let marker = canary.path().join("CANARY_MARKER");
std::fs::write(&marker, b"keep").unwrap();
let entries_before: Vec<_> = std::fs::read_dir(canary.path())
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
let _ws = RootEnvGuard::set(None);
unsafe {
std::env::set_var(CLAUDE_PROJECT_DIR_ENV, canary.path().as_os_str());
}
let resolved = find_project_root();
let canary_canon = canary.path().canonicalize().unwrap();
let resolved_canon = resolved
.as_ref()
.map(|p| p.canonicalize().unwrap_or_else(|_| p.clone()));
unsafe {
std::env::remove_var(CLAUDE_PROJECT_DIR_ENV);
}
assert_ne!(
resolved_canon,
Some(canary_canon),
"inherited CLAUDE_PROJECT_DIR must not bind the canary as project root; got {resolved:?}"
);
assert!(
marker.exists() && std::fs::read(&marker).unwrap() == b"keep",
"canary marker must be untouched"
);
assert!(
!canary.path().join(".symforge").exists(),
"canary must not receive a .symforge directory from the suite"
);
let entries_after: Vec<_> = std::fs::read_dir(canary.path())
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(
entries_before, entries_after,
"canary directory entries must be unchanged"
);
}
#[test]
fn cwd_only_discovery_ignores_claude_project_dir() {
let _lock = ENV_LOCK.lock().unwrap();
let baseline = cwd_only_root();
let (_tmp, repo) = repo_with_src();
let _ws = RootEnvGuard::set(None);
let _claude = ClaudeDirGuard::set(Some(&repo.display().to_string()));
assert_eq!(
find_project_root_from_cwd(),
baseline,
"init's Claude Desktop registration must not freeze a session's project"
);
assert_ne!(
find_project_root(),
baseline,
"anti-vacuity: the env is honored elsewhere"
);
}
#[test]
fn rejects_a_forbidden_home_dir_override() {
let _lock = ENV_LOCK.lock().unwrap();
let Some(home) = home_dir() else {
return; };
let _guard = RootEnvGuard::set(Some(&home.display().to_string()));
assert!(
workspace_root_env_override().is_none(),
"the forbidden home dir must be rejected by the shared trust-boundary guard"
);
}
}
mod roots_workspace_resolution {
use super::*;
fn file_uri(path: &std::path::Path) -> String {
let s = path.display().to_string().replace('\\', "/");
if s.starts_with('/') {
format!("file://{s}")
} else {
format!("file:///{s}")
}
}
#[test]
fn client_root_wins_over_forbidden_cwd_and_no_env() {
let workspace = TempDir::new().unwrap();
let uri = file_uri(workspace.path());
let resolved = resolve_workspace_root(None, std::slice::from_ref(&uri), None)
.expect("a valid client root must resolve the workspace with no env and no CWD");
let resolved = resolved.canonicalize().unwrap_or(resolved);
let expected = workspace.path().canonicalize().unwrap();
assert_eq!(
resolved, expected,
"client root must drive workspace resolution when CWD is unusable"
);
}
#[test]
fn env_override_beats_client_roots() {
let env_ws = TempDir::new().unwrap();
let client_ws = TempDir::new().unwrap();
let client_uri = file_uri(client_ws.path());
let resolved = resolve_workspace_root(
Some(env_ws.path().to_path_buf()),
std::slice::from_ref(&client_uri),
None,
)
.expect("env override must resolve");
assert_eq!(
resolved,
env_ws.path().canonicalize().unwrap(),
"SYMFORGE_WORKSPACE_ROOT must take precedence over client roots"
);
}
#[test]
fn cwd_used_only_when_env_and_roots_absent() {
let cwd_ws = TempDir::new().unwrap();
let resolved = resolve_workspace_root(None, &[], Some(cwd_ws.path().to_path_buf()))
.expect("CWD fallback must resolve");
assert_eq!(resolved, cwd_ws.path().canonicalize().unwrap());
}
#[test]
fn forbidden_client_root_is_skipped_not_fatal() {
let Some(home) = home_dir() else {
return; };
let valid = TempDir::new().unwrap();
let roots = vec![file_uri(&home), file_uri(valid.path())];
let resolved = resolve_workspace_root(None, &roots, None)
.expect("a valid later root must resolve after a forbidden one is skipped");
let resolved = resolved.canonicalize().unwrap_or(resolved);
let expected = valid.path().canonicalize().unwrap();
assert_eq!(resolved, expected);
}
#[test]
fn all_forbidden_roots_yield_none_when_no_other_source() {
let Some(home) = home_dir() else {
return;
};
let roots = vec![file_uri(&home)];
assert!(
resolve_workspace_root(None, &roots, None).is_none(),
"no env, all-forbidden roots, no CWD -> no workspace (must not widen trust)"
);
}
#[test]
fn automatic_protected_roots_stay_unbound_before_source_or_project_state_io() {
let tmp = TempDir::new().unwrap();
let protected = tmp.path().join("System32");
std::fs::create_dir(&protected).unwrap();
let protected_uri = file_uri(&protected);
assert!(
resolve_workspace_root(Some(protected.clone()), &[], None).is_none(),
"workspace environment candidates must pass the shared root gate"
);
assert!(
resolve_workspace_root(None, &[protected_uri], None).is_none(),
"MCP client candidates must pass the shared root gate"
);
assert!(
resolve_workspace_root(None, &[], Some(protected.clone())).is_none(),
"launch-CWD candidates must pass the shared root gate"
);
assert!(
!protected.join(".symforge").exists(),
"rejected automatic roots must not receive per-project state"
);
}
#[cfg(windows)]
#[test]
fn network_root_is_refused_before_any_filesystem_call() {
for raw in [
r"\\nonexistent-host.invalid\share",
"//nonexistent-host.invalid/share",
r"\\?\UNC\nonexistent-host.invalid\share",
r"\\.\UNC\nonexistent-host.invalid\share",
"//./UNC/nonexistent-host.invalid/share",
r"\??\UNC\nonexistent-host.invalid\share",
r"\\?\GLOBALROOT\Device\Mup\nonexistent-host.invalid\share",
"//?/GLOBALROOT/Device/Mup/nonexistent-host.invalid/share",
r"\\?\Global\UNC\nonexistent-host.invalid\share",
] {
let started = std::time::Instant::now();
let resolution = resolve_root_candidate_with(
Path::new(raw),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
|_| panic!("filesystem probe for {raw}"),
|_| panic!("canonicalize for {raw}"),
);
assert!(
matches!(
resolution,
RootResolution::Unbound {
reason: UnboundReason::Refused(RootRefusalReason::NetworkPath),
..
}
),
"{raw}: {resolution:?}"
);
assert!(
started.elapsed() < std::time::Duration::from_millis(250),
"{raw}"
);
}
let local = TempDir::new().unwrap();
let verbatim = format!(r"\\?\{}", local.path().display());
let accepted = resolve_root_candidate(
Path::new(&verbatim),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
);
assert!(matches!(accepted, RootResolution::Bound(_)), "{accepted:?}");
}
#[cfg(windows)]
#[test]
fn root_canonicalizing_to_network_path_is_refused() {
let local = TempDir::new().unwrap();
let resolution = resolve_root_candidate_with(
local.path(),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: false,
},
|_| true,
|_| {
Ok(PathBuf::from(
r"\\?\UNC\nonexistent-host.invalid\share\repo",
))
},
);
assert!(
matches!(
resolution,
RootResolution::Unbound {
reason: UnboundReason::Refused(RootRefusalReason::NetworkPath),
..
}
),
"{resolution:?}"
);
}
#[test]
fn device_or_uncanonicalizable_root_remains_nonindexable_with_override() {
let override_mode = RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
};
let (device_namespace, expected) = if cfg!(windows) {
(PathBuf::from(r"\\.\NUL"), RootRefusalReason::NetworkPath)
} else {
(
PathBuf::from("/dev/null"),
RootRefusalReason::DeviceOrSpecialNamespace,
)
};
let device = resolve_root_candidate(
&device_namespace,
RootCandidateSource::ExplicitIndexFolder,
override_mode,
);
assert!(
matches!(
&device,
RootResolution::Unbound {
reason: UnboundReason::Refused(reason),
..
} if *reason == expected
),
"explicit protected-root authority must never authorize a device namespace: {device:?}"
);
let ordinary = TempDir::new().unwrap();
let uncanonicalizable = resolve_root_candidate_with(
ordinary.path(),
RootCandidateSource::ExplicitIndexFolder,
override_mode,
|_| true,
|_| Err(std::io::Error::other("injected canonicalization failure")),
);
assert!(
matches!(
uncanonicalizable,
RootResolution::Unbound {
reason: UnboundReason::Refused(RootRefusalReason::CanonicalizationFailed),
..
}
),
"override must not turn an unknown canonical identity into a binding: {uncanonicalizable:?}"
);
}
#[cfg(unix)]
#[test]
fn non_utf8_canonical_root_is_never_indexable() {
use std::os::unix::ffi::OsStringExt;
let canonical_root = PathBuf::from(std::ffi::OsString::from_vec(vec![
b'/', b'w', b'o', b'r', b'k', b'/', b'a', 0xff, b'b',
]));
let resolution = resolve_root_candidate_with(
Path::new("utf8-alias"),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
|_| true,
|_| Ok(canonical_root.clone()),
);
assert!(matches!(
resolution,
RootResolution::Unbound {
reason: UnboundReason::Refused(RootRefusalReason::UnsupportedPathEncoding),
..
}
));
}
#[cfg(windows)]
#[test]
fn windows_verbatim_and_simplified_roots_share_project_id() {
let root = TempDir::new().unwrap();
let verbatim = root.path().canonicalize().unwrap();
let simplified = dunce::canonicalize(root.path()).unwrap();
assert_eq!(
project_id_for_canonical_root(&verbatim),
project_id_for_canonical_root(&simplified),
"equivalent Windows canonical path spellings must select one user-local state identity"
);
}
#[test]
fn explicit_protected_root_uses_user_local_then_memory_only() {
let tmp = TempDir::new().unwrap();
let protected = tmp.path().join("System32");
std::fs::create_dir(&protected).unwrap();
let RootResolution::Bound(binding) = resolve_root_candidate(
&protected,
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: true,
},
) else {
panic!("the modeled protected root should bind with direct authority");
};
let user_state = tmp.path().join("private-user-state");
let placement =
resolve_state_placement_with(&binding, Some(user_state.clone()), |candidate| {
assert_eq!(candidate, user_state);
std::fs::create_dir_all(candidate)
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))
});
match placement {
crate::domain::StatePlacement::UserLocal {
directory,
root_id,
reason: crate::domain::UserLocalPlacementReason::ExplicitProtected,
} => {
assert_eq!(directory.as_path(), user_state);
assert_eq!(root_id, binding.root_id);
}
other => panic!("expected user-local placement, got {other:?}"),
}
assert!(!protected.join(".symforge").exists());
let placement = resolve_state_placement_with(&binding, Some(user_state), |_| {
Err(AccessErrorKind::PermissionDenied)
});
match placement {
crate::domain::StatePlacement::MemoryOnly { failures } => {
assert_eq!(failures.len(), 1);
assert_eq!(
failures[0].location,
crate::domain::StateLocationKind::UserLocal
);
assert_eq!(failures[0].safe_reason, AccessErrorKind::PermissionDenied);
}
other => panic!("expected memory-only placement, got {other:?}"),
}
assert!(!protected.join(".symforge").exists());
}
#[test]
fn readable_unwritable_project_relocates_state_without_retargeting_source() {
let project = TempDir::new().unwrap();
std::fs::write(project.path().join("lib.rs"), "pub fn readable() {}\n").unwrap();
let RootResolution::Bound(binding) = resolve_root_candidate(
project.path(),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: false,
},
) else {
panic!("ordinary readable project should bind");
};
let bound_source = binding.canonical_root.clone();
let bound_id = binding.root_id.clone();
let project_state = bound_source.join(".symforge");
let user_base = TempDir::new().unwrap();
let user_state = user_base.path().join("projects").join(&bound_id.0);
let mut attempts = Vec::new();
let placement =
resolve_state_placement_with(&binding, Some(user_state.clone()), |candidate| {
attempts.push(candidate.to_path_buf());
if candidate == project_state {
Err(AccessErrorKind::PermissionDenied)
} else {
std::fs::create_dir_all(candidate)
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))
}
});
assert_eq!(attempts, vec![project_state.clone(), user_state.clone()]);
assert_eq!(binding.canonical_root, bound_source);
assert_eq!(binding.root_id, bound_id);
assert!(std::fs::read_to_string(bound_source.join("lib.rs")).is_ok());
assert!(!project_state.exists());
match placement {
StatePlacement::UserLocal {
directory,
root_id,
reason:
UserLocalPlacementReason::ProjectLocalUnavailable {
safe_reason: AccessErrorKind::PermissionDenied,
},
} => {
assert_eq!(directory.as_path(), user_state);
assert_eq!(root_id, binding.root_id);
}
other => panic!("expected user-local fallback, got {other:?}"),
}
}
#[test]
fn project_symforge_symlink_or_reparse_point_uses_global_without_following() {
let project = TempDir::new().unwrap();
std::fs::write(project.path().join("lib.rs"), "pub fn readable() {}\n").unwrap();
let RootResolution::Bound(binding) = resolve_root_candidate(
project.path(),
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: false,
},
) else {
panic!("ordinary readable project should bind");
};
let outside = TempDir::new().unwrap();
let sentinel = outside.path().join("sentinel");
std::fs::write(&sentinel, b"outside state remains untouched\n").unwrap();
let project_state = binding.canonical_root.join(".symforge");
#[cfg(unix)]
let link_result = std::os::unix::fs::symlink(outside.path(), &project_state);
#[cfg(windows)]
let link_result = std::os::windows::fs::symlink_dir(outside.path(), &project_state);
let linked = if let Err(error) = link_result {
#[cfg(windows)]
{
assert_eq!(error.raw_os_error(), Some(1314));
std::fs::write(&project_state, b"unsafe state entry\n").unwrap();
false
}
#[cfg(not(windows))]
panic!("test host must support directory symlinks: {error}");
} else {
true
};
let user_base = TempDir::new().unwrap();
let user_state = user_base.path().join("projects").join(&binding.root_id.0);
let placement =
resolve_state_placement_with(&binding, Some(user_state.clone()), |candidate| {
std::fs::create_dir_all(candidate)
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))?;
std::fs::write(candidate.join("prepared"), b"prepared\n")
.map_err(|error| crate::discovery::os_response::stored_kind_of(&error))
});
assert_eq!(
std::fs::read(&sentinel).unwrap(),
b"outside state remains untouched\n"
);
assert!(!outside.path().join("prepared").exists());
if !linked {
assert!(crate::paths::state_directory_entry_type_is_unsafe(
true, false, true
));
assert_eq!(
std::fs::read(&project_state).unwrap(),
b"unsafe state entry\n"
);
}
assert_eq!(
std::fs::read(user_state.join("prepared")).unwrap(),
b"prepared\n"
);
match placement {
StatePlacement::UserLocal {
directory,
root_id,
reason:
UserLocalPlacementReason::ProjectLocalUnavailable {
safe_reason: AccessErrorKind::InvalidData,
},
} => {
assert_eq!(directory.as_path(), user_state);
assert_eq!(root_id, binding.root_id);
}
other => panic!("expected user-local fallback, got {other:?}"),
}
}
#[test]
fn root_state_key_coalesces_aliases_and_isolates_repos_and_worktrees() {
fn bind(root: &Path) -> RootBinding {
let RootResolution::Bound(binding) = resolve_root_candidate(
root,
RootCandidateSource::ExplicitIndexFolder,
RootRequestMode::ExplicitIndexFolder {
allow_protected_root: false,
},
) else {
panic!("ordinary root should bind: {}", root.display());
};
binding
}
let parent_a = TempDir::new().unwrap();
let parent_b = TempDir::new().unwrap();
let repo_a = parent_a.path().join("same-name");
let repo_b = parent_b.path().join("same-name");
let linked_worktree = parent_a.path().join("linked-worktree");
for root in [&repo_a, &repo_b, &linked_worktree] {
std::fs::create_dir(root).unwrap();
}
std::fs::write(
linked_worktree.join(".git"),
format!("gitdir: {}/.git/worktrees/linked\n", repo_a.display()),
)
.unwrap();
let primary = bind(&repo_a);
let alias = bind(&repo_a.join("."));
let same_basename_other_repo = bind(&repo_b);
let worktree = bind(&linked_worktree);
assert_eq!(primary.canonical_root, alias.canonical_root);
assert_eq!(primary.root_id, alias.root_id);
assert_ne!(primary.root_id, same_basename_other_repo.root_id);
assert_ne!(primary.root_id, worktree.root_id);
assert!(primary.root_id.0.starts_with("project-v1-"));
assert_eq!(primary.root_id.0.len(), "project-v1-".len() + 64);
assert!(!primary.root_id.0.contains("same-name"));
}
#[test]
fn parse_root_uri_handles_file_scheme_and_raw_path() {
let ws = TempDir::new().unwrap();
let native = ws.path().to_path_buf();
let from_uri = parse_root_uri(&file_uri(ws.path())).expect("file:// URI must parse");
assert_eq!(
from_uri.canonicalize().unwrap(),
native.canonicalize().unwrap()
);
let raw = native.display().to_string();
assert_eq!(parse_root_uri(&raw), Some(native.clone()));
assert_eq!(parse_root_uri(" "), None);
}
#[test]
fn parse_root_uri_rejects_non_file_scheme() {
assert_eq!(parse_root_uri("http://example.com/repo"), None);
assert_eq!(parse_root_uri("https://example.com/repo"), None);
}
#[test]
fn parse_root_uri_percent_decodes() {
let decoded = parse_root_uri("file:///tmp/a%20b/c%2Bd").expect("must parse");
assert_eq!(
decoded.to_string_lossy().replace('\\', "/"),
"/tmp/a b/c+d",
"percent escapes must decode to literal bytes"
);
}
}
#[test]
fn outbound_link_scout_single_path_refuses_link_and_accepts_regular_file() {
const SECRET: &str = "OUTBOUND_LINK_SECRET_7f3a9c2e";
const PLAIN: &str = "OUTBOUND_LINK_PLAIN_ok";
let dir = TempDir::new().unwrap();
create_file(
dir.path(),
"plain.rs",
&format!("fn plain() {{ let _ = \"{PLAIN}\"; }}\n"),
);
let regular = scout_single_path("plain.rs", &dir.path().join("plain.rs")).expect("plain");
assert!(
matches!(regular.decision, ScoutDecision::Ingest { .. }),
"ordinary file must still be scouted, got {:?}",
regular.decision
);
let outside = TempDir::new().unwrap();
std::fs::write(outside.path().join("secret.txt"), format!("{SECRET}\n")).unwrap();
#[cfg(unix)]
{
let link = dir.path().join("outbound_link.txt");
std::os::unix::fs::symlink(outside.path().join("secret.txt"), &link).unwrap();
let entry = scout_single_path("outbound_link.txt", &link)
.expect("symlink scout stays catalogued");
assert!(
matches!(entry.decision, ScoutDecision::Unavailable { .. }),
"a file symlink must not be ingested, got {:?}",
entry.decision
);
}
#[cfg(windows)]
{
let junction = dir.path().join("junc");
let status = crate::process_util::hidden_command("cmd")
.args(["/C", "mklink", "/J"])
.arg(&junction)
.arg(outside.path())
.output()
.expect("mklink runs");
assert!(
status.status.success(),
"mklink /J failed: {}",
String::from_utf8_lossy(&status.stderr)
);
let entry = scout_single_path("junc", &junction).expect("junction scout");
assert!(
!matches!(entry.decision, ScoutDecision::Ingest { .. }),
"a junction must not be ingested, got {:?}",
entry.decision
);
}
}
}