use std::path::{Path, PathBuf};
use tracing::{debug, trace, warn};
use crate::domain::{
FileClassification, FileDisposition, LanguageId, MetadataOnlyReason, ScoutDecision,
};
use crate::hash;
use crate::live_index::store::{IndexedFile, SharedIndex};
use crate::parsing;
#[derive(Debug, PartialEq, Eq)]
pub enum ReindexResult {
HashSkip,
Reindexed,
Skipped,
NotFound,
Removed,
ReadError(String),
}
pub(crate) fn maybe_reindex<L>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
) -> ReindexResult
where
L: Into<Option<LanguageId>>,
{
let language = language.into();
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 catalog_terminal_disposition(decision: &ScoutDecision) -> Option<FileDisposition> {
match decision {
ScoutDecision::HardSkip { reason } => Some(FileDisposition::HardSkip { reason: *reason }),
ScoutDecision::MetadataOnly { reason } => Some(FileDisposition::MetadataOnly {
reason: reason.clone(),
}),
ScoutDecision::Ingest { .. } | ScoutDecision::Unavailable { .. } => None,
}
}
pub(crate) fn read_and_index<L>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
) -> ReindexResult
where
L: Into<Option<LanguageId>>,
{
read_and_index_with_stable_read(
relative_path,
abs_path,
shared,
language,
expected_gen,
crate::live_index::store::stable_read_file,
)
}
pub(crate) fn admit_and_index_single_path(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
) -> ReindexResult {
read_and_index(relative_path, abs_path, shared, None, expected_gen)
}
pub(crate) fn read_and_index_with_stable_read<L, R>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
mut stable_read: R,
) -> ReindexResult
where
L: Into<Option<LanguageId>>,
R: FnMut(&Path, &crate::domain::FileStamp) -> crate::live_index::store::StableReadOutcome,
{
let language = language.into();
let relative = Path::new(relative_path);
if crate::discovery::path_is_hard_scope_excluded(relative)
|| shared.is_source_excluded(relative)
|| shared.read().is_path_gitignored(relative_path)
{
let removed = shared.remove_file_at_generation(relative_path, expected_gen);
if removed {
debug!("watcher: source-scope eviction {relative_path}");
} else {
trace!("watcher: source-scope skip (no prior record) {relative_path}");
}
return ReindexResult::Skipped;
}
const MAX_PUBLICATION_ATTEMPTS: usize = 4;
for attempt in 1..=MAX_PUBLICATION_ATTEMPTS {
let base_publication_gen = shared.published_state().generation;
let mut scouted = match crate::discovery::scout_single_path(relative_path, abs_path) {
Ok(scouted) => scouted,
Err(error) => {
if matches!(
std::fs::metadata(abs_path),
Err(error) if error.kind() == std::io::ErrorKind::NotFound
) {
return ReindexResult::NotFound;
}
warn!("watcher: failed to scout {relative_path}: {error}");
return ReindexResult::ReadError(error.to_string());
}
};
let mtime_secs = scouted
.stamp
.modified_hint
.and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
.map(|duration| duration.as_secs())
.unwrap_or(0);
if let Some(disposition) = catalog_terminal_disposition(&scouted.decision) {
if shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
base_publication_gen,
) {
debug!("watcher: metadata-terminal admission {relative_path}");
return ReindexResult::Skipped;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying metadata-terminal publication");
continue;
}
trace!("watcher: stale metadata-terminal admission rejected: {relative_path}");
return ReindexResult::Skipped;
}
if let ScoutDecision::Unavailable { stage, kind } = &scouted.decision {
if *stage == crate::domain::AccessStage::Metadata
&& *kind == crate::domain::AccessErrorKind::NotFound
{
return ReindexResult::NotFound;
}
let disposition = FileDisposition::Unreadable {
stage: *stage,
kind: *kind,
};
if shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
base_publication_gen,
) {
return ReindexResult::ReadError("single-path scout unavailable".to_string());
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying unavailable publication");
continue;
}
return ReindexResult::ReadError("single-path scout unavailable".to_string());
}
if let Some(root) = project_root_from_paths(abs_path, relative_path)
&& crate::discovery::is_untracked_generated_output_path(&root, relative_path)
{
let decision = ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::GeneratedOrVendor,
};
let disposition = catalog_terminal_disposition(&decision)
.expect("metadata-only decision must project to a terminal disposition");
scouted.decision = decision;
if shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
base_publication_gen,
) {
return ReindexResult::Skipped;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying generated-output publication");
continue;
}
return ReindexResult::Skipped;
}
let targets = match &scouted.decision {
ScoutDecision::Ingest { targets } => *targets,
_ => unreachable!("terminal scout decisions return before content ingestion"),
};
let language = language
.clone()
.or_else(|| scouted.language.clone())
.unwrap_or(LanguageId::Text);
let bytes = match stable_read(abs_path, &scouted.stamp) {
crate::live_index::store::StableReadOutcome::Accepted { bytes, .. } => bytes,
crate::live_index::store::StableReadOutcome::HardSkip { reason } => {
let decision = ScoutDecision::HardSkip { reason };
let disposition = catalog_terminal_disposition(&decision)
.expect("hard-skip decision must project to a terminal disposition");
scouted.decision = decision;
if shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
base_publication_gen,
) {
return ReindexResult::Skipped;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(
attempt,
"watcher: retrying stable-read hard-skip publication"
);
continue;
}
return ReindexResult::Skipped;
}
crate::live_index::store::StableReadOutcome::Unreadable { stage, kind } => {
if !shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::Unreadable { stage, kind },
expected_gen,
base_publication_gen,
) && shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(
attempt,
"watcher: retrying stable-read unavailable publication"
);
continue;
}
return ReindexResult::ReadError("stable read unavailable".to_string());
}
crate::live_index::store::StableReadOutcome::UnstableDuringRead => {
if !shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::UnstableDuringRead,
expected_gen,
base_publication_gen,
) && shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying unstable-read publication");
continue;
}
return ReindexResult::ReadError("file changed during stable read".to_string());
}
};
if let crate::knowledge::StableContentAdmission::MetadataOnly(reason) =
crate::knowledge::classify_stable_content(relative_path, targets, &bytes)
{
if shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::MetadataOnly { reason },
expected_gen,
base_publication_gen,
) {
return ReindexResult::Skipped;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying content-policy publication");
continue;
}
return ReindexResult::Skipped;
}
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
{
drop(index);
if shared.publish_hash_skip_at_generation(
relative_path,
mtime_secs,
scouted,
targets,
expected_gen,
base_publication_gen,
) {
debug!("watcher: hash-skip {relative_path}");
return ReindexResult::HashSkip;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying hash-skip publication");
continue;
}
return ReindexResult::Skipped;
}
}
let result = parsing::process_file_with_classification(
relative_path,
&bytes,
language,
FileClassification::for_indexed_path(relative_path, targets),
);
let indexed = IndexedFile::from_parse_result(result, bytes).with_mtime(mtime_secs);
if shared.publish_indexed_file_at_generation(
relative_path,
indexed,
scouted,
targets,
expected_gen,
base_publication_gen,
) {
debug!("watcher: re-indexed {relative_path}");
return ReindexResult::Reindexed;
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != base_publication_gen
{
trace!(attempt, "watcher: retrying indexed-file publication");
continue;
}
trace!("watcher: stale indexed-file publication rejected: {relative_path}");
return ReindexResult::Skipped;
}
warn!(
"watcher: aborting {relative_path} after {MAX_PUBLICATION_ATTEMPTS} concurrent publications"
);
ReindexResult::Skipped
}
pub fn update_file_from_disk(
shared: &SharedIndex,
repo_root: &Path,
relative_path: &str,
) -> ReindexResult {
let relative = relative_path.replace('\\', "/");
let abs_path = repo_root.join(&relative);
let expected_gen = shared.current_project_generation();
admit_and_index_single_path(&relative, &abs_path, shared, expected_gen)
}
pub fn remove_file(shared: &SharedIndex, relative_path: &str) -> bool {
let relative = relative_path.replace('\\', "/");
shared.remove_file_at_generation(&relative, shared.current_project_generation())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::live_index::LiveIndex;
#[test]
fn facade_update_and_remove_round_trip() {
let dir = tempfile::TempDir::new().expect("root");
std::fs::write(
dir.path().join("lib.rs"),
"pub fn first() {}
",
)
.expect("seed");
let shared = LiveIndex::load(dir.path()).expect("cold load");
assert_eq!(shared.read().file_count(), 1);
std::fs::write(
dir.path().join("extra.rs"),
"pub fn second() {}
",
)
.expect("new file");
let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
assert!(
matches!(outcome, ReindexResult::Reindexed),
"new file must be admitted and parsed, got {outcome:?}"
);
assert_eq!(shared.read().file_count(), 2);
let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
assert!(
matches!(outcome, ReindexResult::HashSkip),
"unchanged file must hash-skip, got {outcome:?}"
);
std::fs::write(
dir.path().join("extra.rs"),
"pub fn renamed() {}
",
)
.expect("edit");
let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
assert!(
matches!(outcome, ReindexResult::Reindexed),
"edited file must re-parse, got {outcome:?}"
);
assert!(remove_file(&shared, "extra.rs"), "tracked file removes");
assert_eq!(shared.read().file_count(), 1);
assert!(
remove_file(&shared, "extra.rs"),
"no-op removal still passes the generation fence"
);
assert_eq!(shared.read().file_count(), 1);
let outcome = update_file_from_disk(&shared, dir.path(), "ghost.rs");
assert!(
matches!(outcome, ReindexResult::NotFound),
"missing file must be NotFound, got {outcome:?}"
);
}
}