use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::{debug, trace, warn};
use crate::domain::{
FileClassification, FileDisposition, LanguageId, MetadataOnlyReason, ScoutDecision,
};
use crate::hash;
use crate::live_index::store::{
FencedRemoval, IndexedFile, PublicationFence, PublishedGeneration, SharedIndex,
SnapshotVerifiedAdmission, SnapshotVerifiedFile,
};
use crate::parsing;
#[derive(Debug, PartialEq, Eq)]
pub enum ReindexResult {
HashSkip,
Reindexed,
Skipped,
NotFound,
Removed,
ReadError(String),
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ReindexOutcome {
HashSkip,
Reindexed,
Skipped,
NotFound,
Removed,
ReadError(String),
PublicationRejected,
}
impl ReindexOutcome {
fn into_public_compat(self) -> ReindexResult {
match self {
Self::HashSkip => ReindexResult::HashSkip,
Self::Reindexed => ReindexResult::Reindexed,
Self::Skipped => ReindexResult::Skipped,
Self::NotFound => ReindexResult::NotFound,
Self::Removed => ReindexResult::Removed,
Self::ReadError(error) => ReindexResult::ReadError(error),
Self::PublicationRejected => ReindexResult::Skipped,
}
}
}
pub(crate) struct ReindexReceipt {
pub outcome: ReindexOutcome,
pub observed_at: PublicationFence,
pub published: Option<Arc<PublishedGeneration>>,
pub snapshot_created: bool,
}
impl ReindexReceipt {
fn observed(outcome: ReindexOutcome, observed_at: PublicationFence) -> Self {
Self {
outcome,
observed_at,
published: None,
snapshot_created: false,
}
}
fn published(
outcome: ReindexOutcome,
observed_at: PublicationFence,
published: Arc<PublishedGeneration>,
) -> Self {
Self {
outcome,
observed_at,
published: Some(published),
snapshot_created: false,
}
}
}
pub(crate) fn maybe_reindex<L>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
) -> ReindexOutcome
where
L: Into<Option<LanguageId>>,
{
let language = language.into();
let first =
read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen);
if first.observed_at.project_generation != expected_gen {
return ReindexOutcome::PublicationRejected;
}
let mut last_absence_fence = match first.outcome {
ReindexOutcome::NotFound => first.observed_at,
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));
let retry =
read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen);
if retry.observed_at.project_generation != expected_gen {
return ReindexOutcome::PublicationRejected;
}
match retry.outcome {
ReindexOutcome::NotFound => last_absence_fence = retry.observed_at,
other => return other,
}
}
finalize_missing_file(
shared,
relative_path,
abs_path,
expected_gen,
last_absence_fence,
)
}
fn finalize_missing_file(
shared: &SharedIndex,
relative_path: &str,
abs_path: &Path,
expected_gen: u64,
absence_fence: PublicationFence,
) -> ReindexOutcome {
if absence_fence.project_generation != expected_gen {
return ReindexOutcome::PublicationRejected;
}
match shared.remove_file_if_absent_at_publication_fence_with_receipt(
relative_path,
abs_path,
absence_fence,
) {
FencedRemoval::Removed(_) => {
warn!("watcher: file not found after retries, removed from index: {relative_path}");
ReindexOutcome::Removed
}
FencedRemoval::NothingHeld => {
trace!(
"watcher: file not found and nothing held, no removal to publish: {relative_path}"
);
ReindexOutcome::NotFound
}
FencedRemoval::Rejected => {
trace!(
"watcher: file not found after retries, stale publication rejected remove: {relative_path}"
);
ReindexOutcome::PublicationRejected
}
}
}
fn project_root_from_paths(abs_path: &Path, relative_path: &str) -> Option<PathBuf> {
let depth = relative_depth(Path::new(relative_path));
abs_path.ancestors().nth(depth).map(|p| p.to_path_buf())
}
fn classify_for_project(
abs_path: &Path,
relative_path: &str,
targets: crate::domain::IndexTargets,
bytes: &[u8],
) -> crate::knowledge::StableContentAdmission {
match project_root_from_paths(abs_path, relative_path) {
Some(root) => {
crate::knowledge::classify_stable_content_for_root(&root, relative_path, targets, bytes)
}
None => crate::knowledge::classify_stable_content(relative_path, targets, bytes),
}
}
fn relative_depth(relative: &Path) -> usize {
relative
.components()
.filter(|component| !matches!(component, std::path::Component::CurDir))
.count()
}
fn path_crosses_symlink(abs_path: &Path, relative: &Path) -> bool {
abs_path
.ancestors()
.take(relative_depth(relative))
.any(|path| {
std::fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_symlink())
})
}
fn path_spelling_is_refused(abs_path: &Path, relative_path: &str) -> bool {
project_root_from_paths(abs_path, relative_path)
.is_some_and(|root| crate::discovery::resolve_repo_path(&root, relative_path).is_err())
}
fn scout_single_path(
relative_path: &str,
abs_path: &Path,
) -> anyhow::Result<crate::domain::ScoutedEntry> {
#[cfg(test)]
if test_scout_failure_path().lock().as_deref() == Some(relative_path) {
anyhow::bail!("injected scout failure");
}
crate::discovery::scout_single_path(relative_path, abs_path)
}
#[cfg(test)]
pub(crate) fn test_scout_failure_path() -> &'static parking_lot::Mutex<Option<String>> {
static PATH: parking_lot::Mutex<Option<String>> = parking_lot::Mutex::new(None);
&PATH
}
pub(crate) 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,
) -> ReindexOutcome
where
L: Into<Option<LanguageId>>,
{
read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen).outcome
}
fn read_and_index_with_receipt<L>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
) -> ReindexReceipt
where
L: Into<Option<LanguageId>>,
{
read_and_index_with_stable_read_receipt(
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,
) -> ReindexOutcome {
admit_and_index_single_path_with_receipt(relative_path, abs_path, shared, expected_gen).outcome
}
pub(crate) fn admit_and_index_single_path_with_receipt(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
) -> ReindexReceipt {
read_and_index_with_stable_read_receipt(
relative_path,
abs_path,
shared,
None,
expected_gen,
crate::live_index::store::stable_read_file,
)
}
#[cfg(all(feature = "server", not(feature = "embed")))]
#[allow(dead_code)]
pub(crate) fn prepare_snapshot_verify_admission(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
base: Option<&Arc<IndexedFile>>,
) -> Option<SnapshotVerifiedFile> {
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)
|| path_crosses_symlink(abs_path, relative)
|| path_spelling_is_refused(abs_path, relative_path)
{
return None;
}
let mut scouted = scout_single_path(relative_path, abs_path).ok()?;
let prepare = |scouted, bytes| {
prepare_snapshot_verify_from_observation(
relative_path,
scouted,
bytes,
base,
|targets, bytes| classify_for_project(abs_path, relative_path, targets, bytes),
)
};
if catalog_terminal_disposition(&scouted.decision).is_some() {
return prepare(scouted, None);
}
let ScoutDecision::Ingest { .. } = scouted.decision else {
return None;
};
if let Some(root) = project_root_from_paths(abs_path, relative_path)
&& crate::discovery::is_untracked_generated_output_path(&root, relative_path)
{
scouted.decision = ScoutDecision::MetadataOnly {
reason: MetadataOnlyReason::GeneratedOrVendor,
};
return prepare(scouted, None);
}
let bytes = match crate::live_index::store::stable_read_file(abs_path, &scouted.stamp) {
crate::live_index::store::StableReadOutcome::Accepted { bytes, .. } => bytes,
crate::live_index::store::StableReadOutcome::HardSkip { reason } => {
scouted.decision = ScoutDecision::HardSkip { reason };
return prepare(scouted, None);
}
_ => return None,
};
prepare(scouted, Some(bytes))
}
pub(crate) fn prepare_snapshot_verify_from_observation(
relative_path: &str,
scouted: crate::domain::ScoutedEntry,
bytes: Option<Vec<u8>>,
base: Option<&Arc<IndexedFile>>,
classify: impl FnOnce(
crate::domain::IndexTargets,
&[u8],
) -> crate::knowledge::StableContentAdmission,
) -> Option<SnapshotVerifiedFile> {
#[cfg(test)]
if test_scout_failure_path().lock().as_deref() == Some(relative_path) {
return None;
}
let base_hash = base.map(|row| row.content_hash.clone());
let terminal = |scouted, disposition| SnapshotVerifiedFile {
path: relative_path.to_string(),
scouted,
admission: SnapshotVerifiedAdmission::Terminal(disposition),
base_hash: base_hash.clone(),
};
if let Some(disposition) = catalog_terminal_disposition(&scouted.decision) {
return Some(terminal(scouted, disposition));
}
let ScoutDecision::Ingest { targets } = scouted.decision else {
return None;
};
let bytes = bytes?;
if let crate::knowledge::StableContentAdmission::MetadataOnly(reason) =
classify(targets, &bytes)
{
return Some(terminal(scouted, FileDisposition::MetadataOnly { reason }));
}
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);
let file = match base {
Some(row) if row.content_hash == hash::digest_hex(&bytes) => {
let mut row = row.as_ref().clone();
row.mtime_secs = mtime_secs;
row
}
_ => {
let language = scouted.language.unwrap_or(LanguageId::Text);
let result = parsing::process_file_with_classification(
relative_path,
&bytes,
language,
FileClassification::for_indexed_path(relative_path, targets),
);
IndexedFile::from_parse_result(result, bytes).with_mtime(mtime_secs)
}
};
Some(SnapshotVerifiedFile {
path: relative_path.to_string(),
scouted,
admission: SnapshotVerifiedAdmission::Indexed {
file: Box::new(file),
targets,
},
base_hash,
})
}
#[cfg(test)]
pub(crate) fn read_and_index_with_stable_read<L, R>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
stable_read: R,
) -> ReindexOutcome
where
L: Into<Option<LanguageId>>,
R: FnMut(&Path, &crate::domain::FileStamp) -> crate::live_index::store::StableReadOutcome,
{
read_and_index_with_stable_read_receipt(
relative_path,
abs_path,
shared,
language,
expected_gen,
stable_read,
)
.outcome
}
fn read_and_index_with_stable_read_receipt<L, R>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
mut stable_read: R,
) -> ReindexReceipt
where
L: Into<Option<LanguageId>>,
R: FnMut(&Path, &crate::domain::FileStamp) -> crate::live_index::store::StableReadOutcome,
{
let language = language.into();
let mut base = shared.published_generation();
let mut observed_at = PublicationFence::from_published(&base);
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)
|| path_crosses_symlink(abs_path, relative)
|| path_spelling_is_refused(abs_path, relative_path)
{
if observed_at.project_generation != expected_gen {
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
return match shared
.remove_file_at_publication_fence_with_receipt(relative_path, observed_at)
{
FencedRemoval::Removed(published) => {
debug!("watcher: source-scope eviction {relative_path}");
ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published)
}
FencedRemoval::NothingHeld => {
trace!("watcher: source-scope exclusion already reconciled {relative_path}");
ReindexReceipt::observed(ReindexOutcome::Skipped, observed_at)
}
FencedRemoval::Rejected => {
trace!("watcher: source-scope eviction publication rejected {relative_path}");
ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at)
}
};
}
const MAX_PUBLICATION_ATTEMPTS: usize = 4;
for attempt in 1..=MAX_PUBLICATION_ATTEMPTS {
base = shared.published_generation();
observed_at = PublicationFence::from_published(&base);
let expected_index_state_generation = base.health.generation;
let mut scouted = match 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 ReindexReceipt::observed(ReindexOutcome::NotFound, observed_at);
}
warn!("watcher: failed to scout {relative_path}: {error}");
return ReindexReceipt::observed(
ReindexOutcome::ReadError(error.to_string()),
observed_at,
);
}
};
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 let Some(published) = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
expected_index_state_generation,
) {
debug!("watcher: metadata-terminal admission {relative_path}");
return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying metadata-terminal publication");
continue;
}
trace!("watcher: stale metadata-terminal admission rejected: {relative_path}");
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
if let ScoutDecision::Unavailable { stage, kind } = &scouted.decision {
if *stage == crate::domain::AccessStage::Metadata
&& *kind == crate::domain::AccessErrorKind::NotFound
{
return ReindexReceipt::observed(ReindexOutcome::NotFound, observed_at);
}
let disposition = FileDisposition::Unreadable {
stage: *stage,
kind: *kind,
};
if let Some(published) = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
expected_index_state_generation,
) {
return ReindexReceipt::published(
ReindexOutcome::ReadError("single-path scout unavailable".to_string()),
observed_at,
published,
);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying unavailable publication");
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
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 let Some(published) = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
expected_index_state_generation,
) {
return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying generated-output publication");
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
let targets = match &scouted.decision {
ScoutDecision::Ingest { targets } => *targets,
_ => unreachable!("terminal scout decisions return before content ingestion"),
};
let language = language.or(scouted.language).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 let Some(published) = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
disposition,
expected_gen,
expected_index_state_generation,
) {
return ReindexReceipt::published(
ReindexOutcome::Skipped,
observed_at,
published,
);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(
attempt,
"watcher: retrying stable-read hard-skip publication"
);
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
crate::live_index::store::StableReadOutcome::Unreadable { stage, kind } => {
let publication = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::Unreadable { stage, kind },
expected_gen,
expected_index_state_generation,
);
if let Some(published) = publication {
return ReindexReceipt::published(
ReindexOutcome::ReadError("stable read unavailable".to_string()),
observed_at,
published,
);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(
attempt,
"watcher: retrying stable-read unavailable publication"
);
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
crate::live_index::store::StableReadOutcome::UnstableDuringRead => {
let publication = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::UnstableDuringRead,
expected_gen,
expected_index_state_generation,
);
if let Some(published) = publication {
return ReindexReceipt::published(
ReindexOutcome::ReadError("file changed during stable read".to_string()),
observed_at,
published,
);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying unstable-read publication");
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
};
if let crate::knowledge::StableContentAdmission::MetadataOnly(reason) =
classify_for_project(abs_path, relative_path, targets, &bytes)
{
if let Some(published) = shared.publish_terminal_disposition_at_generation(
relative_path,
scouted,
FileDisposition::MetadataOnly { reason },
expected_gen,
expected_index_state_generation,
) {
return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying content-policy publication");
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
let new_hash = hash::digest_hex(&bytes);
{
let index = shared.read();
if let Some(existing) = index.files.get(relative_path)
&& existing.content_hash == new_hash
{
drop(index);
if let Some(published) = shared.publish_hash_skip_at_generation(
relative_path,
mtime_secs,
scouted,
targets,
expected_gen,
expected_index_state_generation,
) {
debug!("watcher: hash-skip {relative_path}");
return ReindexReceipt {
outcome: ReindexOutcome::HashSkip,
observed_at,
published: Some(published),
snapshot_created: false,
};
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying hash-skip publication");
continue;
}
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
}
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 let Some(publication) = shared.publish_indexed_file_at_generation(
relative_path,
indexed,
scouted,
targets,
expected_gen,
expected_index_state_generation,
) {
debug!("watcher: re-indexed {relative_path}");
return ReindexReceipt {
outcome: ReindexOutcome::Reindexed,
observed_at,
published: Some(publication.published),
snapshot_created: publication.snapshot_created,
};
}
if shared.current_project_generation() == expected_gen
&& shared.published_state().generation != expected_index_state_generation
{
trace!(attempt, "watcher: retrying indexed-file publication");
continue;
}
trace!("watcher: stale indexed-file publication rejected: {relative_path}");
return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
}
warn!(
"watcher: aborting {relative_path} after {MAX_PUBLICATION_ATTEMPTS} concurrent publications"
);
ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at)
}
pub fn update_file_from_disk(
shared: &SharedIndex,
repo_root: &Path,
relative_path: &str,
) -> ReindexResult {
let relative = relative_path.replace('\\', "/");
match crate::discovery::resolve_repo_path(repo_root, &relative) {
Ok(Some(_)) => {}
Ok(None) => return ReindexResult::NotFound,
Err(_) => return ReindexResult::Skipped,
}
let abs_path = repo_root.join(&relative);
let expected_gen = shared.current_project_generation();
let outcome = admit_and_index_single_path(&relative, &abs_path, shared, expected_gen)
.into_public_compat();
if matches!(outcome, ReindexResult::Reindexed) {
crate::live_index::index_lifecycle::activation::project_source_authority(repo_root)
.observe_admission_active(&relative);
}
outcome
}
pub(crate) fn reconcile_secret_dismissals(
shared: &SharedIndex,
repo_root: &Path,
) -> std::collections::BTreeMap<String, ReindexOutcome> {
use crate::knowledge::secret_dismissals as dismissals;
let current = dismissals::observe_dismissals(repo_root);
let held = dismissals::held_dismissals(repo_root);
let mut outcomes = std::collections::BTreeMap::new();
if held
.as_ref()
.is_some_and(|held| held.digest == current.digest)
{
return outcomes;
}
let mut paths = current.paths.clone();
match held {
Some(held) => paths.extend(held.paths),
None => paths.extend(shared.read().files.keys().cloned()),
}
let expected_gen = shared.current_project_generation();
for relative in paths.iter().cloned() {
if !matches!(
crate::discovery::resolve_repo_path(repo_root, &relative),
Ok(Some(_))
) {
continue;
}
let abs_path = repo_root.join(&relative);
let outcome = admit_and_index_single_path(&relative, &abs_path, shared, expected_gen);
if matches!(outcome, ReindexOutcome::Reindexed) {
crate::live_index::index_lifecycle::activation::project_source_authority(repo_root)
.observe_admission_active(&relative);
}
outcomes.insert(relative, outcome);
}
let settled = !outcomes
.values()
.any(|outcome| matches!(outcome, ReindexOutcome::PublicationRejected));
let held = if settled {
current
} else {
dismissals::HeldDismissals {
digest: dismissals::UNKNOWN_DISMISSAL_DIGEST.to_string(),
paths,
}
};
dismissals::set_held_dismissals(repo_root, held);
outcomes
}
pub fn remove_file(shared: &SharedIndex, relative_path: &str) -> bool {
let relative = relative_path.replace('\\', "/");
let outcome =
shared.remove_file_outcome_at_generation(&relative, shared.current_project_generation());
if matches!(outcome, FencedRemoval::Removed(_))
&& let Some(root) = shared.read().indexed_root.clone()
{
crate::live_index::index_lifecycle::activation::project_source_authority(&root)
.observe_removal_active(&relative);
}
!matches!(outcome, FencedRemoval::Rejected)
}
pub(crate) fn refuses_observed_admission_into_cold_bootstrap(shared: &SharedIndex) -> bool {
shared.read().refuses_watcher_observed_admission()
}
pub(crate) fn maybe_reindex_observed<L>(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
language: L,
expected_gen: u64,
) -> ReindexOutcome
where
L: Into<Option<LanguageId>>,
{
if refuses_observed_admission_into_cold_bootstrap(shared) {
trace!(
"watcher: refusing observed reindex into cold bootstrap placeholder: {relative_path}"
);
return ReindexOutcome::Skipped;
}
maybe_reindex(relative_path, abs_path, shared, language, expected_gen)
}
#[must_use]
pub(crate) enum FreshenResult {
Fresh,
StaleReindexed,
StaleRemoved,
GenerationMismatch,
PublicationRejected,
}
pub(crate) fn supported_language(path: &Path) -> Option<LanguageId> {
path.to_str().and_then(LanguageId::from_path)
}
pub(crate) fn freshen_file_if_stale(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
authority: &crate::live_index::index_lifecycle::activation::ProjectSourceAuthority,
observer: crate::live_index::index_lifecycle::observer::ObserverId,
) -> FreshenResult {
if shared.current_project_generation() != expected_gen {
if shared.remove_file_at_generation(relative_path, expected_gen)
&& authority.observe_removal(observer, relative_path).is_err()
{
debug!("freshen: stale incarnation's removal observation refused");
}
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 = supported_language(abs_path);
debug!("freshness guard: stale file detected, re-indexing {relative_path}");
let result = maybe_reindex_observed(relative_path, abs_path, shared, language, expected_gen);
if matches!(result, ReindexOutcome::Reindexed)
&& authority
.observe_admission(observer, relative_path)
.is_err()
{
debug!("freshen: stale incarnation's admission observation refused");
}
if matches!(result, ReindexOutcome::Removed)
&& authority.observe_removal(observer, relative_path).is_err()
{
debug!("freshen: stale incarnation's removal observation refused");
}
if shared.current_project_generation() != expected_gen {
if shared.remove_file_at_generation(relative_path, expected_gen)
&& authority.observe_removal(observer, relative_path).is_err()
{
debug!("freshen: stale incarnation's removal observation refused");
}
return FreshenResult::GenerationMismatch;
}
match result {
ReindexOutcome::HashSkip | ReindexOutcome::Reindexed | ReindexOutcome::ReadError(_) => {
FreshenResult::StaleReindexed
}
ReindexOutcome::Skipped => FreshenResult::StaleReindexed,
ReindexOutcome::NotFound | ReindexOutcome::Removed => FreshenResult::StaleRemoved,
ReindexOutcome::PublicationRejected => FreshenResult::PublicationRejected,
}
}
#[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:?}"
);
}
#[test]
fn facade_admission_feeds_the_observation_lane() {
let dir = tempfile::TempDir::new().expect("root");
std::fs::write(dir.path().join("lib.rs"), "pub fn first() {}\n").expect("seed");
let shared = LiveIndex::load(dir.path()).expect("cold load");
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
assert_eq!(authority.committed_observations("extra.rs"), 0);
std::fs::write(dir.path().join("extra.rs"), "pub fn second() {}\n").expect("new file");
assert!(matches!(
update_file_from_disk(&shared, dir.path(), "extra.rs"),
ReindexResult::Reindexed
));
assert_eq!(
authority.committed_observations("extra.rs"),
1,
"an admitted mutation must reach the candidate commit point"
);
assert!(matches!(
update_file_from_disk(&shared, dir.path(), "extra.rs"),
ReindexResult::HashSkip
));
assert_eq!(
authority.committed_observations("extra.rs"),
1,
"a hash-skip observed no change and commits nothing"
);
std::fs::write(dir.path().join("extra.rs"), "pub fn renamed() {}\n").expect("edit");
assert!(matches!(
update_file_from_disk(&shared, dir.path(), "extra.rs"),
ReindexResult::Reindexed
));
assert_eq!(authority.committed_observations("extra.rs"), 2);
assert!(remove_file(&shared, "extra.rs"));
assert_eq!(
authority.committed_observations("extra.rs"),
2,
"a removal rides the accumulator, not the candidate pipeline \
(dark bridging: no removal payload exists yet)"
);
}
#[test]
fn single_path_admission_does_not_follow_symlinks() {
let root = tempfile::TempDir::new().expect("root");
let outside = tempfile::TempDir::new().expect("outside");
std::fs::write(
outside.path().join("secret.txt"),
"placeholder-fixture-value\n",
)
.expect("outside file");
std::fs::create_dir_all(root.path().join("docs")).expect("docs");
std::fs::write(root.path().join("lib.rs"), "pub fn first() {}\n").expect("seed");
let shared = LiveIndex::load(root.path()).expect("cold load");
#[cfg(unix)]
let links = (
std::os::unix::fs::symlink(
outside.path().join("secret.txt"),
root.path().join("docs/setup.txt"),
),
std::os::unix::fs::symlink(outside.path(), root.path().join("linked")),
);
#[cfg(windows)]
let links = (
std::os::windows::fs::symlink_file(
outside.path().join("secret.txt"),
root.path().join("docs/setup.txt"),
),
std::os::windows::fs::symlink_dir(outside.path(), root.path().join("linked")),
);
let mut exercised = Vec::new();
match links.0 {
Ok(()) => exercised.push("docs/setup.txt"),
Err(error) => eprintln!("cannot create a file symlink here ({error}); skipped"),
}
match links.1 {
Ok(()) => exercised.push("linked/secret.txt"),
Err(error) => eprintln!("cannot create a directory symlink here ({error}); skipped"),
}
for relative in exercised {
let outcome = admit_and_index_single_path(
relative,
&root.path().join(relative),
&shared,
shared.current_project_generation(),
);
assert!(
matches!(outcome, ReindexOutcome::Skipped),
"{relative} must be skipped like the cold walk skips it, got {outcome:?}"
);
assert!(
shared.read().get_file(relative).is_none(),
"{relative} was published through a symlink"
);
}
}
fn filesystem_is_case_insensitive(dir: &Path) -> bool {
let probe = dir.join("case_probe.tmp");
std::fs::write(&probe, b"").expect("case probe");
let insensitive = dir.join("CASE_PROBE.TMP").exists();
std::fs::remove_file(&probe).expect("remove case probe");
insensitive
}
#[test]
fn facade_refuses_paths_that_are_not_the_files_own_name_beneath_the_root() {
let root = tempfile::TempDir::new().expect("root");
let outside = tempfile::TempDir::new().expect("outside");
std::fs::write(
outside.path().join("secret.txt"),
"placeholder-fixture-value\n",
)
.expect("outside file");
std::fs::create_dir_all(root.path().join(".aws")).expect("aws dir");
std::fs::write(
root.path().join(".aws/credentials"),
"placeholder-fixture-value\n",
)
.expect("credential fixture");
std::fs::write(root.path().join("lib.rs"), "pub fn first() {}\n").expect("seed");
std::fs::create_dir_all(root.path().join("src")).expect("src dir");
std::fs::write(root.path().join("src/x.rs"), "pub fn x() {}\n").expect("src file");
let shared = LiveIndex::load(root.path()).expect("cold load");
let outside_name = outside
.path()
.file_name()
.expect("tempdir name")
.to_string_lossy()
.into_owned();
let mut refused = vec![
format!("../{outside_name}/secret.txt"),
outside
.path()
.join("secret.txt")
.to_string_lossy()
.into_owned(),
".aws/credentials.".to_string(),
".aws//credentials".to_string(),
".aws/./credentials".to_string(),
"src/./x.rs".to_string(),
];
if filesystem_is_case_insensitive(root.path()) {
refused.push("LIB.RS".to_string());
} else {
eprintln!("case-sensitive filesystem; case alias skipped");
}
for spelling in refused {
let result = update_file_from_disk(&shared, root.path(), &spelling);
assert!(
!matches!(result, ReindexResult::Reindexed | ReindexResult::HashSkip),
"{spelling:?} was admitted: {result:?}"
);
let key = spelling.replace('\\', "/");
assert!(
shared.read().get_file(&key).is_none(),
"{spelling:?} was published into the index"
);
}
}
#[test]
fn single_path_admission_refuses_non_canonical_spellings() {
let root = tempfile::TempDir::new().expect("root");
std::fs::create_dir_all(root.path().join("src")).expect("src");
std::fs::write(root.path().join("src/lib.rs"), "pub fn first() {}\n").expect("seed");
std::fs::write(
root.path().join("src/longer_module_name.rs"),
"pub fn second() {}\n",
)
.expect("long name");
let shared = LiveIndex::load(root.path()).expect("cold load");
let mut aliases = Vec::new();
if filesystem_is_case_insensitive(root.path()) {
aliases.push("SRC/LIB.RS");
} else {
eprintln!("case-sensitive filesystem; case alias skipped");
}
if root.path().join("src/LONGER~1.RS").is_file() {
aliases.push("src/LONGER~1.RS");
} else {
eprintln!("8.3 short names are not generated here; short-name alias skipped");
}
for alias in aliases {
let outcome = admit_and_index_single_path(
alias,
&root.path().join(alias),
&shared,
shared.current_project_generation(),
);
assert!(
matches!(outcome, ReindexOutcome::Skipped),
"{alias} must be skipped, got {outcome:?}"
);
assert!(
shared.read().get_file(alias).is_none(),
"{alias} was published under a second key"
);
}
}
#[test]
fn reindex_receipt_remains_exact_after_later_publication() {
let dir = tempfile::TempDir::new().expect("root");
let path = dir.path().join("lib.rs");
std::fs::write(&path, "pub fn first() {}\n").expect("seed");
let shared = LiveIndex::load(dir.path()).expect("cold load");
let expected_gen = shared.current_project_generation();
std::fs::write(&path, "pub fn second() {}\n").expect("first edit");
let second =
admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
assert_eq!(second.outcome, ReindexOutcome::Reindexed);
assert!(second.snapshot_created);
let second_published = second.published.expect("second publication receipt");
let second_fence = PublicationFence::from_published(&second_published);
let unchanged =
admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
assert_eq!(unchanged.outcome, ReindexOutcome::HashSkip);
assert!(!unchanged.snapshot_created);
let unchanged_published = unchanged.published.expect("hash-skip publication receipt");
assert!(
unchanged_published
.live
.get_file("lib.rs")
.expect("hash-skip file")
.symbols
.iter()
.any(|symbol| symbol.name == "second")
);
std::fs::write(&path, "pub fn third() {}\n").expect("second edit");
let third =
admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
assert_eq!(third.outcome, ReindexOutcome::Reindexed);
let third_published = third.published.expect("third publication receipt");
let third_fence = PublicationFence::from_published(&third_published);
assert_ne!(second_fence, third_fence);
let second_file = second_published
.live
.get_file("lib.rs")
.expect("second file");
assert!(
second_file
.symbols
.iter()
.any(|symbol| symbol.name == "second")
);
assert!(
!second_file
.symbols
.iter()
.any(|symbol| symbol.name == "third")
);
assert!(
!unchanged_published
.live
.get_file("lib.rs")
.expect("immutable hash-skip file")
.symbols
.iter()
.any(|symbol| symbol.name == "third")
);
assert!(
third_published
.live
.get_file("lib.rs")
.expect("third file")
.symbols
.iter()
.any(|symbol| symbol.name == "third")
);
assert!(
shared
.take_pre_update_snapshot_for_publication_at_generation(
"lib.rs",
expected_gen,
second_fence,
)
.is_none(),
"the old receipt must not drain the later publication's snapshot"
);
let third_baseline = shared
.take_pre_update_snapshot_for_publication_at_generation(
"lib.rs",
expected_gen,
third_fence,
)
.expect("third publication owns the current snapshot");
assert!(
third_baseline
.symbols
.iter()
.any(|symbol| symbol.name == "second")
);
}
#[test]
fn bridge_only_publication_does_not_invalidate_live_index_reindex_cas() {
let dir = tempfile::TempDir::new().expect("root");
let path = dir.path().join("lib.rs");
std::fs::write(&path, "pub fn before() {}\n").expect("seed");
let shared = LiveIndex::load(dir.path()).expect("cold load");
let before = shared.published_generation();
let health_generation = before.health.generation;
let prepared = shared.prepare_bridge_rebuild();
assert!(shared.publish_prepared_bridge(prepared));
let bridge_only = shared.published_generation();
assert!(
bridge_only.publication_generation > before.publication_generation,
"bridge publication must advance the full publication bundle"
);
assert_eq!(
bridge_only.health.generation, health_generation,
"bridge publication must retain the live-index health base"
);
std::fs::write(&path, "pub fn after() {}\n").expect("edit");
let receipt = admit_and_index_single_path_with_receipt(
"lib.rs",
&path,
&shared,
shared.current_project_generation(),
);
assert_eq!(receipt.outcome, ReindexOutcome::Reindexed);
assert!(
receipt
.published
.expect("winning reindex publication")
.live
.get_file("lib.rs")
.expect("reindexed file")
.symbols
.iter()
.any(|symbol| symbol.name == "after")
);
}
#[test]
fn stale_not_found_fence_preserves_recreated_file_and_reports_rejection() {
let dir = tempfile::TempDir::new().expect("root");
let path = dir.path().join("lib.rs");
std::fs::write(&path, "pub fn first() {}\n").expect("seed");
let shared = LiveIndex::load(dir.path()).expect("cold load");
std::fs::remove_file(&path).expect("simulate missing observation");
let absence_fence = shared.publication_fence();
std::fs::write(&path, "pub fn recreated() {}\n").expect("recreate");
assert_eq!(
finalize_missing_file(
&shared,
"lib.rs",
&path,
shared.current_project_generation(),
absence_fence,
),
ReindexOutcome::PublicationRejected,
"a rejected stale removal must not claim the file was removed"
);
assert!(
shared
.read()
.get_file("lib.rs")
.expect("last-valid indexed file remains")
.symbols
.iter()
.any(|symbol| symbol.name == "first")
);
assert!(
std::fs::read_to_string(&path)
.expect("recreated disk file")
.contains("recreated")
);
}
#[test]
fn missing_file_finalization_ignores_unrelated_same_project_publication() {
let dir = tempfile::TempDir::new().expect("root");
let a_path = dir.path().join("a.rs");
let b_path = dir.path().join("b.rs");
std::fs::write(&a_path, "pub fn a_before() {}\n").expect("seed a");
std::fs::write(&b_path, "pub fn b_before() {}\n").expect("seed b");
let shared = LiveIndex::load(dir.path()).expect("cold load");
let expected_gen = shared.current_project_generation();
std::fs::remove_file(&a_path).expect("observe a missing");
let absence_fence = shared.publication_fence();
std::fs::write(&b_path, "pub fn b_after() {}\n").expect("edit b");
let b_receipt =
admit_and_index_single_path_with_receipt("b.rs", &b_path, &shared, expected_gen);
assert_eq!(b_receipt.outcome, ReindexOutcome::Reindexed);
let after_b = shared.publication_fence();
assert_eq!(after_b.project_generation, absence_fence.project_generation);
assert_ne!(
after_b.publication_generation, absence_fence.publication_generation,
"the regression requires an intervening unrelated publication"
);
assert_eq!(
finalize_missing_file(&shared, "a.rs", &a_path, expected_gen, absence_fence,),
ReindexOutcome::Removed,
"an unrelated publication must not prevent convergence to disk absence"
);
let published = shared.published_generation();
assert!(published.live.get_file("a.rs").is_none());
assert!(
published
.live
.get_file("b.rs")
.expect("unrelated publication survives")
.symbols
.iter()
.any(|symbol| symbol.name == "b_after")
);
}
#[test]
fn stale_project_generation_cannot_remove_from_rebound_project() {
let project_a = tempfile::TempDir::new().expect("project A");
let project_b = tempfile::TempDir::new().expect("project B");
std::fs::write(project_a.path().join("lib.rs"), "pub fn a() {}\n").unwrap();
std::fs::write(project_b.path().join("lib.rs"), "pub fn b() {}\n").unwrap();
let shared = LiveIndex::load(project_a.path()).expect("load A");
let stale_gen = shared.current_project_generation();
shared.reload(project_b.path()).expect("rebind to B");
let rebound_fence = shared.publication_fence();
assert_eq!(
finalize_missing_file(
&shared,
"lib.rs",
&project_a.path().join("lib.rs"),
stale_gen,
rebound_fence,
),
ReindexOutcome::PublicationRejected
);
assert!(
shared
.read()
.get_file("lib.rs")
.expect("B file survives stale absence finalization")
.symbols
.iter()
.any(|symbol| symbol.name == "b")
);
let publication_before = shared.publication_fence();
let excluded = read_and_index_with_receipt(
".git/config",
&project_b.path().join(".git/config"),
&shared,
None::<LanguageId>,
stale_gen,
);
assert_eq!(excluded.outcome, ReindexOutcome::PublicationRejected);
assert_eq!(shared.publication_fence(), publication_before);
assert!(shared.read().get_file("lib.rs").is_some());
}
}