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 budget = crate::discovery::os_response::current_budget();
let own = crate::discovery::os_response::RetryBudget::new(VANISH_WAIT_MINIMUM);
let delays_ms = [50u64, 200, 500];
for delay_ms in delays_ms {
let remaining = budget.remaining().max(own.remaining());
if remaining.is_zero() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(delay_ms).min(remaining));
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,
)
}
const VANISH_WAIT_MINIMUM: std::time::Duration = std::time::Duration::from_millis(250);
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,
)
}
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;
if !crate::paths::canonical_abs_stays_under_relative_root(abs_path, relative_path) {
return ReindexReceipt::observed(ReindexOutcome::Skipped, observed_at);
}
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,
Unconfirmed(crate::discovery::os_response::OsCause),
UnstampedAbsent,
}
pub(crate) fn supported_language(path: &Path) -> Option<LanguageId> {
path.to_str().and_then(LanguageId::from_path)
}
pub(crate) enum DiskComparison {
Same,
Differs,
GenerationChanged,
Unconfirmed(crate::discovery::os_response::OsCause),
UnstampedAbsent,
}
pub(crate) fn compare_with_disk(
relative_path: &str,
abs_path: &Path,
shared: &SharedIndex,
expected_gen: u64,
) -> DiskComparison {
use crate::discovery::os_response::{AccessFailure, OsClass, current_budget, retry_transient};
let budget = current_budget();
let stat = retry_transient(&budget, Some(abs_path), || {
#[cfg(test)]
if let Some(code) = compare_faults::STAT_ERROR.with(std::cell::Cell::get) {
return Err(AccessFailure::capture(std::io::Error::from_raw_os_error(
code,
)));
}
crate::paths::path_must_be_regular_non_link(abs_path).map_err(AccessFailure::capture)
});
#[cfg(test)]
if let Some(hook) = compare_faults::AFTER_STAT.with(|hook| hook.borrow_mut().take()) {
hook();
}
let metadata = match stat {
Ok(metadata) => Some(metadata),
Err(cause) if cause.class == OsClass::NotFound => None,
Err(cause) => return DiskComparison::Unconfirmed(cause),
};
let indexed = shared
.read()
.get_file(relative_path)
.map(|file| (file.byte_len, file.mtime_secs, file.content_hash.clone()));
if shared.current_project_generation() != expected_gen {
return DiskComparison::GenerationChanged;
}
let (metadata, (byte_len, mtime_secs, content_hash)) = match (metadata, indexed) {
(None, None) => return DiskComparison::Differs,
(None, Some((_, 0, _))) => return DiskComparison::UnstampedAbsent,
(Some(metadata), Some(indexed)) => (metadata, indexed),
_ => return DiskComparison::Differs,
};
let modified = metadata.modified().ok();
let disk_mtime_secs = modified
.and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
.map_or(0, |duration| duration.as_secs());
let filesystem_now = shared.observe_filesystem_time(modified);
if metadata.len() != byte_len || disk_mtime_secs != mtime_secs {
return DiskComparison::Differs;
}
if shared
.verified_stamp(relative_path)
.is_some_and(|stamp| stamp.trusts(byte_len, modified, &content_hash))
{
return DiskComparison::Same;
}
#[cfg(test)]
let limit = compare_faults::HASH_LIMIT
.with(std::cell::Cell::get)
.unwrap_or(crate::domain::index::HARD_SKIP_BYTES);
#[cfg(not(test))]
let limit = crate::domain::index::HARD_SKIP_BYTES;
if byte_len > limit {
return DiskComparison::Unconfirmed(crate::discovery::os_response::OsCause::refused(
"larger than the indexing size limit",
));
}
let bytes = match retry_transient(&budget, Some(abs_path), || {
crate::index_lifecycle::guidance::read_gate::read_regular_file(abs_path)
.map_err(AccessFailure::capture)
}) {
Ok(bytes) => bytes,
Err(cause) if cause.class == OsClass::NotFound => return DiskComparison::Differs,
Err(cause) => return DiskComparison::Unconfirmed(cause),
};
if hash::digest_hex(&bytes) != content_hash {
return DiskComparison::Differs;
}
if shared.current_project_generation() != expected_gen {
return DiskComparison::GenerationChanged;
}
shared.record_verified_stamp(
relative_path,
crate::live_index::store::VerifiedStamp {
size: byte_len,
modified,
content_hash,
filesystem_now,
},
);
DiskComparison::Same
}
#[cfg(test)]
pub(crate) mod compare_faults {
thread_local! {
pub(crate) static STAT_ERROR: std::cell::Cell<Option<i32>> =
const { std::cell::Cell::new(None) };
pub(crate) static AFTER_STAT: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
const { std::cell::RefCell::new(None) };
pub(crate) static HASH_LIMIT: std::cell::Cell<Option<u64>> =
const { std::cell::Cell::new(None) };
}
}
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;
}
if let Some(root) = abs_path
.ancestors()
.nth(std::path::Path::new(relative_path).components().count())
&& crate::index_lifecycle::guidance::read_gate::resolve_repo_path(root, relative_path)
.is_err_and(|message| message.contains("outside the repository"))
{
return FreshenResult::Unconfirmed(crate::discovery::os_response::OsCause::refused(
"resolves outside the repository",
));
}
match compare_with_disk(relative_path, abs_path, shared, expected_gen) {
DiskComparison::Same => return FreshenResult::Fresh,
DiskComparison::GenerationChanged => return FreshenResult::GenerationMismatch,
DiskComparison::Unconfirmed(cause) => return FreshenResult::Unconfirmed(cause),
DiskComparison::UnstampedAbsent => return FreshenResult::UnstampedAbsent,
DiskComparison::Differs => {}
}
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 => FreshenResult::StaleReindexed,
ReindexOutcome::ReadError(message) => FreshenResult::Unconfirmed(
crate::discovery::os_response::recorded_failure(abs_path).unwrap_or_else(|| {
crate::discovery::os_response::classify(&std::io::Error::other(message), None, None)
}),
),
ReindexOutcome::Skipped => FreshenResult::StaleReindexed,
ReindexOutcome::NotFound | ReindexOutcome::Removed => {
shared.forget_verified_stamp(relative_path);
FreshenResult::StaleRemoved
}
ReindexOutcome::PublicationRejected => FreshenResult::PublicationRejected,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::live_index::LiveIndex;
fn freshen_at(shared: &SharedIndex, root: &Path, path: &str, generation: u64) -> FreshenResult {
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(root);
let observer = authority.active_observer();
freshen_file_if_stale(
path,
&root.join(path),
shared,
generation,
&authority,
observer,
)
}
fn set_mtime(path: &Path, time: std::time::SystemTime) {
std::fs::File::options()
.write(true)
.open(path)
.unwrap()
.set_modified(time)
.unwrap();
}
#[test]
fn a_future_dated_file_does_not_end_the_racy_window() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("future.rs"), "fn f() {}\n").unwrap();
std::fs::write(dir.path().join("b.rs"), "fn b() {}\n").unwrap();
let future =
std::time::SystemTime::now() + std::time::Duration::from_secs(5 * 365 * 86_400);
set_mtime(&dir.path().join("future.rs"), future);
let shared = LiveIndex::load(dir.path()).unwrap();
let generation = shared.current_project_generation();
let _ = freshen_at(&shared, dir.path(), "future.rs", generation);
assert!(matches!(
freshen_at(&shared, dir.path(), "b.rs", generation),
FreshenResult::Fresh
));
let stamp = std::fs::metadata(dir.path().join("b.rs"))
.unwrap()
.modified()
.unwrap();
std::fs::write(dir.path().join("b.rs"), "fn c() {}\n").unwrap();
set_mtime(&dir.path().join("b.rs"), stamp);
assert!(
matches!(
freshen_at(&shared, dir.path(), "b.rs", generation),
FreshenResult::StaleReindexed
),
"a same-size edit in the same tick must be found by content"
);
}
#[test]
fn a_retarget_during_the_comparison_confirms_nothing() {
let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
for dir in [&a, &b] {
std::fs::write(dir.path().join("lib.rs"), "pub fn same() {}\n").unwrap();
}
let shared = LiveIndex::load(a.path()).unwrap();
let generation = shared.current_project_generation();
let (retarget, root_b) = (shared.clone(), b.path().to_path_buf());
compare_faults::AFTER_STAT.with(|hook| {
*hook.borrow_mut() = Some(Box::new(move || retarget.reload(&root_b).unwrap()));
});
let result = freshen_at(&shared, a.path(), "lib.rs", generation);
assert!(
matches!(result, FreshenResult::GenerationMismatch),
"the comparison must report the retarget"
);
assert!(
shared.verified_stamp("lib.rs").is_none(),
"no stamp is saved under the new generation"
);
}
#[test]
fn a_save_by_rename_survives_a_spent_call_budget() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("a.rs");
std::fs::write(&path, "pub fn a() {}\n").unwrap();
let shared = LiveIndex::load(dir.path()).unwrap();
let generation = shared.current_project_generation();
std::fs::remove_file(&path).unwrap();
let writer = {
let path = path.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(30));
std::fs::write(path, "pub fn renamed() {}\n").unwrap();
})
};
let result = crate::discovery::os_response::with_spent_call_budget(|| {
freshen_at(&shared, dir.path(), "a.rs", generation)
});
writer.join().unwrap();
assert!(
matches!(result, FreshenResult::StaleReindexed),
"the re-saved file is re-indexed, not evicted"
);
assert!(shared.read().get_file("a.rs").is_some());
}
#[test]
fn a_file_over_the_size_limit_is_not_read_for_its_hash() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("a.rs"), "pub fn a() {}\n").unwrap();
let shared = LiveIndex::load(dir.path()).unwrap();
let generation = shared.current_project_generation();
compare_faults::HASH_LIMIT.with(|limit| limit.set(Some(4)));
let result = freshen_at(&shared, dir.path(), "a.rs", generation);
compare_faults::HASH_LIMIT.with(|limit| limit.set(None));
match result {
FreshenResult::Unconfirmed(cause) => assert!(
cause.describe().contains("size limit"),
"{}",
cause.describe()
),
_ => panic!("a file over the limit must not be confirmed"),
}
}
#[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 javascript_module_aliases_extract_symbols_on_cold_load_and_update() {
let dir = tempfile::TempDir::new().unwrap();
let modules = [
(
"module.cjs",
"function common_before() { return 1; }\nmodule.exports = common_before;\n",
"common_before",
),
(
"module.mjs",
"export function module_before() { return 1; }\n",
"module_before",
),
];
for (path, source, _) in modules {
std::fs::write(dir.path().join(path), source).unwrap();
}
let shared = LiveIndex::load(dir.path()).unwrap();
for (path, source, before) in modules {
{
let index = shared.read();
let file = index.get_file(path).expect("cold module must be indexed");
assert_eq!(file.language, LanguageId::JavaScript);
assert!(file.classification.is_code());
assert!(file.symbols.iter().any(|symbol| symbol.name == before));
}
let after = before.replace("_before", "_after");
std::fs::write(dir.path().join(path), source.replace("_before", "_after")).unwrap();
assert!(matches!(
update_file_from_disk(&shared, dir.path(), path),
ReindexResult::Reindexed
));
let index = shared.read();
let file = index
.get_file(path)
.expect("updated module must be indexed");
assert_eq!(file.language, LanguageId::JavaScript);
assert!(file.symbols.iter().any(|symbol| symbol.name == after));
assert!(!file.symbols.iter().any(|symbol| symbol.name == before));
}
}
#[test]
fn javascript_module_aliases_keep_credential_admission_on_cold_load_and_update() {
for extension in ["cjs", "mjs"] {
let dir = tempfile::TempDir::new().unwrap();
let safe = format!("safe.{extension}");
let guarded = format!("guarded.{extension}");
let canary = ["runtime", "-", "canary", "-", "segment"].concat();
let sensitive = format!("const password = \"{canary}\";\nfunction withheld() {{}}\n");
std::fs::write(dir.path().join(&safe), "function safe_function() {}\n").unwrap();
std::fs::write(dir.path().join(&guarded), &sensitive).unwrap();
let shared = LiveIndex::load(dir.path()).unwrap();
{
let index = shared.read();
assert!(
index.get_file(&guarded).is_none(),
"cold admission must withhold credentials"
);
assert_eq!(
index.get_file(&safe).unwrap().language,
LanguageId::JavaScript
);
}
std::fs::write(dir.path().join(&safe), &sensitive).unwrap();
assert!(matches!(
update_file_from_disk(&shared, dir.path(), &safe),
ReindexResult::Skipped
));
assert!(
shared.read().get_file(&safe).is_none(),
"changed-file admission must evict credentials"
);
}
}
const OUTBOUND_LINK_SECRET: &str = "OUTBOUND_LINK_SECRET_7f3a9c2e";
fn plant_outbound_secret(repo: &Path) -> (tempfile::TempDir, &'static str) {
let outside = tempfile::TempDir::new().expect("outside");
std::fs::write(
outside.path().join("secret.txt"),
format!("{OUTBOUND_LINK_SECRET}\n"),
)
.expect("secret");
#[cfg(unix)]
{
std::os::unix::fs::symlink(outside.path(), repo.join("junc")).expect("dir symlink");
(outside, "junc/secret.txt")
}
#[cfg(windows)]
{
let junction = repo.join("junc");
let status = crate::process_util::hidden_command("cmd")
.args(["/C", "mklink", "/J"])
.arg(&junction)
.arg(outside.path())
.output()
.expect("mklink");
assert!(
status.status.success(),
"mklink /J failed: {}",
String::from_utf8_lossy(&status.stderr)
);
(outside, "junc/secret.txt")
}
}
#[test]
fn outbound_link_single_path_ingest_skips_nested_secret() {
let dir = tempfile::TempDir::new().expect("repo");
std::fs::write(dir.path().join("plain.rs"), "pub fn plain() {}\n").expect("plain");
let (_outside, link_path) = plant_outbound_secret(dir.path());
let shared = LiveIndex::load(dir.path()).expect("load");
let outcome = update_file_from_disk(&shared, dir.path(), link_path);
assert!(
matches!(outcome, ReindexResult::Skipped),
"nested outbound ingest must skip, got {outcome:?}"
);
assert!(
shared.read().get_file(link_path).is_none()
|| shared.read().get_file(link_path).is_some_and(|file| {
!String::from_utf8_lossy(&file.content).contains(OUTBOUND_LINK_SECRET)
}),
"ingest must not publish the outside secret"
);
let plain = update_file_from_disk(&shared, dir.path(), "plain.rs");
assert!(
matches!(plain, ReindexResult::HashSkip | ReindexResult::Reindexed),
"ordinary file must still ingest, got {plain:?}"
);
}
#[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());
}
#[test]
fn outbound_link_freshen_skips_and_does_not_index_secret() {
let dir = tempfile::TempDir::new().expect("repo");
let plain_marker = "OUTBOUND_LINK_PLAIN_ok";
std::fs::write(
dir.path().join("plain.rs"),
format!("fn plain() {{ let _ = \"{plain_marker}\"; }}\n"),
)
.expect("plain");
let (_outside, link_path) = plant_outbound_secret(dir.path());
let shared = LiveIndex::load(dir.path()).expect("load");
let expected_gen = shared.current_project_generation();
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
let outcome = freshen_file_if_stale(
link_path,
&dir.path().join(link_path),
&shared,
expected_gen,
&authority,
authority.active_observer(),
);
assert!(
matches!(outcome, FreshenResult::Unconfirmed(_)),
"outbound path must skip reindex and stay unconfirmed"
);
assert!(
shared.read().get_file(link_path).is_none()
|| shared.read().get_file(link_path).is_some_and(|file| {
!String::from_utf8_lossy(&file.content).contains(OUTBOUND_LINK_SECRET)
}),
"freshen must not index the outside secret"
);
let plain_abs = dir.path().join("plain.rs");
let _ = freshen_file_if_stale(
"plain.rs",
&plain_abs,
&shared,
expected_gen,
&authority,
authority.active_observer(),
);
let guard = shared.read();
let plain = guard.get_file("plain.rs").expect("plain indexed");
assert!(
String::from_utf8_lossy(&plain.content).contains(plain_marker),
"ordinary file must remain readable"
);
}
}