use parking_lot::Mutex;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant, SystemTime};
use notify::{EventKind, RecommendedWatcher as NotifyRecommendedWatcher, RecursiveMode};
use notify_debouncer_full::{
DebounceEventResult, DebouncedEvent, Debouncer, NoCache, new_debouncer_opt,
};
use tracing::{debug, error, trace, warn};
use crate::discovery::classify_admission;
use crate::domain::index::{AdmissionTier, SkippedFile};
use crate::domain::{FileClassification, LanguageId};
use crate::live_index::store::{IndexedFile, SharedIndex};
use crate::{hash, parsing};
pub use crate::watcher_state::{WatcherInfo, WatcherState};
pub struct BurstTracker {
pub event_count: u32,
pub window_start: Instant,
pub last_event_at: Instant,
pub extended: bool,
}
impl BurstTracker {
const BURST_THRESHOLD: u32 = 3;
const BASE_MS: u64 = 200;
const BURST_MS: u64 = 500;
const QUIET_SECS: u64 = 5;
pub fn new() -> Self {
let now = Instant::now();
BurstTracker {
event_count: 0,
window_start: now,
last_event_at: now,
extended: false,
}
}
pub fn update(&mut self, now: Instant) {
let window_duration = now.duration_since(self.window_start);
if window_duration > Duration::from_millis(Self::BASE_MS) {
self.window_start = now;
self.event_count = 1;
self.extended = false;
} else {
self.event_count += 1;
if self.event_count > Self::BURST_THRESHOLD {
self.extended = true;
}
}
self.last_event_at = now;
}
pub fn effective_debounce_ms(&self) -> u64 {
let since_last = self.last_event_at.elapsed();
if since_last > Duration::from_secs(Self::QUIET_SECS) {
return Self::BASE_MS;
}
if self.extended {
Self::BURST_MS
} else {
Self::BASE_MS
}
}
}
impl Default for BurstTracker {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ReindexResult {
HashSkip,
Reindexed,
Skipped,
NotFound,
Removed,
ReadError(String),
}
#[must_use]
pub(crate) enum FreshenResult {
Fresh,
StaleReindexed,
StaleRemoved,
GenerationMismatch,
}
pub(crate) fn normalize_event_path(abs_path: &Path, repo_root: &Path) -> Option<String> {
let raw_path = abs_path.to_string_lossy();
let stripped_raw: &str = if let Some(stripped) = raw_path.strip_prefix(r"\\?\") {
stripped
} else {
raw_path.as_ref()
};
let clean_abs = Path::new(stripped_raw);
let relative = clean_abs.strip_prefix(repo_root).or_else(|_| {
let root_raw = repo_root.to_string_lossy();
let stripped_root: &str = if let Some(stripped) = root_raw.strip_prefix(r"\\?\") {
stripped
} else {
return clean_abs.strip_prefix(repo_root);
};
clean_abs.strip_prefix(Path::new(stripped_root))
});
relative
.ok()
.map(|p| p.to_string_lossy().replace('\\', "/"))
}
pub(crate) fn supported_language(path: &Path) -> Option<LanguageId> {
let ext = path.extension()?.to_str()?;
LanguageId::from_extension(ext)
}
pub(crate) fn is_relevant_event(event: &DebouncedEvent) -> bool {
matches!(
event.kind,
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
)
}
pub(crate) fn maybe_reindex(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: LanguageId,
expected_gen: u64,
) -> ReindexResult {
match read_and_index(
relative_path,
abs_path,
shared,
language.clone(),
expected_gen,
) {
ReindexResult::NotFound => {}
other => return other,
}
let delays_ms = [50u64, 200, 500];
for delay_ms in delays_ms {
std::thread::sleep(std::time::Duration::from_millis(delay_ms));
match read_and_index(
relative_path,
abs_path,
shared,
language.clone(),
expected_gen,
) {
ReindexResult::NotFound => continue,
other => return other,
}
}
if shared.remove_file_at_generation(relative_path, expected_gen) {
warn!("watcher: file not found after retries, removed from index: {relative_path}");
} else {
trace!(
"watcher: file not found after retries, stale generation rejected remove: {relative_path}"
);
}
ReindexResult::Removed
}
fn project_root_from_paths(abs_path: &Path, relative_path: &str) -> Option<PathBuf> {
let depth = Path::new(relative_path).components().count();
abs_path.ancestors().nth(depth).map(|p| p.to_path_buf())
}
fn read_and_index(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: LanguageId,
expected_gen: u64,
) -> ReindexResult {
let metadata = match std::fs::metadata(abs_path) {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return ReindexResult::NotFound,
Err(e) => {
warn!("watcher: failed to stat {relative_path}: {e}");
return ReindexResult::ReadError(e.to_string());
}
};
let file_size = metadata.len();
let mtime_secs = metadata
.modified()
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
let bytes = match std::fs::read(abs_path) {
Ok(b) => b,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return ReindexResult::NotFound,
Err(e) => {
warn!("watcher: failed to read {relative_path}: {e}");
return ReindexResult::ReadError(e.to_string());
}
};
let new_hash = hash::digest_hex(&bytes);
{
let index = shared.read();
if let Some(existing) = index.get_file(relative_path)
&& existing.content_hash == new_hash
{
let needs_mtime_touch = existing.mtime_secs != mtime_secs && mtime_secs != 0;
drop(index); if needs_mtime_touch {
shared.touch_mtime_at_generation(relative_path, mtime_secs, expected_gen);
}
debug!("watcher: hash-skip {relative_path}");
return ReindexResult::HashSkip;
}
}
if crate::discovery::path_has_hidden_component(relative_path) {
drop(bytes);
let removed = shared.remove_file_at_generation(relative_path, expected_gen);
let cleared = shared.clear_skipped_at_generation(relative_path, expected_gen);
if removed || cleared {
debug!("watcher: scan-policy hidden-path eviction {relative_path}");
} else {
trace!("watcher: scan-policy hidden-path skip (no prior record) {relative_path}");
}
return ReindexResult::Skipped;
}
let decision = classify_admission(abs_path, file_size, Some(&bytes));
match decision.tier {
AdmissionTier::HardSkip | AdmissionTier::MetadataOnly => {
let extension = abs_path
.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_string());
drop(bytes);
let sf = SkippedFile {
path: relative_path.to_string(),
size: file_size,
extension,
decision,
};
if shared.demote_to_skipped_at_generation(relative_path, sf, expected_gen) {
debug!("watcher: admission-skip {relative_path} (Tier 2/3)");
} else {
trace!("watcher: admission-skip stale generation rejected: {relative_path}");
}
return ReindexResult::Skipped;
}
AdmissionTier::Normal => {}
}
if let Some(root) = project_root_from_paths(abs_path, relative_path)
&& crate::discovery::is_untracked_generated_output_path(&root, relative_path)
{
let extension = abs_path
.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_string());
drop(bytes);
let sf = SkippedFile {
path: relative_path.to_string(),
size: file_size,
extension,
decision: crate::domain::index::AdmissionDecision::skip(
AdmissionTier::MetadataOnly,
crate::domain::index::SkipReason::GeneratedOutput,
),
};
if shared.demote_to_skipped_at_generation(relative_path, sf, expected_gen) {
debug!("watcher: generated-output demotion {relative_path}");
} else {
trace!("watcher: generated-output demotion stale generation rejected: {relative_path}");
}
return ReindexResult::Skipped;
}
let result = parsing::process_file_with_classification(
relative_path,
&bytes,
language,
FileClassification::for_code_path(relative_path),
);
let indexed = IndexedFile::from_parse_result(result, bytes).with_mtime(mtime_secs);
shared.update_file_at_generation(relative_path, indexed, expected_gen);
shared.clear_skipped_at_generation(relative_path, expected_gen);
debug!("watcher: re-indexed {relative_path}");
ReindexResult::Reindexed
}
pub(crate) fn freshen_file_if_stale(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
) -> FreshenResult {
if shared.current_project_generation() != expected_gen {
let _ = shared.remove_file_at_generation(relative_path, expected_gen);
return FreshenResult::GenerationMismatch;
}
let disk_mtime = std::fs::metadata(abs_path)
.and_then(|m| m.modified())
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
let indexed_mtime = {
let index = shared.read();
index
.get_file(relative_path)
.map(|f| f.mtime_secs)
.unwrap_or(u64::MAX)
};
if disk_mtime == 0 && indexed_mtime == 0 {
return FreshenResult::Fresh; }
if disk_mtime != 0 && disk_mtime == indexed_mtime {
return FreshenResult::Fresh; }
let language = match supported_language(abs_path) {
Some(l) => l,
None => return FreshenResult::Fresh,
};
debug!("freshness guard: stale file detected, re-indexing {relative_path}");
let result = maybe_reindex(relative_path, abs_path, shared, language, expected_gen);
if shared.current_project_generation() != expected_gen {
let _ = shared.remove_file_at_generation(relative_path, expected_gen);
return FreshenResult::GenerationMismatch;
}
match result {
ReindexResult::HashSkip | ReindexResult::Reindexed | ReindexResult::ReadError(_) => {
FreshenResult::StaleReindexed
}
ReindexResult::Skipped => FreshenResult::StaleReindexed,
ReindexResult::NotFound | ReindexResult::Removed => FreshenResult::StaleRemoved,
}
}
fn freshen_file_if_stale_at_generation(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
) -> bool {
matches!(
freshen_file_if_stale(relative_path, abs_path, shared, expected_gen),
FreshenResult::StaleReindexed
| FreshenResult::StaleRemoved
| FreshenResult::GenerationMismatch
)
}
pub(crate) fn reconcile_stale_files_with_stop(
repo_root: &Path,
shared: &SharedIndex,
should_stop: impl Fn() -> bool,
expected_gen: u64,
) -> usize {
let paths: Vec<String> = {
let index = shared.read();
index.all_files().map(|(p, _)| p.clone()).collect()
};
let mut stale_count = 0usize;
for relative_path in &paths {
if should_stop() {
break;
}
let abs_path = repo_root.join(relative_path);
if freshen_file_if_stale_at_generation(relative_path, &abs_path, shared, expected_gen) {
stale_count += 1;
}
}
if stale_count > 0 {
let stale_paths: Vec<&str> = paths
.iter()
.filter(|p| {
let abs = repo_root.join(p.as_str());
let disk = std::fs::metadata(&abs)
.and_then(|m| m.modified())
.ok()
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
let indexed = {
let idx = shared.read();
idx.get_file(p).map(|f| f.mtime_secs).unwrap_or(0)
};
disk != indexed
})
.map(|p| p.as_str())
.take(5)
.collect();
if stale_paths.is_empty() {
warn!("reconciliation re-indexed {stale_count} file(s) (now fresh)");
} else {
warn!(
"reconciliation found {stale_count} stale file(s), still divergent: {}",
stale_paths.join(", ")
);
}
}
stale_count
}
pub(crate) fn reconcile_stale_files(repo_root: &Path, shared: &SharedIndex) -> usize {
let expected_gen = shared.current_project_generation();
reconcile_stale_files_with_stop(repo_root, shared, || false, expected_gen)
}
pub struct WatcherHandle {
_debouncer: Debouncer<NotifyRecommendedWatcher, NoCache>,
pub event_rx: std::sync::mpsc::Receiver<DebounceEventResult>,
}
pub struct WatcherTaskHandle {
pub task: tokio::task::JoinHandle<()>,
pub stop_token: Arc<AtomicBool>,
}
pub(crate) fn start_watcher(
repo_root: &Path,
debounce_ms: u64,
) -> Result<WatcherHandle, notify::Error> {
let (tx, rx) = std::sync::mpsc::channel::<DebounceEventResult>();
let mut debouncer = new_debouncer_opt::<_, NotifyRecommendedWatcher, NoCache>(
Duration::from_millis(debounce_ms),
None,
move |result: DebounceEventResult| {
let _ = tx.send(result);
},
NoCache::new(),
notify::Config::default(),
)?;
debouncer.watch(repo_root, RecursiveMode::Recursive)?;
Ok(WatcherHandle {
_debouncer: debouncer,
event_rx: rx,
})
}
pub(crate) fn process_events(
events: Vec<DebouncedEvent>,
repo_root: &Path,
shared: &SharedIndex,
burst_trackers: &mut HashMap<PathBuf, BurstTracker>,
watcher_info: &Arc<Mutex<WatcherInfo>>,
should_stop: &dyn Fn() -> bool,
expected_gen: u64,
) {
for event in events {
if should_stop() {
break;
}
if !is_relevant_event(&event) {
continue;
}
for abs_path in &event.paths {
if should_stop() {
break;
}
let relative_path = match normalize_event_path(abs_path, repo_root) {
Some(r) => r,
None => continue,
};
let language = match supported_language(abs_path) {
Some(l) => l,
None => continue,
};
if shared.read().is_path_gitignored(&relative_path) {
continue;
}
match event.kind {
EventKind::Remove(_) => {
shared.remove_file_at_generation(&relative_path, expected_gen);
let mut info = watcher_info.lock();
info.events_processed += 1;
info.last_event_at = Some(SystemTime::now());
}
EventKind::Create(_) | EventKind::Modify(_) => {
let now = Instant::now();
let tracker = burst_trackers.entry(abs_path.clone()).or_default();
tracker.update(now);
let debounce_ms = tracker.effective_debounce_ms();
maybe_reindex(&relative_path, abs_path, shared, language, expected_gen);
let mut info = watcher_info.lock();
info.events_processed += 1;
info.last_event_at = Some(SystemTime::now());
info.debounce_window_ms = debounce_ms;
}
_ => {}
}
}
}
let evict_threshold = Duration::from_secs(BurstTracker::QUIET_SECS * 2);
burst_trackers.retain(|_, tracker| tracker.last_event_at.elapsed() < evict_threshold);
}
pub async fn run_watcher_with_stop(
repo_root: PathBuf,
shared: SharedIndex,
watcher_info: Arc<Mutex<WatcherInfo>>,
stop_token: Arc<AtomicBool>,
) {
let expected_gen = shared.current_project_generation();
if stop_token.load(Ordering::Acquire) {
let mut info = watcher_info.lock();
info.state = WatcherState::Off;
return;
}
{
let mut info = watcher_info.lock();
info.state = WatcherState::Starting;
}
let mut consecutive_failures: u32 = 0;
const MAX_FAILURES: u32 = 3;
let mut cancelled = false;
'watcher: loop {
if stop_token.load(Ordering::Acquire) {
cancelled = true;
break;
}
let debounce_ms = watcher_info.lock().debounce_window_ms;
match start_watcher(&repo_root, debounce_ms) {
Err(e) => {
consecutive_failures += 1;
warn!(
"watcher: start_watcher failed (attempt {}): {}",
consecutive_failures, e
);
if consecutive_failures >= MAX_FAILURES {
let mut info = watcher_info.lock();
info.state = WatcherState::Degraded;
error!(
"watcher: entering degraded mode after {} consecutive failures",
MAX_FAILURES
);
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
Ok(handle) => {
consecutive_failures = 0;
{
let mut info = watcher_info.lock();
info.state = WatcherState::Active;
}
let mut burst_trackers: HashMap<PathBuf, BurstTracker> = HashMap::new();
let mut session_errors: u32 = 0;
const MAX_SESSION_ERRORS: u32 = 10;
const RECV_TIMEOUT_MS: u64 = 50;
let reconcile_interval_secs: u64 = std::env::var("SYMFORGE_RECONCILE_INTERVAL")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(30);
let mut last_reconcile = Instant::now();
loop {
if stop_token.load(Ordering::Acquire) {
cancelled = true;
break 'watcher;
}
if reconcile_interval_secs > 0
&& last_reconcile.elapsed() >= Duration::from_secs(reconcile_interval_secs)
{
let shared_clone = shared.clone();
let root_clone = repo_root.clone();
let watcher_info_clone = watcher_info.clone();
let stop_for_reconcile = Arc::clone(&stop_token);
let expected_gen_for_reconcile = expected_gen;
tokio::task::spawn_blocking(move || {
let stale = reconcile_stale_files_with_stop(
&root_clone,
&shared_clone,
|| stop_for_reconcile.load(Ordering::Acquire),
expected_gen_for_reconcile,
);
if stop_for_reconcile.load(Ordering::Acquire) {
return;
}
let mut info = watcher_info_clone.lock();
info.stale_files_found += stale as u64;
info.last_reconcile_at = Some(SystemTime::now());
});
let root_for_coupling = repo_root.clone();
let stop_for_coupling = Arc::clone(&stop_token);
let expected_gen_for_coupling = expected_gen;
let shared_for_coupling = shared.clone();
tokio::task::spawn_blocking(move || {
if stop_for_coupling.load(Ordering::Acquire) {
return;
}
crate::live_index::coupling::refresh_on_reconcile_tick(
&root_for_coupling,
expected_gen_for_coupling,
&shared_for_coupling,
);
});
last_reconcile = Instant::now();
}
match handle.event_rx.try_recv() {
Ok(Ok(events)) => {
let shared_clone = shared.clone();
let root_clone = repo_root.clone();
let watcher_info_clone = watcher_info.clone();
let stop_for_events = Arc::clone(&stop_token);
let expected_gen_for_events = expected_gen;
let mut trackers = std::mem::take(&mut burst_trackers);
match tokio::task::spawn_blocking(move || {
process_events(
events,
&root_clone,
&shared_clone,
&mut trackers,
&watcher_info_clone,
&|| stop_for_events.load(Ordering::Acquire),
expected_gen_for_events,
);
trackers
})
.await
{
Ok(t) => burst_trackers = t,
Err(e) => warn!("watcher: process_events panicked: {e}"),
}
}
Ok(Err(errors)) => {
let mut overflow_detected = false;
for err in &errors {
warn!("watcher: notify error: {err}");
if matches!(
err.kind,
notify::ErrorKind::Io(_)
| notify::ErrorKind::Generic(_)
| notify::ErrorKind::MaxFilesWatch
) {
overflow_detected = true;
}
}
if overflow_detected {
warn!(
"watcher: buffer overflow detected — running full reconciliation"
);
let shared_clone = shared.clone();
let root_clone = repo_root.clone();
let watcher_info_clone = watcher_info.clone();
let stop_for_reconcile = Arc::clone(&stop_token);
let expected_gen_for_reconcile = expected_gen;
tokio::task::spawn_blocking(move || {
let stale = reconcile_stale_files_with_stop(
&root_clone,
&shared_clone,
|| stop_for_reconcile.load(Ordering::Acquire),
expected_gen_for_reconcile,
);
if stop_for_reconcile.load(Ordering::Acquire) {
return;
}
let mut info = watcher_info_clone.lock();
info.overflow_count += 1;
info.last_overflow_at = Some(SystemTime::now());
info.stale_files_found += stale as u64;
info.last_reconcile_at = Some(SystemTime::now());
});
}
session_errors += u32::try_from(errors.len()).unwrap_or(u32::MAX);
if session_errors >= MAX_SESSION_ERRORS {
warn!("watcher: too many session errors, restarting watcher");
break;
}
}
Err(std::sync::mpsc::TryRecvError::Empty) => {
tokio::time::sleep(Duration::from_millis(RECV_TIMEOUT_MS)).await;
}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
warn!("watcher: event channel closed, restarting");
break;
}
}
}
consecutive_failures += 1;
if consecutive_failures >= MAX_FAILURES {
let mut info = watcher_info.lock();
info.state = WatcherState::Degraded;
error!(
"watcher: entering degraded mode after {} consecutive failures",
MAX_FAILURES
);
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
}
if cancelled {
let mut info = watcher_info.lock();
info.state = WatcherState::Off;
}
}
pub async fn run_watcher(
repo_root: PathBuf,
shared: SharedIndex,
watcher_info: Arc<Mutex<WatcherInfo>>,
) {
let stop_token = Arc::new(AtomicBool::new(false));
run_watcher_with_stop(repo_root, shared, watcher_info, stop_token).await;
}
pub fn restart_watcher(
repo_root: PathBuf,
shared: SharedIndex,
watcher_info: Arc<Mutex<WatcherInfo>>,
prev: Option<WatcherTaskHandle>,
) -> WatcherTaskHandle {
{
let mut info = watcher_info.lock();
info.state = WatcherState::Starting;
}
let stop_token = Arc::new(AtomicBool::new(false));
let stop_for_task = Arc::clone(&stop_token);
let task = tokio::spawn(async move {
if let Some(prev) = prev {
prev.stop_token.store(true, Ordering::Release);
let mut old_task = prev.task;
if tokio::time::timeout(Duration::from_secs(2), &mut old_task)
.await
.is_err()
{
warn!("watcher: previous watcher did not stop within 2s; aborting task");
old_task.abort();
}
}
run_watcher_with_stop(repo_root, shared, watcher_info, stop_for_task).await;
});
WatcherTaskHandle { task, stop_token }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::index::SkipReason;
use std::time::Duration;
use tempfile::TempDir;
static GENERATED_OUTPUT_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
struct GeneratedOutputEnvGuard(Option<std::ffi::OsString>);
impl GeneratedOutputEnvGuard {
#[allow(unsafe_code)]
fn set(value: Option<&str>) -> Self {
let previous = std::env::var_os("SYMFORGE_INDEX_GENERATED_OUTPUT");
unsafe {
match value {
Some(value) => std::env::set_var("SYMFORGE_INDEX_GENERATED_OUTPUT", value),
None => std::env::remove_var("SYMFORGE_INDEX_GENERATED_OUTPUT"),
}
}
Self(previous)
}
}
#[allow(unsafe_code)]
impl Drop for GeneratedOutputEnvGuard {
fn drop(&mut self) {
unsafe {
match &self.0 {
Some(value) => std::env::set_var("SYMFORGE_INDEX_GENERATED_OUTPUT", value),
None => std::env::remove_var("SYMFORGE_INDEX_GENERATED_OUTPUT"),
}
}
}
}
fn create_test_source(root: &Path, relative_path: &str, content: &[u8]) -> PathBuf {
let absolute_path = root.join(relative_path);
std::fs::create_dir_all(
absolute_path
.parent()
.expect("test source must have a parent directory"),
)
.expect("create test source directory");
std::fs::write(&absolute_path, content).expect("write test source");
absolute_path
}
fn stage_test_path(repository: &git2::Repository, relative_path: &str) {
let mut index = repository.index().expect("open git index");
index
.add_path(Path::new(relative_path))
.expect("stage test path");
index.write().expect("write git index");
}
fn init_test_git_repository(root: &Path) -> git2::Repository {
let repository = git2::Repository::init(root).expect("initialize git repository");
create_test_source(root, "src/main.rs", b"fn main() {}\n");
stage_test_path(&repository, "src/main.rs");
repository
}
fn assert_generated_output_skip(shared: &SharedIndex, relative_path: &str) {
let index = shared.read();
assert!(
index.get_file(relative_path).is_none(),
"{relative_path} must not be present in Tier 1"
);
let skipped = index
.skipped_files()
.iter()
.find(|skipped| skipped.path == relative_path)
.unwrap_or_else(|| panic!("{relative_path} must have a skip record"));
assert_eq!(skipped.decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(skipped.decision.reason, Some(SkipReason::GeneratedOutput));
}
#[test]
fn test_watcher_state_variants() {
let active = WatcherState::Active;
let starting = WatcherState::Starting;
let degraded = WatcherState::Degraded;
let off = WatcherState::Off;
assert_ne!(active, degraded);
assert_ne!(active, off);
assert_ne!(degraded, off);
assert_ne!(starting, active);
assert_ne!(starting, degraded);
assert_ne!(starting, off);
}
#[test]
fn test_restart_watcher_sets_starting_state() {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let _guard = rt.enter();
let shared = crate::live_index::store::LiveIndex::empty();
let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
assert_eq!(
watcher_info.lock().state,
WatcherState::Off,
"precondition: default watcher state is Off"
);
let tmp = TempDir::new().unwrap();
let handle = restart_watcher(
tmp.path().to_path_buf(),
shared,
Arc::clone(&watcher_info),
None,
);
assert_eq!(
watcher_info.lock().state,
WatcherState::Starting,
"restart_watcher should publish Starting synchronously, not Off"
);
handle.stop_token.store(true, Ordering::Release);
handle.task.abort();
}
#[test]
fn test_watcher_info_default() {
let info = WatcherInfo::default();
assert_eq!(info.state, WatcherState::Off);
assert_eq!(info.events_processed, 0);
assert!(info.last_event_at.is_none());
assert_eq!(info.debounce_window_ms, 200);
}
#[test]
fn test_burst_tracker_new() {
let tracker = BurstTracker::new();
assert_eq!(tracker.event_count, 0);
assert!(!tracker.extended);
}
#[test]
fn test_burst_tracker_extends_window() {
let mut tracker = BurstTracker::new();
let start = Instant::now();
tracker.update(start + Duration::from_millis(10));
tracker.update(start + Duration::from_millis(20));
tracker.update(start + Duration::from_millis(30));
tracker.update(start + Duration::from_millis(40));
assert!(tracker.extended, "4 events in window should trigger burst");
assert_eq!(tracker.effective_debounce_ms(), 500);
}
#[test]
fn test_burst_tracker_resets_after_quiet() {
let mut tracker = BurstTracker::new();
let past = Instant::now() - Duration::from_secs(10);
tracker.extended = true;
tracker.last_event_at = past;
assert_eq!(
tracker.effective_debounce_ms(),
200,
"after quiet period, should reset to 200ms"
);
}
#[test]
fn test_burst_tracker_new_window_resets_count() {
let mut tracker = BurstTracker::new();
let t0 = Instant::now();
tracker.update(t0 + Duration::from_millis(10));
tracker.update(t0 + Duration::from_millis(20));
tracker.update(t0 + Duration::from_millis(30));
tracker.update(t0 + Duration::from_millis(40));
assert!(tracker.extended, "should be extended after burst");
tracker.update(t0 + Duration::from_millis(350));
assert_eq!(tracker.event_count, 1, "count should reset to 1 after gap");
assert!(!tracker.extended, "extended should reset after new window");
}
#[test]
fn test_burst_tracker_base_debounce_no_burst() {
let mut tracker = BurstTracker::new();
let t0 = Instant::now();
tracker.update(t0 + Duration::from_millis(10));
tracker.update(t0 + Duration::from_millis(20));
assert!(!tracker.extended);
assert_eq!(tracker.effective_debounce_ms(), 200);
}
#[test]
#[cfg(windows)]
fn test_normalize_event_path_basic() {
let abs = Path::new(r"C:\repo\src\main.rs");
let root = Path::new(r"C:\repo");
let result = normalize_event_path(abs, root);
assert_eq!(result, Some("src/main.rs".to_string()));
}
#[test]
#[cfg(windows)]
fn test_normalize_event_path_unc_prefix() {
let abs = Path::new(r"\\?\C:\repo\src\main.rs");
let root = Path::new(r"C:\repo");
let result = normalize_event_path(abs, root);
assert_eq!(result, Some("src/main.rs".to_string()));
}
#[test]
#[cfg(windows)]
fn test_normalize_event_path_outside_repo() {
let abs = Path::new(r"C:\other\file.rs");
let root = Path::new(r"C:\repo");
let result = normalize_event_path(abs, root);
assert_eq!(result, None);
}
#[test]
fn test_normalize_event_path_forward_slash() {
let abs = Path::new("/home/user/project/src/lib.rs");
let root = Path::new("/home/user/project");
let result = normalize_event_path(abs, root);
assert_eq!(result, Some("src/lib.rs".to_string()));
}
#[test]
fn test_normalize_event_path_nested_subdir() {
let abs = Path::new("/repo/a/b/c.rs");
let root = Path::new("/repo");
let result = normalize_event_path(abs, root);
assert_eq!(result, Some("a/b/c.rs".to_string()));
}
#[test]
fn test_supported_language_rs() {
let path = Path::new("src/main.rs");
assert_eq!(supported_language(path), Some(LanguageId::Rust));
}
#[test]
fn test_supported_language_py() {
let path = Path::new("scripts/build.py");
assert_eq!(supported_language(path), Some(LanguageId::Python));
}
#[test]
fn test_supported_language_ts() {
let path = Path::new("src/app.ts");
assert_eq!(supported_language(path), Some(LanguageId::TypeScript));
}
#[test]
fn test_supported_language_go() {
let path = Path::new("main.go");
assert_eq!(supported_language(path), Some(LanguageId::Go));
}
#[test]
fn test_supported_language_java() {
let path = Path::new("Main.java");
assert_eq!(supported_language(path), Some(LanguageId::Java));
}
#[test]
fn test_supported_language_txt() {
let path = Path::new("README.txt");
assert_eq!(supported_language(path), None);
}
#[test]
fn test_supported_language_md() {
let path = Path::new("README.md");
assert_eq!(supported_language(path), Some(LanguageId::Markdown));
}
#[test]
fn test_supported_language_no_extension() {
let path = Path::new("Makefile");
assert_eq!(supported_language(path), None);
}
#[test]
fn test_maybe_reindex_updates_reverse_index_on_change() {
use crate::domain::LanguageId;
use crate::live_index::store::IndexedFile;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let rs_path = tmp
.path()
.join("tests")
.join("generated")
.join("lib.generated.rs");
std::fs::create_dir_all(rs_path.parent().unwrap()).unwrap();
let initial_content = b"fn entry() { old_function(); }";
std::fs::write(&rs_path, initial_content).unwrap();
let rel_path = "tests/generated/lib.generated.rs";
let shared: crate::live_index::store::SharedIndex = {
let result = crate::parsing::process_file(rel_path, initial_content, LanguageId::Rust);
let indexed = IndexedFile::from_parse_result(result, initial_content.to_vec());
let mut index = crate::live_index::store::LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: None,
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
index.update_file(rel_path.to_string(), indexed);
crate::live_index::SharedIndexHandle::shared(index)
};
{
let idx = shared.read();
assert!(
idx.reverse_index.contains_key("old_function"),
"reverse_index should contain 'old_function' after initial parse"
);
}
let updated_content = b"fn entry() { new_function(); }";
std::fs::write(&rs_path, updated_content).unwrap();
let expected_gen = shared.current_project_generation();
let result = maybe_reindex(rel_path, &rs_path, &shared, LanguageId::Rust, expected_gen);
assert_eq!(
result,
ReindexResult::Reindexed,
"file should be re-parsed on content change"
);
{
let idx = shared.read();
assert!(
idx.reverse_index.contains_key("new_function"),
"reverse_index should contain 'new_function' after re-index"
);
assert!(
!idx.reverse_index.contains_key("old_function"),
"reverse_index should NOT contain 'old_function' after re-index"
);
let file = idx
.get_file(rel_path)
.expect("reindexed file should still exist");
assert!(file.classification.is_code());
assert!(file.classification.is_test);
assert!(file.classification.is_generated);
}
}
#[test]
fn reindex_refuses_hidden_path_to_match_bulk_walk() {
use crate::domain::LanguageId;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let rel_path = ".github/workflows/ci.yml";
let abs_path = tmp.path().join(".github").join("workflows").join("ci.yml");
std::fs::create_dir_all(abs_path.parent().unwrap()).unwrap();
std::fs::write(&abs_path, "name: ci\non: [push]\n").unwrap();
let shared: crate::live_index::store::SharedIndex = {
let index = crate::live_index::store::LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: None,
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
crate::live_index::SharedIndexHandle::shared(index)
};
let expected_gen = shared.current_project_generation();
let result = maybe_reindex(rel_path, &abs_path, &shared, LanguageId::Yaml, expected_gen);
assert_eq!(
result,
ReindexResult::Skipped,
"hidden-path file must be scan-policy skipped, not parsed/inserted"
);
let idx = shared.read();
assert!(
idx.get_file(rel_path).is_none(),
"hidden-path file must NOT be inserted into the parsed index"
);
assert_eq!(
idx.tier_counts(),
(0, 0, 0),
"hidden-path skip must not mint a tier record; index stays empty"
);
}
#[test]
fn reindex_still_admits_visible_supported_file() {
use crate::domain::LanguageId;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let rel_path = "config/ci.yml";
let abs_path = tmp.path().join("config").join("ci.yml");
std::fs::create_dir_all(abs_path.parent().unwrap()).unwrap();
std::fs::write(&abs_path, "name: ci\non: [push]\n").unwrap();
let shared: crate::live_index::store::SharedIndex = {
let index = crate::live_index::store::LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: None,
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
crate::live_index::SharedIndexHandle::shared(index)
};
let expected_gen = shared.current_project_generation();
let result = maybe_reindex(rel_path, &abs_path, &shared, LanguageId::Yaml, expected_gen);
assert_eq!(
result,
ReindexResult::Reindexed,
"visible supported file must be parsed and inserted"
);
let idx = shared.read();
assert!(
idx.get_file(rel_path).is_some(),
"visible supported file must be present in the parsed index"
);
}
#[test]
fn process_events_predicate_skips_gitignored_state_dir() {
use crate::live_index::store::LiveIndex;
use ignore::gitignore::GitignoreBuilder;
let mut builder = GitignoreBuilder::new("/repo");
builder.add_line(None, "/.*/").unwrap();
builder.add_line(None, "!/.github/").unwrap();
let gitignore = builder.build().unwrap();
let index = LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: Some(gitignore),
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
assert!(index.is_path_gitignored(".symforge/tee/1780038581944-000040-handlers.rs"));
assert!(index.is_path_gitignored(".claude/settings.local.json"));
assert!(!index.is_path_gitignored("src/sidecar/handlers.rs"));
assert!(!index.is_path_gitignored(".github/workflows/ci.yml"));
assert!(!index.is_path_gitignored("vendor/tree-sitter-scss/src/parser.c"));
assert!(!index.is_path_gitignored("/abs/path.rs"));
}
#[test]
fn test_maybe_reindex_hash_skip_on_unchanged_content() {
use crate::domain::LanguageId;
use crate::live_index::store::IndexedFile;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let rs_path = tmp.path().join("a.rs");
let content = b"fn foo() {}";
std::fs::write(&rs_path, content).unwrap();
let rel_path = "a.rs";
let shared: crate::live_index::store::SharedIndex = {
let result = crate::parsing::process_file(rel_path, content, LanguageId::Rust);
let indexed = IndexedFile::from_parse_result(result, content.to_vec());
let mut index = crate::live_index::store::LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: None,
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
index.update_file(rel_path.to_string(), indexed);
crate::live_index::SharedIndexHandle::shared(index)
};
let expected_gen = shared.current_project_generation();
let result = maybe_reindex(rel_path, &rs_path, &shared, LanguageId::Rust, expected_gen);
assert_eq!(
result,
ReindexResult::HashSkip,
"unchanged content should produce HashSkip"
);
}
#[test]
fn test_read_and_index_preserves_crlf_bytes_and_hash() {
use crate::domain::LanguageId;
let tmp = TempDir::new().unwrap();
let rs_path = tmp.path().join("src").join("lib.rs");
std::fs::create_dir_all(rs_path.parent().unwrap()).unwrap();
let content = b"fn entry() {\r\n watched_call();\r\n}\r\n";
std::fs::write(&rs_path, content).unwrap();
let rel_path = "src/lib.rs";
let shared: crate::live_index::store::SharedIndex = {
let index = crate::live_index::store::LiveIndex {
files: std::collections::HashMap::new(),
loaded_at: std::time::Instant::now(),
loaded_at_system: std::time::SystemTime::now(),
load_duration: std::time::Duration::ZERO,
cb_state: crate::live_index::store::CircuitBreakerState::new(0.20),
is_empty: false,
load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
reverse_index: std::collections::HashMap::new(),
files_by_basename: std::collections::HashMap::new(),
files_by_dir_component: std::collections::HashMap::new(),
trigram_index: crate::live_index::trigram::TrigramIndex::new(),
gitignore: None,
skipped_files: Vec::new(),
coupling_store: None,
local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
indexed_root: None,
};
crate::live_index::SharedIndexHandle::shared(index)
};
let expected_gen = shared.current_project_generation();
let result = read_and_index(rel_path, &rs_path, &shared, LanguageId::Rust, expected_gen);
assert_eq!(result, ReindexResult::Reindexed);
let idx = shared.read();
let file = idx
.get_file(rel_path)
.expect("watcher should index the CRLF file");
assert_eq!(file.content, content);
assert_eq!(file.byte_len, content.len() as u64);
assert_eq!(file.content_hash, crate::hash::digest_hex(content));
}
#[test]
fn test_maybe_reindex_retries_transient_not_found() {
let tmp = TempDir::new().unwrap();
let src_dir = tmp.path().join("src");
std::fs::create_dir_all(&src_dir).unwrap();
let rel_path = "src/flaky.rs";
let abs_path = tmp.path().join(rel_path);
let content = b"fn flaky() -> usize { 1 }";
std::fs::write(&abs_path, content).unwrap();
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
std::fs::remove_file(&abs_path).unwrap();
let restore_path = abs_path.clone();
let restore = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(20));
std::fs::write(&restore_path, content).unwrap();
});
let result = maybe_reindex(rel_path, &abs_path, &shared, LanguageId::Rust, expected_gen);
restore.join().unwrap();
assert_ne!(
result,
ReindexResult::Removed,
"transient NotFound should be retried instead of removed immediately"
);
let index = shared.read();
assert!(
index.get_file(rel_path).is_some(),
"transiently missing file should remain indexed after retry succeeds"
);
}
#[test]
fn test_maybe_reindex_removes_persistent_not_found() {
let tmp = TempDir::new().unwrap();
let src_dir = tmp.path().join("src");
std::fs::create_dir_all(&src_dir).unwrap();
let rel_path = "src/deleted.rs";
let abs_path = tmp.path().join(rel_path);
std::fs::write(&abs_path, b"fn deleted() {}").unwrap();
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
std::fs::remove_file(&abs_path).unwrap();
let result = maybe_reindex(rel_path, &abs_path, &shared, LanguageId::Rust, expected_gen);
assert_eq!(
result,
ReindexResult::Removed,
"persistent NotFound should remove after bounded retries"
);
let index = shared.read();
assert!(
index.get_file(rel_path).is_none(),
"persistently missing file should be removed from the index"
);
}
#[test]
fn slipped_past_cancellation_fence_increments_counter() {
let project_a = TempDir::new().unwrap();
let project_b = TempDir::new().unwrap();
let a_src = project_a.path().join("src");
let b_src = project_b.path().join("src");
std::fs::create_dir_all(&a_src).unwrap();
std::fs::create_dir_all(&b_src).unwrap();
std::fs::write(a_src.join("a.rs"), b"fn a() {}").unwrap();
std::fs::write(b_src.join("b.rs"), b"fn b() {}").unwrap();
let shared = crate::live_index::LiveIndex::load(project_a.path()).unwrap();
let stale_gen = shared.current_project_generation();
let rejected_before = shared.current_rejected_stale_mutations();
shared.reload(project_b.path()).unwrap();
let stale = reconcile_stale_files_with_stop(project_a.path(), &shared, || false, stale_gen);
assert_eq!(
stale, 1,
"stale doomed reconcile should visit B's path once"
);
assert!(
shared.current_rejected_stale_mutations() > rejected_before,
"stale-generation watcher reconcile should be rejected by the fence"
);
let index = shared.read();
assert!(
index.get_file("src/b.rs").is_some(),
"B file should survive stale-generation reconcile"
);
}
#[test]
fn test_maybe_reindex_admission_skips_lockfile() {
let tmp = TempDir::new().unwrap();
std::fs::write(tmp.path().join("main.rs"), b"fn main() {}").unwrap();
let lock_rel = "package-lock.json";
let lock_abs = tmp.path().join(lock_rel);
std::fs::write(&lock_abs, br#"{"name":"x","lockfileVersion":3}"#).unwrap();
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
let (t1_before, t2_before, t3_before) = {
let idx = shared.read();
assert!(
idx.get_file(lock_rel).is_none(),
"lockfile must not be a Tier-1 file after bulk load"
);
assert_eq!(
idx.skipped_files()
.iter()
.filter(|sf| sf.path == lock_rel)
.count(),
1,
"lockfile must have exactly one skip record after bulk load"
);
idx.tier_counts()
};
assert_eq!((t1_before, t2_before, t3_before), (1, 1, 0));
std::fs::write(&lock_abs, br#"{"name":"x","lockfileVersion":3,"extra":1}"#).unwrap();
let result = maybe_reindex(lock_rel, &lock_abs, &shared, LanguageId::Json, expected_gen);
assert_eq!(
result,
ReindexResult::Skipped,
"lockfile must be admission-skipped, not re-parsed into the index"
);
let idx = shared.read();
assert!(
idx.get_file(lock_rel).is_none(),
"lockfile must STILL be absent from Tier-1 files after the reindex attempt"
);
assert_eq!(
idx.skipped_files()
.iter()
.filter(|sf| sf.path == lock_rel)
.count(),
1,
"skip record must remain de-duplicated (exactly one) after re-skip"
);
let sf = idx
.skipped_files()
.iter()
.find(|sf| sf.path == lock_rel)
.expect("skip record must survive");
assert_eq!(sf.decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(
sf.decision.reason,
Some(crate::domain::index::SkipReason::DependencyLockfile),
"lockfile skip reason must be preserved"
);
assert_eq!(
idx.tier_counts(),
(t1_before, t2_before, t3_before),
"tier counts must be unchanged by the admission-skipped reindex"
);
}
#[test]
fn test_freshen_admission_demotes_grown_file() {
use crate::domain::index::{AdmissionTier, SkipReason};
let tmp = TempDir::new().unwrap();
let rel = "big.rs";
let abs = tmp.path().join(rel);
std::fs::write(&abs, b"fn small() {}\n").unwrap();
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
{
let idx = shared.read();
assert!(
idx.get_file(rel).is_some(),
"small source file must be Tier 1 after load"
);
assert_eq!(idx.tier_counts(), (1, 0, 0));
}
let mut grown = b"fn big() {}\n".to_vec();
grown.resize(4_400_000, b' ');
std::fs::write(&abs, &grown).unwrap();
{
let f = std::fs::File::options().write(true).open(&abs).unwrap();
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(120);
f.set_times(std::fs::FileTimes::new().set_modified(old))
.unwrap();
}
let outcome = freshen_file_if_stale(rel, &abs, &shared, expected_gen);
assert!(
matches!(outcome, FreshenResult::StaleReindexed),
"freshen should report the stale file was reconciled"
);
let idx = shared.read();
assert!(
idx.get_file(rel).is_none(),
"grown file must be REMOVED from Tier-1 files (Tier 1 -> Tier 2 transition)"
);
let sf = idx
.skipped_files()
.iter()
.find(|sf| sf.path == rel)
.expect("grown file must have a Tier-2 skip record");
assert_eq!(sf.decision.tier, AdmissionTier::MetadataOnly);
assert_eq!(sf.decision.reason, Some(SkipReason::SizeThreshold));
assert_eq!(
idx.tier_counts(),
(0, 1, 0),
"tier counts must reflect the demotion: 0 Tier-1, 1 Tier-2"
);
}
#[test]
fn test_new_generated_output_directory_stays_metadata_only() {
let _env_lock = GENERATED_OUTPUT_ENV_LOCK.lock().unwrap();
let _env = GeneratedOutputEnvGuard::set(None);
let tmp = TempDir::new().unwrap();
let _repository = init_test_git_repository(tmp.path());
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
let relative_path = "graphify-out/cache/new.rs";
let absolute_path =
create_test_source(tmp.path(), relative_path, b"fn generated_after_load() {}\n");
let result = maybe_reindex(
relative_path,
&absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
assert_eq!(
result,
ReindexResult::Skipped,
"a generated directory created after load must follow bulk admission"
);
assert_generated_output_skip(&shared, relative_path);
assert_eq!(shared.read().tier_counts(), (1, 1, 0));
}
#[test]
fn test_bulk_demoted_generated_output_stays_metadata_only_on_watcher_event() {
let _env_lock = GENERATED_OUTPUT_ENV_LOCK.lock().unwrap();
let _env = GeneratedOutputEnvGuard::set(None);
let tmp = TempDir::new().unwrap();
let _repository = init_test_git_repository(tmp.path());
let relative_path = "graphify-out/cache/existing.rs";
let absolute_path = create_test_source(
tmp.path(),
relative_path,
b"fn generated_before_load() {}\n",
);
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
assert_generated_output_skip(&shared, relative_path);
std::fs::write(&absolute_path, b"fn generated_before_load_changed() {}\n").unwrap();
let result = maybe_reindex(
relative_path,
&absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
assert_eq!(
result,
ReindexResult::Skipped,
"watcher must not promote a bulk-demoted generated file"
);
assert_generated_output_skip(&shared, relative_path);
assert_eq!(
shared
.read()
.skipped_files()
.iter()
.filter(|skipped| skipped.path == relative_path)
.count(),
1,
"repeated demotion must retain exactly one skip record"
);
}
#[test]
fn test_tracked_and_prefix_rescue_re_admit_generated_output() {
let _env_lock = GENERATED_OUTPUT_ENV_LOCK.lock().unwrap();
let _env = GeneratedOutputEnvGuard::set(None);
let tmp = TempDir::new().unwrap();
let repository = init_test_git_repository(tmp.path());
let tracked_relative_path = "graphify-out/cache/tracked.rs";
let sibling_relative_path = "graphify-out/cache/sibling.rs";
let tracked_absolute_path = create_test_source(
tmp.path(),
tracked_relative_path,
b"fn tracked_generated() {}\n",
);
let sibling_absolute_path = create_test_source(
tmp.path(),
sibling_relative_path,
b"fn untracked_sibling() {}\n",
);
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
assert_generated_output_skip(&shared, tracked_relative_path);
assert_generated_output_skip(&shared, sibling_relative_path);
stage_test_path(&repository, tracked_relative_path);
let tracked_result = maybe_reindex(
tracked_relative_path,
&tracked_absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
let sibling_result = maybe_reindex(
sibling_relative_path,
&sibling_absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
assert_eq!(tracked_result, ReindexResult::Reindexed);
assert_eq!(
sibling_result,
ReindexResult::Reindexed,
"one tracked file must rescue the entire generated-output prefix"
);
let index = shared.read();
assert!(index.get_file(tracked_relative_path).is_some());
assert!(index.get_file(sibling_relative_path).is_some());
assert!(
index
.skipped_files()
.iter()
.all(|skipped| skipped.path != tracked_relative_path
&& skipped.path != sibling_relative_path),
"Tier-2 skip records must be cleared after tracked-prefix rescue"
);
assert_eq!(index.tier_counts(), (3, 0, 0));
}
#[test]
fn test_generated_output_opt_in_re_admits_tier_one() {
let _env_lock = GENERATED_OUTPUT_ENV_LOCK.lock().unwrap();
let _env = GeneratedOutputEnvGuard::set(None);
let tmp = TempDir::new().unwrap();
let _repository = init_test_git_repository(tmp.path());
let relative_path = "graphify-out/cache/opted_in.rs";
let absolute_path = create_test_source(tmp.path(), relative_path, b"fn opted_in() {}\n");
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
assert_generated_output_skip(&shared, relative_path);
let _enabled = GeneratedOutputEnvGuard::set(Some("1"));
let result = maybe_reindex(
relative_path,
&absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
assert_eq!(result, ReindexResult::Reindexed);
let index = shared.read();
assert!(index.get_file(relative_path).is_some());
assert!(
index
.skipped_files()
.iter()
.all(|skipped| skipped.path != relative_path)
);
assert_eq!(index.tier_counts(), (2, 0, 0));
}
#[test]
fn test_generated_output_watcher_non_git_tree_fails_open() {
let _env_lock = GENERATED_OUTPUT_ENV_LOCK.lock().unwrap();
let _env = GeneratedOutputEnvGuard::set(None);
let tmp = TempDir::new().unwrap();
create_test_source(tmp.path(), "src/main.rs", b"fn main() {}\n");
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
let relative_path = "graphify-out/cache/non_git.rs";
let absolute_path =
create_test_source(tmp.path(), relative_path, b"fn non_git_generated() {}\n");
let result = maybe_reindex(
relative_path,
&absolute_path,
&shared,
LanguageId::Rust,
expected_gen,
);
assert_eq!(
result,
ReindexResult::Reindexed,
"without readable Git evidence the watcher must fail open"
);
let index = shared.read();
assert!(index.get_file(relative_path).is_some());
assert_eq!(index.tier_counts(), (2, 0, 0));
}
#[test]
fn test_maybe_reindex_clears_stale_skip_on_shrink() {
let tmp = TempDir::new().unwrap();
let rel = "shrink.rs";
let abs = tmp.path().join(rel);
let mut big = b"fn shrink() {}\n".to_vec();
big.resize(4_400_000, b' ');
std::fs::write(&abs, &big).unwrap();
let shared = crate::live_index::LiveIndex::load(tmp.path()).unwrap();
let expected_gen = shared.current_project_generation();
{
let idx = shared.read();
assert!(
idx.get_file(rel).is_none(),
"oversized file must start Tier 2"
);
assert_eq!(idx.tier_counts(), (0, 1, 0));
}
std::fs::write(&abs, b"fn shrink() {}\n").unwrap();
let result = maybe_reindex(rel, &abs, &shared, LanguageId::Rust, expected_gen);
assert_eq!(
result,
ReindexResult::Reindexed,
"shrunk file must be re-admitted as Tier 1"
);
let idx = shared.read();
assert!(
idx.get_file(rel).is_some(),
"shrunk file must now be a Tier-1 indexed file"
);
assert_eq!(
idx.skipped_files()
.iter()
.filter(|sf| sf.path == rel)
.count(),
0,
"stale Tier-2 skip record must be cleared on Tier-1 re-admission"
);
assert_eq!(
idx.tier_counts(),
(1, 0, 0),
"no double-counting: 1 Tier-1, 0 Tier-2"
);
}
}