use std::path::PathBuf;
use crate::live_index::single_file::{FreshenResult, freshen_file_if_stale};
use crate::live_index::store::SharedIndex;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TargetedFreshenRefusal {
ProjectGenerationChanged,
PublicationRejected,
}
impl TargetedFreshenRefusal {
pub(crate) fn message(self, relative_path: &str) -> String {
match self {
Self::ProjectGenerationChanged => format!(
"Index refresh interrupted: project changed while refreshing '{relative_path}'; retry the read."
),
Self::PublicationRejected => format!(
"Index refresh interrupted: refresh for '{relative_path}' did not publish; retry the read."
),
}
}
pub(crate) fn permits_authoritative_disk_fallback(self) -> bool {
matches!(self, Self::PublicationRejected)
}
}
pub(crate) fn classify_targeted_freshen_result(
result: FreshenResult,
) -> Result<bool, TargetedFreshenRefusal> {
match result {
FreshenResult::Fresh => Ok(false),
FreshenResult::StaleReindexed | FreshenResult::StaleRemoved => Ok(true),
FreshenResult::GenerationMismatch => Err(TargetedFreshenRefusal::ProjectGenerationChanged),
FreshenResult::PublicationRejected => Err(TargetedFreshenRefusal::PublicationRejected),
FreshenResult::Unconfirmed(_) | FreshenResult::UnstampedAbsent => Ok(false),
}
}
pub(crate) fn safe_repo_path_for_freshen(
repo_root: &std::path::Path,
relative_path: &str,
) -> Result<PathBuf, String> {
let relative = std::path::Path::new(relative_path);
if relative.is_absolute()
|| relative
.components()
.any(|component| matches!(component, std::path::Component::ParentDir))
{
return Err(format!("path '{relative_path}' is outside the repository"));
}
match super::read_gate::resolve_repo_path(repo_root, relative_path)? {
Some(path) => Ok(path),
None => {
let canon_root = repo_root
.canonicalize()
.map_err(|e| format!("cannot resolve repo root: {e}"))?;
Ok(canon_root.join(relative))
}
}
}
pub(crate) const ANSWER_CHECK_FILE_BOUND: usize = 5_000;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct AnswerCheck {
unconfirmed: Vec<(String, String)>,
uncompared: usize,
not_run: Option<&'static str>,
observations_lost: bool,
compared: usize,
provisional: bool,
}
pub(crate) const NOT_RUN_INDEX_NOT_READY: &str = "the index is not ready";
#[cfg_attr(not(feature = "embed"), allow(dead_code))] pub(crate) const NOT_RUN_SHUTTING_DOWN: &str = "the source is shutting down";
#[cfg_attr(not(feature = "embed"), allow(dead_code))] pub(crate) const NOT_RUN_ROOT_REPLACED: &str =
"the repository root was replaced or cannot be reached";
pub(crate) const NOT_RUN_NO_ROOT: &str = "no repository root is bound";
const MAX_NAMED_UNCONFIRMED: usize = 5;
pub(crate) fn answer_was_cut(text: &str) -> bool {
text.contains("budget-limited (was:")
|| text.contains(super::source::CANONICAL_TRUNCATION_MARKER)
|| text.contains("[adaptive verbosity:")
}
pub(crate) const LOST_OBSERVATIONS_LINE: &str =
"Files created since the last scan may be missing; run index_folder to rescan.";
impl AnswerCheck {
pub(crate) fn not_run(reason: &'static str) -> Self {
Self {
unconfirmed: Vec::new(),
uncompared: 0,
not_run: Some(reason),
observations_lost: false,
compared: 0,
provisional: false,
}
}
#[cfg(test)]
pub(crate) fn confirmed_for_tests() -> Self {
Self {
unconfirmed: Vec::new(),
uncompared: 0,
not_run: None,
observations_lost: false,
compared: 1,
provisional: false,
}
}
#[cfg(test)]
pub(crate) fn unconfirmed_for_tests(path: &str, cause: &str) -> Self {
Self {
unconfirmed: vec![(path.to_string(), cause.to_string())],
uncompared: 0,
not_run: None,
observations_lost: false,
compared: 0,
provisional: false,
}
}
pub(crate) fn is_provisional(&self) -> bool {
self.provisional
}
pub(crate) fn uncompared(&self) -> usize {
self.uncompared
}
pub(crate) fn confirmed_label(&self) -> String {
match self.compared {
1 => "1 file confirmed against disk".to_string(),
n => format!("{n} files confirmed against disk"),
}
}
pub(crate) fn all_confirmed(&self) -> bool {
self.unconfirmed.is_empty() && self.uncompared == 0 && self.not_run.is_none()
}
pub(crate) fn notes(&self, claims_complete: bool) -> String {
let mut lines: Vec<String> = self
.unconfirmed
.iter()
.take(MAX_NAMED_UNCONFIRMED)
.map(|(path, cause)| format!("Not confirmed against disk: {path} ({cause})"))
.collect();
if self.unconfirmed.len() > MAX_NAMED_UNCONFIRMED {
lines.push(format!(
"Not confirmed against disk: +{} more files",
self.unconfirmed.len() - MAX_NAMED_UNCONFIRMED
));
}
if let Some(reason) = self.not_run {
lines.push(format!(
"Not confirmed against disk: no file was compared ({reason})"
));
}
if self.uncompared > 0 {
lines.push(format!(
"Not confirmed against disk: {} files in scope beyond the {ANSWER_CHECK_FILE_BOUND}-file check bound; this answer may be incomplete.",
self.uncompared
));
}
if claims_complete && self.observations_lost {
lines.push(LOST_OBSERVATIONS_LINE.to_string());
}
lines.join("\n")
}
pub(crate) fn append_notes(&self, output: &mut String, claims_complete: bool) {
let notes = self.notes(claims_complete);
if !notes.is_empty() {
output.push_str("\n\n");
output.push_str(¬es);
}
}
}
fn observations_lost(shared: &SharedIndex) -> bool {
observed_publication(shared).is_none()
}
pub(crate) fn observed_publication(
shared: &SharedIndex,
) -> Option<std::sync::Arc<crate::live_index::store::PublishedGeneration>> {
let published = shared.published_generation();
(!observations_lost_in(&published.freshness, published.scout_plan.as_deref()))
.then_some(published)
}
fn observations_lost_in(
freshness: &crate::domain::FreshnessStatus,
plan: Option<&crate::discovery::ScoutPlan>,
) -> bool {
use crate::domain::{FreshnessReason, FreshnessStatus, ScoutIssueKind};
let unseen_by_scout = || {
plan.is_none_or(|plan| {
let scouted: std::collections::HashSet<&str> = plan
.entries
.iter()
.map(|entry| entry.path.public_id.as_str())
.collect();
plan.issues.iter().any(|issue| {
matches!(issue.kind, ScoutIssueKind::TraversalCircuitBreaker)
|| issue
.path_id
.as_deref()
.is_none_or(|id| !scouted.contains(id))
})
})
};
match freshness {
FreshnessStatus::Current => false,
FreshnessStatus::Verifying => true,
FreshnessStatus::Degraded { reason_codes, .. } => {
reason_codes.iter().any(|reason| match reason {
FreshnessReason::WatcherUnavailable
| FreshnessReason::SnapshotVerificationFailed
| FreshnessReason::CatalogEntryCapacityExceeded
| FreshnessReason::CatalogMetadataCapacityExceeded => true,
FreshnessReason::ReconciliationPending => unseen_by_scout(),
FreshnessReason::ObservationFailed | FreshnessReason::DerivedPublicationPending => {
false
}
})
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Claim {
Rows,
Whole,
}
impl Claim {
pub(crate) fn whole_if(complete: bool) -> Self {
if complete { Self::Whole } else { Self::Rows }
}
}
pub(crate) struct Rendered<T> {
pub(crate) answer: T,
pub(crate) cited: Vec<String>,
pub(crate) claim: Claim,
}
pub(crate) struct AnswerDisk<'a> {
pub(crate) shared: &'a SharedIndex,
pub(crate) expected_gen: u64,
pub(crate) repo_root: &'a std::path::Path,
pub(crate) authority:
&'a crate::live_index::index_lifecycle::activation::ProjectSourceAuthority,
pub(crate) may_reindex: bool,
pub(crate) scope: Option<&'a str>,
}
pub(crate) const MAX_REINDEX_PER_ANSWER: usize = 32;
const CHANGED_DURING_ANSWER: &str = "changed again while this answer was prepared; retry";
pub(crate) const NOT_REINDEXED_REFRESHING: &str = "the index is refreshing; retry";
const GENERATION_CHANGED: &str = "the project changed during the check; retry";
pub(crate) fn cited_paths<'a>(
live: &crate::live_index::LiveIndex,
paths: impl IntoIterator<Item = &'a str>,
) -> Vec<String> {
let mut cited = std::collections::BTreeSet::new();
for path in paths {
let path = path.trim_start_matches("./");
let spelled = path.replace('\\', "/");
for candidate in [path, spelled.as_str()] {
if live.files.contains_key(candidate) {
cited.insert(candidate.to_string());
break;
}
}
}
cited.into_iter().collect()
}
pub(crate) fn answer_checked<Q, T>(
disk: Result<AnswerDisk<'_>, &'static str>,
mut capture: impl FnMut() -> Q,
mut render: impl FnMut(&Q, &AnswerCheck) -> Rendered<T>,
mut on_reindexed: impl FnMut(),
) -> (Q, T, AnswerCheck) {
let disk = match disk {
Ok(disk) => disk,
Err(reason) => {
let check = AnswerCheck::not_run(reason);
let query = capture();
let answer = render(&query, &check).answer;
return (query, answer, check);
}
};
crate::discovery::os_response::with_call_budget(|| {
let expected_gen = disk.expected_gen;
let provisional = AnswerCheck {
unconfirmed: Vec::new(),
uncompared: 0,
not_run: None,
observations_lost: false,
compared: 0,
provisional: true,
};
let mut confinement = Confinement::new(disk.repo_root);
let mut query = capture();
let first = render(&query, &provisional);
let (paths, uncompared) = paths_to_compare(&disk, &first);
let seen = compare_paths(&disk, &mut confinement, &paths);
let differing: Vec<String> = seen
.iter()
.filter(|(_, seen)| matches!(seen, Seen::Differs))
.map(|(path, _)| path.clone())
.collect();
let mut claim = first.claim;
let mut check = if differing.is_empty() || !disk.may_reindex {
build_check(seen, uncompared, NOT_REINDEXED_REFRESHING, &[])
} else {
let mut differing = differing;
let deferred = differing.split_off(differing.len().min(MAX_REINDEX_PER_ANSWER));
let (changed, mut failed) = reindex_batch(&disk, expected_gen, differing);
failed.extend(
deferred
.into_iter()
.map(|path| (path, NOT_REINDEXED_REFRESHING.to_string())),
);
if changed {
on_reindexed();
}
query = capture();
let again = render(&query, &provisional);
claim = again.claim;
let (paths, uncompared) = paths_to_compare(&disk, &again);
let seen = compare_paths(&disk, &mut confinement, &paths);
build_check(seen, uncompared, CHANGED_DURING_ANSWER, &failed)
};
check.observations_lost = claim == Claim::Whole && observations_lost(disk.shared);
let answer = render(&query, &check).answer;
(query, answer, check)
})
}
fn paths_to_compare<T>(disk: &AnswerDisk<'_>, rendered: &Rendered<T>) -> (Vec<String>, usize) {
paths_to_compare_bounded(disk, rendered, ANSWER_CHECK_FILE_BOUND)
}
fn paths_to_compare_bounded<T>(
disk: &AnswerDisk<'_>,
rendered: &Rendered<T>,
bound: usize,
) -> (Vec<String>, usize) {
let mut paths: std::collections::BTreeSet<String> = rendered.cited.iter().cloned().collect();
let mut uncompared = 0;
if rendered.claim == Claim::Whole {
let prefix = disk
.scope
.filter(|prefix| !prefix.is_empty())
.map(|prefix| {
prefix
.replace('\\', "/")
.trim_start_matches("./")
.to_string()
});
let published = disk.shared.published_generation();
let mut scope: Vec<&String> = published
.live
.all_files()
.map(|(path, _)| path)
.filter(|path| {
prefix
.as_deref()
.is_none_or(|prefix| path.starts_with(prefix))
&& !paths.contains(*path)
})
.collect();
scope.sort_unstable();
let room = bound.saturating_sub(paths.len());
uncompared = scope.len().saturating_sub(room);
paths.extend(scope.into_iter().take(room).cloned());
}
(paths.into_iter().collect(), uncompared)
}
enum Seen {
Same,
Differs,
Unconfirmed(String),
}
struct Confinement {
root: Option<std::path::PathBuf>,
inside: std::collections::HashMap<std::path::PathBuf, bool>,
}
impl Confinement {
fn new(repo_root: &std::path::Path) -> Self {
Self {
root: repo_root.canonicalize().ok(),
inside: std::collections::HashMap::new(),
}
}
fn admits(&mut self, repo_root: &std::path::Path, relative: &str) -> bool {
let Some(root) = &self.root else {
return !super::read_gate::resolve_repo_path(repo_root, relative)
.is_err_and(|message| message.contains("outside the repository"));
};
let Some(parent) = std::path::Path::new(relative)
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
else {
return true;
};
let parent = parent.to_path_buf();
if let Some(&inside) = self.inside.get(&parent) {
return inside;
}
let inside = repo_root
.join(&parent)
.canonicalize()
.map_or(true, |resolved| resolved.starts_with(root));
self.inside.insert(parent, inside);
inside
}
}
fn compare_paths(
disk: &AnswerDisk<'_>,
confinement: &mut Confinement,
paths: &[String],
) -> Vec<(String, Seen)> {
use crate::live_index::single_file::{DiskComparison, compare_with_disk};
paths
.iter()
.map(|path| {
let seen = if !confinement.admits(disk.repo_root, path) {
Seen::Unconfirmed(
crate::discovery::os_response::OsCause::refused(
"resolves outside the repository",
)
.describe(),
)
} else {
match compare_with_disk(
path,
&disk.repo_root.join(path),
disk.shared,
disk.expected_gen,
) {
DiskComparison::Same => Seen::Same,
DiskComparison::GenerationChanged => {
Seen::Unconfirmed(GENERATION_CHANGED.to_string())
}
DiskComparison::Differs => Seen::Differs,
DiskComparison::Unconfirmed(cause) => Seen::Unconfirmed(cause.describe()),
DiskComparison::UnstampedAbsent => Seen::Unconfirmed(
"no modification time was recorded and no file is on disk".to_string(),
),
}
};
(path.clone(), seen)
})
.collect()
}
fn build_check(
seen: Vec<(String, Seen)>,
uncompared: usize,
differs_cause: &str,
failed: &[(String, String)],
) -> AnswerCheck {
let mut check = AnswerCheck {
unconfirmed: Vec::new(),
uncompared,
not_run: None,
observations_lost: false,
compared: 0,
provisional: false,
};
for (path, seen) in seen {
if let Some((_, cause)) = failed.iter().find(|(failed, _)| *failed == path) {
check.unconfirmed.push((path, cause.clone()));
continue;
}
match seen {
Seen::Same => check.compared += 1,
Seen::Differs => check.unconfirmed.push((path, differs_cause.to_string())),
Seen::Unconfirmed(cause) => check.unconfirmed.push((path, cause)),
}
}
check
}
fn reindex_batch(
disk: &AnswerDisk<'_>,
expected_gen: u64,
paths: Vec<String>,
) -> (bool, Vec<(String, String)>) {
use crate::live_index::single_file::prepare_snapshot_verify_admission;
let shared = disk.shared;
let observer = disk.authority.active_observer();
let mut changed = false;
let mut failed = Vec::new();
let mut batch = Vec::new();
let mut single = Vec::new();
{
let live = shared.read();
for path in paths {
let abs_path = disk.repo_root.join(&path);
let prepared = abs_path
.symlink_metadata()
.is_ok()
.then(|| {
prepare_snapshot_verify_admission(
&path,
&abs_path,
shared,
live.files.get(&path),
)
})
.flatten();
match prepared {
Some(prepared) => batch.push(prepared),
None => single.push(path),
}
}
}
if !batch.is_empty() {
let batched: Vec<String> = batch.iter().map(|file| file.path.clone()).collect();
match shared.publish_snapshot_verify_with_observation(
expected_gen,
&[],
batch,
None,
|_, _| false,
|_, scouted| {
scouted.absolute_path.as_deref().is_some_and(|path| {
std::fs::metadata(path).is_ok_and(|meta| {
meta.len() == scouted.stamp.size
&& meta.modified().ok() == scouted.stamp.modified_hint
})
})
},
|| true,
) {
None => failed.extend(
batched
.into_iter()
.map(|path| (path, GENERATION_CHANGED.to_string())),
),
Some(receipt) => {
changed |= !receipt.indexed.is_empty()
|| !receipt.terminal.is_empty()
|| !receipt.superseded.is_empty();
for path in &receipt.indexed {
if disk.authority.observe_admission(observer, path).is_err() {
tracing::debug!("answer check: stale incarnation's admission refused");
}
}
single.extend(receipt.refused);
}
}
}
for path in single {
let abs_path = disk.repo_root.join(&path);
match freshen_file_if_stale(
&path,
&abs_path,
shared,
expected_gen,
disk.authority,
observer,
) {
FreshenResult::Fresh => {}
FreshenResult::StaleReindexed | FreshenResult::StaleRemoved => changed = true,
FreshenResult::GenerationMismatch => failed.push((path, GENERATION_CHANGED.into())),
FreshenResult::PublicationRejected => {
failed.push((path, "refresh did not publish; retry".into()));
}
FreshenResult::Unconfirmed(cause) => failed.push((path, cause.describe())),
FreshenResult::UnstampedAbsent => failed.push((
path,
"no modification time was recorded and no file is on disk".into(),
)),
}
}
(changed, failed)
}
pub(crate) fn budget_with_notes(
check: &AnswerCheck,
claim: Claim,
max_tokens: Option<u64>,
assemble: impl FnOnce(Option<u64>) -> String,
) -> String {
let notes = check.notes(claim == Claim::Whole);
if notes.is_empty() {
return assemble(max_tokens);
}
let reserve = crate::stel::handler::estimate_tokens_from_bytes(notes.len() as u64 + 2);
let body = assemble(max_tokens.map(|max| max.saturating_sub(reserve).max(1)));
let notes = if answer_was_cut(&body) {
check.notes(false)
} else {
notes
};
if notes.is_empty() {
body
} else {
format!("{body}\n\n{notes}")
}
}
pub(crate) fn fit_caveat(body: String, caveat: Option<&str>, max_tokens: Option<u64>) -> String {
let Some(caveat) = caveat.filter(|caveat| !caveat.is_empty()) else {
return body;
};
let reserve = caveat.len() + 2;
let body = match max_tokens.filter(|max| *max > 0) {
Some(max) if body.len() + reserve > (max as usize).saturating_mul(4) => {
let left = (max as usize).saturating_mul(4).saturating_sub(reserve) / 4;
super::source::enforce_token_budget(body, Some(left.max(1) as u64))
}
_ => body,
};
format!("{body}\n\n{caveat}")
}
pub(crate) fn close_targeted_read(
body: String,
caveats: &[String],
max_tokens: Option<u64>,
) -> String {
if caveats.is_empty()
|| super::outcome::is_error_output(&body)
|| super::outcome::is_index_unavailable_output(&body)
{
return body;
}
fit_caveat(body, Some(&caveats.join("\n")), max_tokens)
}
impl<T> Rendered<T> {
pub(crate) fn plain(answer: T) -> Self {
Self {
answer,
cited: Vec::new(),
claim: Claim::Rows,
}
}
}
pub(crate) fn freshen_exact_path(
shared: &SharedIndex,
expected_gen: u64,
repo_root: &std::path::Path,
authority: &crate::live_index::index_lifecycle::activation::ProjectSourceAuthority,
relative_path: &str,
) -> Result<TargetedFreshen, TargetedFreshenRefusal> {
let Ok(abs_path) = safe_repo_path_for_freshen(repo_root, relative_path) else {
return Ok(TargetedFreshen::default());
};
let observer = authority.active_observer();
let result = freshen_file_if_stale(
relative_path,
&abs_path,
shared,
expected_gen,
authority,
observer,
);
let unconfirmed = match &result {
FreshenResult::Unconfirmed(cause) => Some(format!(
"Not confirmed against disk: {relative_path} ({})",
cause.describe()
)),
_ => None,
};
Ok(TargetedFreshen {
changed: classify_targeted_freshen_result(result)?,
unconfirmed,
})
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct TargetedFreshen {
pub(crate) changed: bool,
pub(crate) unconfirmed: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_a_possibly_unseen_file_is_a_lost_observation() {
use crate::discovery::ScoutPlan;
use crate::domain::{
CoverageStatus, FreshnessReason as R, FreshnessStatus, ScoutIssue, ScoutIssueKind,
};
let plan = |issues: Vec<ScoutIssue>| ScoutPlan {
coverage: CoverageStatus::Degraded,
entries: Vec::new(),
issues,
usage: Default::default(),
};
let issue = |path_id: Option<&str>, kind| ScoutIssue {
path_id: path_id.map(str::to_owned),
safe_path: None,
kind,
safe_message: String::new(),
};
let degraded = |reason| FreshnessStatus::Degraded {
last_valid_content_generation: 0,
reason_codes: vec![reason],
};
let clean = plan(Vec::new());
let unseen_directory = plan(vec![issue(
Some("src/hidden"),
ScoutIssueKind::DirectoryEntryUnreadable {
kind: crate::domain::AccessErrorKind::PermissionDenied,
},
)]);
let circuit_breaker = plan(vec![issue(None, ScoutIssueKind::TraversalCircuitBreaker)]);
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("known.rs"), "fn known() {}\n").unwrap();
let scouted = crate::discovery::scout_repository(dir.path()).unwrap();
let known_id = scouted.entries[0].path.public_id.clone();
let with_known = |issues: Vec<ScoutIssue>| ScoutPlan {
entries: scouted.entries.clone(),
..plan(issues)
};
let known_unreadable = with_known(vec![issue(
Some(&known_id),
ScoutIssueKind::DirectoryEntryUnreadable {
kind: crate::domain::AccessErrorKind::PermissionDenied,
},
)]);
let known_and_unseen = with_known(vec![
issue(
Some(&known_id),
ScoutIssueKind::DirectoryEntryUnreadable {
kind: crate::domain::AccessErrorKind::PermissionDenied,
},
),
issue(
Some("src/hidden"),
ScoutIssueKind::DirectoryEntryUnreadable {
kind: crate::domain::AccessErrorKind::PermissionDenied,
},
),
]);
let known_circuit_breaker = with_known(vec![issue(
Some(&known_id),
ScoutIssueKind::TraversalCircuitBreaker,
)]);
let both = |first, second| FreshnessStatus::Degraded {
last_valid_content_generation: 0,
reason_codes: vec![first, second],
};
for (freshness, plan, lost) in [
(
degraded(R::ReconciliationPending),
Some(&known_unreadable),
false,
),
(FreshnessStatus::Verifying, Some(&known_unreadable), true),
(
degraded(R::WatcherUnavailable),
Some(&known_unreadable),
true,
),
(
degraded(R::SnapshotVerificationFailed),
Some(&known_unreadable),
true,
),
(
degraded(R::CatalogEntryCapacityExceeded),
Some(&known_unreadable),
true,
),
(
degraded(R::CatalogMetadataCapacityExceeded),
Some(&known_unreadable),
true,
),
(
both(R::ReconciliationPending, R::WatcherUnavailable),
Some(&known_unreadable),
true,
),
(
degraded(R::ReconciliationPending),
Some(&known_and_unseen),
true,
),
(
degraded(R::ReconciliationPending),
Some(&known_circuit_breaker),
true,
),
(FreshnessStatus::Current, None, false),
(FreshnessStatus::Verifying, Some(&clean), true),
(degraded(R::WatcherUnavailable), Some(&clean), true),
(degraded(R::SnapshotVerificationFailed), Some(&clean), true),
(
degraded(R::CatalogEntryCapacityExceeded),
Some(&clean),
true,
),
(
degraded(R::CatalogMetadataCapacityExceeded),
Some(&clean),
true,
),
(degraded(R::ReconciliationPending), None, true),
(
degraded(R::ReconciliationPending),
Some(&unseen_directory),
true,
),
(
degraded(R::ReconciliationPending),
Some(&circuit_breaker),
true,
),
(degraded(R::ReconciliationPending), Some(&clean), false),
(degraded(R::ObservationFailed), Some(&clean), false),
(degraded(R::DerivedPublicationPending), Some(&clean), false),
] {
assert_eq!(
observations_lost_in(&freshness, plan),
lost,
"{freshness:?} over {plan:?}"
);
}
}
#[cfg(feature = "server")]
#[test]
fn judging_a_publication_takes_no_writer_lock() {
use std::sync::{Arc, Mutex, mpsc};
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("a.rs"), "fn a() {}\n").unwrap();
let shared = crate::live_index::LiveIndex::load(dir.path()).unwrap();
let before = shared.published_generation();
let seen = Arc::new(Mutex::new(None));
let _guard = {
let shared = shared.clone();
let seen = Arc::clone(&seen);
crate::live_index::store::install_reload_mid_commit_hook(move || {
let (sender, receiver) = mpsc::channel();
let reader = shared.clone();
std::thread::spawn(move || {
let _ = sender.send(observed_publication(&reader));
});
*seen.lock().unwrap() =
Some(receiver.recv_timeout(std::time::Duration::from_secs(5)));
})
};
shared.reload(dir.path()).unwrap();
let observed = seen
.lock()
.unwrap()
.take()
.expect("control: the reload reached its commit point")
.expect("the judgment returns while a reload holds the writer lock")
.expect("the published generation lost no observation");
assert_eq!(
observed.publication_generation,
before.publication_generation
);
let (Some(observed_plan), Some(before_plan)) = (&observed.scout_plan, &before.scout_plan)
else {
panic!("both generations carry their scout plan");
};
assert!(
Arc::ptr_eq(observed_plan, before_plan),
"judged with the plan its own generation published"
);
assert!(
!Arc::ptr_eq(before_plan, &shared.scout_plan().unwrap()),
"control: the reload stored a new plan"
);
}
#[test]
fn caveat_lines_are_capped() {
let check = AnswerCheck {
unconfirmed: (0..7)
.map(|n| {
(
format!("src/f{n}.rs"),
"in use by another process".to_string(),
)
})
.collect(),
uncompared: 0,
not_run: None,
observations_lost: false,
compared: 0,
provisional: false,
};
let notes = check.notes(false);
assert_eq!(notes.lines().count(), 6, "{notes}");
assert!(
notes.ends_with("Not confirmed against disk: +2 more files"),
"{notes}"
);
let five = AnswerCheck {
unconfirmed: check.unconfirmed[..5].to_vec(),
..check.clone()
};
assert_eq!(five.notes(false).lines().count(), 5);
}
#[test]
fn an_unconfirmed_file_is_not_a_change() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("a.rs"),
"fn a() {}
",
)
.unwrap();
std::fs::write(
dir.path().join("b.rs"),
"fn b() {}
",
)
.unwrap();
let shared = crate::live_index::LiveIndex::load(dir.path()).unwrap();
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
#[cfg(windows)]
let _block = {
use std::os::windows::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.read(true)
.share_mode(0)
.open(dir.path().join("a.rs"))
.unwrap()
};
#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(
dir.path().join("a.rs"),
std::fs::Permissions::from_mode(0o000),
)
.unwrap();
if std::fs::read(dir.path().join("a.rs")).is_ok() {
return; }
}
let mut changed = false;
let (_, _, check) = answer_checked(
Ok(disk(&shared, dir.path(), &authority)),
|| (),
|_, _| Rendered {
answer: (),
cited: vec!["a.rs".to_string(), "b.rs".to_string()],
claim: Claim::Rows,
},
|| changed = true,
);
assert!(
!check.all_confirmed(),
"control: a.rs was not confirmed: {check:?}"
);
assert!(!changed, "an unread file is not a re-index");
}
fn disk<'a>(
shared: &'a SharedIndex,
root: &'a std::path::Path,
authority: &'a crate::live_index::index_lifecycle::activation::ProjectSourceAuthority,
) -> AnswerDisk<'a> {
AnswerDisk {
shared,
expected_gen: shared.current_project_generation(),
repo_root: root,
authority,
may_reindex: true,
scope: None,
}
}
#[test]
fn a_file_that_changes_again_is_named_not_looped_on() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("a.rs");
std::fs::write(&path, "fn a() {}\n").unwrap();
let shared = crate::live_index::LiveIndex::load(dir.path()).unwrap();
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
std::fs::write(&path, "fn a() { 1 }\n").unwrap();
let mut renders = 0;
let (_, _, check) = answer_checked(
Ok(disk(&shared, dir.path(), &authority)),
|| (),
|_, _| {
renders += 1;
if renders == 2 {
std::fs::write(&path, "fn a() { 22 }\n").unwrap();
}
Rendered {
answer: (),
cited: vec!["a.rs".to_string()],
claim: Claim::Rows,
}
},
|| {},
);
assert_eq!(renders, 3, "first run, one re-run, final render");
assert_eq!(
check.notes(false),
format!("Not confirmed against disk: a.rs ({CHANGED_DURING_ANSWER})")
);
assert!(
shared
.read()
.get_file("a.rs")
.is_some_and(|file| file.content.as_slice() != b"fn a() {}\n")
);
}
#[test]
fn only_a_whole_claim_reaches_past_its_rows() {
let dir = tempfile::tempdir().unwrap();
for name in ["a.rs", "b.rs", "c.rs"] {
std::fs::write(dir.path().join(name), "fn f() {}\n").unwrap();
}
let shared = crate::live_index::LiveIndex::load(dir.path()).unwrap();
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
let disk = disk(&shared, dir.path(), &authority);
let rendered = |claim| Rendered {
answer: (),
cited: vec!["a.rs".to_string()],
claim,
};
assert_eq!(
paths_to_compare_bounded(&disk, &rendered(Claim::Rows), 2),
(vec!["a.rs".to_string()], 0),
"a partial answer compares its rows only"
);
assert_eq!(
paths_to_compare_bounded(&disk, &rendered(Claim::Whole), 2),
(vec!["a.rs".to_string(), "b.rs".to_string()], 1),
"a whole claim fills the bound and counts the rest"
);
}
#[test]
fn one_answer_reindexes_a_bounded_batch() {
const EXTRA: usize = 3;
let dir = tempfile::tempdir().unwrap();
let names: Vec<String> = (0..super::MAX_REINDEX_PER_ANSWER + EXTRA)
.map(|n| format!("f{n:03}.rs"))
.collect();
for name in &names {
std::fs::write(dir.path().join(name), "fn f() {}\n").unwrap();
}
let shared = crate::live_index::LiveIndex::load(dir.path()).unwrap();
let authority =
crate::live_index::index_lifecycle::activation::project_source_authority(dir.path());
for name in &names {
std::fs::write(dir.path().join(name), "fn f() { 1 }\n").unwrap();
}
let (_, _, check) = answer_checked(
Ok(disk(&shared, dir.path(), &authority)),
|| (),
|_, _| Rendered {
answer: (),
cited: names.clone(),
claim: Claim::Rows,
},
|| {},
);
let deferred = check
.unconfirmed
.iter()
.filter(|(_, cause)| cause == NOT_REINDEXED_REFRESHING)
.count();
assert_eq!(deferred, EXTRA, "{check:?}");
assert_eq!(check.unconfirmed.len(), EXTRA, "{check:?}");
}
}