A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
answer @ file completion from a per-session in-memory file index (#3505)
Sebastian Müller6aaa3fa0639c
diff --git a/Cargo.lock b/Cargo.lockindex 6429264f36..efe71a6b05 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -545,6 +545,15 @@ dependencies = ["percent-encoding",]+[[package]]+name = "fsevent-sys"+version = "4.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"+dependencies = [+ "libc",+]+[[package]]name = "futures"version = "0.3.34"@@ -996,6 +1005,26 @@ dependencies = ["rustversion",]+[[package]]+name = "inotify"+version = "0.11.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4cc00ea907cab49550b7da656f80ebb97be1b997d931fbcd28d39734e17ce592"+dependencies = [+ "bitflags",+ "inotify-sys",+ "libc",+]++[[package]]+name = "inotify-sys"+version = "0.1.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c033f80b2c113cdf91ab7a33faa9cbc014726dcad99880c8609af2a370edf37d"+dependencies = [+ "libc",+]+[[package]]name = "instability"version = "0.3.13"@@ -1047,6 +1076,26 @@ dependencies = ["wasm-bindgen",]+[[package]]+name = "kqueue"+version = "1.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8d763e5b24120b4ddf50de6c92308156765aabfbbccebf401da7cff2d70a41ea"+dependencies = [+ "kqueue-sys",+ "libc",+]++[[package]]+name = "kqueue-sys"+version = "1.1.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "07293a4e297ac234359b510362495713f75ea345d5307140414f20c69ffeb087"+dependencies = [+ "bitflags",+ "libc",+]+[[package]]name = "lazy_static"version = "1.5.0"@@ -1162,6 +1211,33 @@ dependencies = ["libc",]+[[package]]+name = "notify"+version = "8.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"+dependencies = [+ "bitflags",+ "fsevent-sys",+ "inotify",+ "kqueue",+ "libc",+ "log",+ "mio",+ "notify-types",+ "walkdir",+ "windows-sys 0.60.2",+]++[[package]]+name = "notify-types"+version = "2.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "42b8cfee0e339a0337359f3c88165702ac6e600dc01c0cc9579a92d62b08477a"+dependencies = [+ "bitflags",+]+[[package]]name = "num-bigint-dig"version = "0.8.6"@@ -1399,6 +1475,7 @@ dependencies = ["crossterm","fancy-regex","ignore",+ "notify","pa-types","proptest","ratatui",@@ -2781,7 +2858,16 @@ version = "0.52.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"dependencies = [- "windows-targets",+ "windows-targets 0.52.6",+]++[[package]]+name = "windows-sys"+version = "0.60.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"+dependencies = [+ "windows-targets 0.53.5",][[package]]@@ -2799,14 +2885,31 @@ version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"dependencies = [- "windows_aarch64_gnullvm",- "windows_aarch64_msvc",- "windows_i686_gnu",- "windows_i686_gnullvm",- "windows_i686_msvc",- "windows_x86_64_gnu",- "windows_x86_64_gnullvm",- "windows_x86_64_msvc",+ "windows_aarch64_gnullvm 0.52.6",+ "windows_aarch64_msvc 0.52.6",+ "windows_i686_gnu 0.52.6",+ "windows_i686_gnullvm 0.52.6",+ "windows_i686_msvc 0.52.6",+ "windows_x86_64_gnu 0.52.6",+ "windows_x86_64_gnullvm 0.52.6",+ "windows_x86_64_msvc 0.52.6",+]++[[package]]+name = "windows-targets"+version = "0.53.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"+dependencies = [+ "windows-link",+ "windows_aarch64_gnullvm 0.53.1",+ "windows_aarch64_msvc 0.53.1",+ "windows_i686_gnu 0.53.1",+ "windows_i686_gnullvm 0.53.1",+ "windows_i686_msvc 0.53.1",+ "windows_x86_64_gnu 0.53.1",+ "windows_x86_64_gnullvm 0.53.1",+ "windows_x86_64_msvc 0.53.1",][[package]]@@ -2815,48 +2918,96 @@ version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"+[[package]]+name = "windows_aarch64_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"+[[package]]name = "windows_aarch64_msvc"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"+[[package]]+name = "windows_aarch64_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"+[[package]]name = "windows_i686_gnu"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"+[[package]]+name = "windows_i686_gnu"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"+[[package]]name = "windows_i686_gnullvm"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"+[[package]]+name = "windows_i686_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"+[[package]]name = "windows_i686_msvc"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"+[[package]]+name = "windows_i686_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"+[[package]]name = "windows_x86_64_gnu"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"+[[package]]+name = "windows_x86_64_gnu"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"+[[package]]name = "windows_x86_64_gnullvm"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"+[[package]]+name = "windows_x86_64_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"+[[package]]name = "windows_x86_64_msvc"version = "0.52.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"+[[package]]+name = "windows_x86_64_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"+[[package]]name = "winnow"version = "0.7.15"diff --git a/crates/pa-tui/Cargo.toml b/crates/pa-tui/Cargo.tomlindex cf55d4174e..0edc16c2c6 100644--- a/crates/pa-tui/Cargo.toml+++ b/crates/pa-tui/Cargo.toml@@ -16,6 +16,7 @@ fancy-regex = "0.14"url = "2"regex = "1"ignore = "0.4"+notify = "8"serde = { version = "1", features = ["derive"] }serde_json = { workspace = true }anyhow = "1"diff --git a/crates/pa-tui/src/autocomplete.rs b/crates/pa-tui/src/autocomplete.rsindex 0f206d4cb6..9bcce45acb 100644--- a/crates/pa-tui/src/autocomplete.rs+++ b/crates/pa-tui/src/autocomplete.rs@@ -10,6 +10,7 @@ use crate::width::str_width;use crate::{Line, Span};use pa_types::slash_commands::SlashCommandRegistry;+mod file_index;mod fuzzy_file_search;pub use fuzzy_file_search::FileSearch;@@ -738,6 +739,8 @@ pub struct CombinedAutocompleteProvider {/// command's argument position (e.g. the `/tier` tier choices).arguments: std::collections::HashMap<&'static str, Vec<CompletionItem>>,paths: PathCompletionProvider,+ /// The `@` file search's in-memory file index (one per session view).+ file_index: file_index::FileIndex,}impl CombinedAutocompleteProvider {@@ -772,6 +775,7 @@ impl CombinedAutocompleteProvider {hidden: HashSet::default(),arguments: HashMap::default(),paths: PathCompletionProvider { base },+ file_index: file_index::FileIndex::new(),}}@@ -896,6 +900,7 @@ impl AutocompleteProvider for CombinedAutocompleteProvider {if let Some(at_prefix) = extract_at_prefix(&before) {return Some(SuggestionLookup::Searching(fuzzy_file_search::spawn(&self.paths.base,+ self.file_index.clone(),at_prefix,)));}diff --git a/crates/pa-tui/src/autocomplete/file_index.rs b/crates/pa-tui/src/autocomplete/file_index.rsnew file mode 100644index 0000000000..d4ea4ac49b--- /dev/null+++ b/crates/pa-tui/src/autocomplete/file_index.rs@@ -0,0 +1,920 @@+//! The session file index: an in-memory mirror of the `@` completion's walk results+//! under the session base — fd printed-form paths, relative to the base, directories+//! keeping their trailing `/` — so every query after the first answers from memory+//! instead of walking the tree.+//!+//! The first `@` query builds it with one full walk (fd's rules, no result cut), a+//! watcher started before that walk buffers the changes it races with, and one applier+//! thread keeps the entries current afterwards: batches applied by each path's current+//! existence (event kinds differ per editor and platform), the shared walker deciding+//! what may enter. Queries the index cannot serve — an absolute or `..` scope, a typed+//! directory the walk rules exclude, a watcher that failed, or a tree over the entry+//! cap — fall back to the per-query walk, which stays.+//!+//! Limitations: changes inside symlinked directories whose targets sit outside the+//! base are not observed (the build-time view stands until a rebuild); `.git/info/exclude`+//! and global gitignore edits are not observed; Linux spends one inotify watch per+//! directory and falls back to the walk when the watch limit rejects the base.++use std::collections::{BTreeMap, BTreeSet, HashSet};+use std::num::NonZeroUsize;+use std::ops::Range;+use std::path::{Path, PathBuf};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::sync::mpsc::{self, Receiver};+use std::sync::{Arc, Condvar, Mutex, PoisonError, Weak};+use std::thread;++use ignore::WalkState;+use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};++use super::fuzzy_file_search::{+ fd_printed_form, suggestions, to_display_path, top_ranked, walk_builder, AtSearch, RankedForm,+ GIT_DIR,+};+use super::Suggestions;++/// The entry cap. Measured on 202k- and 950k-entry synthetic trees: the built index costs+/// ~50-60 bytes per entry of resident memory (RSS delta), so a capped index holds ~60 MiB.+pub(super) const MAX_INDEX_ENTRIES: usize = 1_000_000;++/// The fewest candidates one scan thread takes: a smaller slice scans on the search+/// thread itself. Measured on 8- and 2-core x86 VMs over 5k–202k-entry trees: `4_096` is+/// the smallest value whose split scan is never slower than the inline scan (`2_048` lost+/// up to 46% on 2 cores near 5k entries).+pub(super) const SCAN_CHUNK_MIN: usize = 4_096;++/// How one query addresses the index.+pub(super) enum IndexScope {+ /// No typed scope: every entry is a candidate.+ Unscoped,+ /// The typed scope as an index prefix (relative, no `..`): the candidates are the+ /// entries under it, relative to it. The empty prefix is the base itself.+ Prefix(String),+ /// The typed scope can never be served by the index (absolute, `~`, or `..`).+ Outside,+}++/// The index's answer for one query: the ranked suggestions when it served the+/// query (`None` inside `Served` is an empty menu), or a walk when it did not.+pub(super) enum Answer {+ Served(Option<Suggestions>),+ Walk,+}++/// The session's file index, one per autocomplete provider: lazily built on the first+/// `@` query, then shared by every query thread. Cheap to clone; the last clone drops+/// the index and its watcher.+#[derive(Clone)]+pub(super) struct FileIndex {+ shared: Arc<Shared>,+}++/// The shared index state and its build-finished signal.+struct Shared {+ state: Mutex<IndexState>,+ ready: Condvar,+}++/// The index lifecycle. `Unbuilt` builds on the next query; `Building` makes later+/// queries wait instead of starting their own walks; `Disabled` walks per query for+/// the rest of the provider's life.+enum IndexState {+ Unbuilt,+ Building,+ Ready {+ /// Sorted, no duplicates: a scope's entries are one contiguous slice.+ entries: Vec<Box<str>>,+ _watcher: RecommendedWatcher,+ },+ Disabled,+}++enum Step {+ Build,+ Wait,+ Serve,+ Walk,+}++impl FileIndex {+ pub(super) fn new() -> Self {+ Self {+ shared: Arc::new(Shared {+ state: Mutex::new(IndexState::Unbuilt),+ ready: Condvar::new(),+ }),+ }+ }++ fn lock(&self) -> std::sync::MutexGuard<'_, IndexState> {+ self.shared+ .state+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ }++ /// Answer one query from the index: build on first use, wait out a concurrent+ /// build, then rank the matched entries (under the lock — the candidates borrow+ /// the entries). `Answer::Served` is the index's own result (`None` inside it an+ /// empty menu); `Answer::Walk` means the index does not serve the query — it is+ /// disabled, or the scope sits outside it.+ pub(super) fn query(&self, base: &Path, scope: &IndexScope, search: &AtSearch<'_>) -> Answer {+ let mut guard = self.lock();+ loop {+ let step = match &*guard {+ IndexState::Unbuilt => Step::Build,+ IndexState::Building => Step::Wait,+ IndexState::Ready { .. } => Step::Serve,+ IndexState::Disabled => Step::Walk,+ };+ match step {+ Step::Wait => {+ guard = self+ .shared+ .ready+ .wait(guard)+ .unwrap_or_else(PoisonError::into_inner);+ }+ Step::Build => {+ drop(guard);+ self.build(base, MAX_INDEX_ENTRIES);+ guard = self.lock();+ }+ Step::Walk => return Answer::Walk,+ Step::Serve => {+ let IndexState::Ready { entries, .. } = &*guard else {+ return Answer::Walk;+ };+ let prefix = match scope {+ // The typed directory is not in the index (the walk rules+ // exclude it): the walk lists its contents instead.+ IndexScope::Outside => return Answer::Walk,+ IndexScope::Prefix(prefix)+ if !prefix.is_empty() && !contains(entries, prefix) =>+ {+ return Answer::Walk;+ }+ IndexScope::Prefix(prefix) => prefix.as_str(),+ IndexScope::Unscoped => "",+ };+ return Answer::Served(suggestions(scan(entries, prefix, search), search));+ }+ }+ }+ }++ /// Build from `Unbuilt`: one full walk under `base`, at most `cap` entries, with the+ /// watcher started before it so changes during the walk are applied after. `true`+ /// when the state is `Ready` on return; a watcher that fails or a tree over the cap+ /// leaves `Disabled`.+ pub(super) fn build(&self, base: &Path, cap: usize) -> bool {+ {+ let mut guard = self.lock();+ if !matches!(&*guard, IndexState::Unbuilt) {+ return matches!(&*guard, IndexState::Ready { .. });+ }+ *guard = IndexState::Building;+ }+ let Some((watcher, events)) = watch(base) else {+ self.replace(IndexState::Disabled);+ return false;+ };+ let canonical_base = base.canonicalize().unwrap_or_else(|_| base.to_path_buf());+ drop(spawn_applier(+ Arc::downgrade(&self.shared),+ events,+ base.to_path_buf(),+ canonical_base,+ ));+ if let Some(entries) = walk_index_entries(base, cap) {+ self.replace(IndexState::Ready {+ entries,+ _watcher: watcher,+ });+ true+ } else {+ self.replace(IndexState::Disabled);+ false+ }+ }++ /// Swap in a new state and wake every waiter.+ fn replace(&self, state: IndexState) {+ let mut guard = self.lock();+ *guard = state;+ self.shared.ready.notify_all();+ }+}++/// The query's top ranks over the built index: the entries under `prefix` (empty for+/// the whole index) matched and scored scope-relative, the slice split across scan+/// threads when it is large enough to pay for them.+fn scan<'entries>(+ entries: &'entries [Box<str>],+ prefix: &str,+ search: &AtSearch<'_>,+) -> Vec<RankedForm<'entries>> {+ // The scope's own directory entry is never a candidate — the walk skips its own root.+ let candidates = &entries[range_under(entries, prefix)];+ let base = search.walk_base.to_string_lossy();+ let scan_chunk = |chunk: &'entries [Box<str>], search: &AtSearch<'_>| {+ let mut target = String::new();+ top_ranked(+ chunk+ .iter()+ .take_while(|_| !search.cancel.load(Ordering::Relaxed))+ .map(|form| &form[prefix.len()..])+ .filter(|form| search.matches(&base, form, &mut target))+ .filter_map(|form| search.score(form)),+ )+ };+ // `available_parallelism` reads cgroup files on Linux: only a slice worth splitting+ // pays for it.+ let chunks = match candidates.len() / SCAN_CHUNK_MIN {+ 0 | 1 => 1,+ wanted => wanted.min(thread::available_parallelism().map_or(1, NonZeroUsize::get)),+ };+ if chunks == 1 {+ return scan_chunk(candidates, search);+ }+ let scan_chunk = &scan_chunk;+ thread::scope(|scope| {+ let handles: Vec<_> = candidates+ .chunks(candidates.len().div_ceil(chunks))+ .map(|chunk| {+ scope.spawn(move || {+ // A shared regex serves its lock-free cache only to the first thread+ // that used it; other threads take its pool's mutex on every match. A+ // clone brings its own pool.+ let regex = search.regex.cloned();+ scan_chunk(+ chunk,+ &AtSearch {+ regex: regex.as_ref(),+ ..*search+ },+ )+ })+ })+ .collect();+ top_ranked(handles.into_iter().flat_map(|handle| {+ handle+ .join()+ .unwrap_or_else(|panic| std::panic::resume_unwind(panic))+ }))+ })+}++/// The recursive watcher over `base`: its events flow into the returned channel.+fn watch(base: &Path) -> Option<(RecommendedWatcher, Receiver<notify::Result<Event>>)> {+ let (tx, events) = mpsc::channel();+ let mut watcher = notify::recommended_watcher(tx).ok()?;+ watcher.watch(base, RecursiveMode::Recursive).ok()?;+ Some((watcher, events))+}++/// One full walk under `base` with fd's rules — no result cut, no cancel: a build+/// completes even when the typing that started it moved on. `None` when the tree holds+/// more than `cap` entries.+fn walk_index_entries(base: &Path, cap: usize) -> Option<Vec<Box<str>>> {+ let count = AtomicUsize::new(0);+ let sink = Mutex::new(Vec::new());+ walk_builder(base).build_parallel().run(|| {+ let count = &count;+ let mut collected = ThreadEntries {+ entries: Vec::new(),+ sink: &sink,+ };+ Box::new(move |entry| {+ let Ok(entry) = entry else {+ return WalkState::Continue;+ };+ let Some(form) = fd_printed_form(&entry, base) else {+ return WalkState::Continue;+ };+ if count.fetch_add(1, Ordering::Relaxed) >= cap {+ return WalkState::Quit;+ }+ collected.entries.push(Box::from(form));+ WalkState::Continue+ })+ });+ if count.into_inner() > cap {+ return None;+ }+ let buffers = sink.into_inner().unwrap_or_else(PoisonError::into_inner);+ let mut entries = Vec::with_capacity(buffers.iter().map(Vec::len).sum());+ for buffer in buffers {+ entries.extend(buffer);+ }+ // Walk paths are unique: sorting alone yields the index order.+ entries.sort_unstable();+ Some(entries)+}++/// One walker thread's build entries: collected without a lock, handed to the shared+/// sink as a whole buffer when the thread's visitor drops at the end of the walk — the+/// buffers merge once, into an exact-capacity index, instead of growing one shared vec.+struct ThreadEntries<'sink> {+ entries: Vec<Box<str>>,+ sink: &'sink Mutex<Vec<Vec<Box<str>>>>,+}++impl Drop for ThreadEntries<'_> {+ fn drop(&mut self) {+ self.sink+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .push(std::mem::take(&mut self.entries));+ }+}++/// The one watcher-draining thread: each batch — everything queued at a wake-up, the+/// coalescing — applies to the index under the lock, and a batch that arrives during a+/// build waits for the build to finish first. Holding the channel's receiver and only+/// a weak index reference, it exits when the watcher drops with the provider.+fn spawn_applier(+ shared: Weak<Shared>,+ events: Receiver<notify::Result<Event>>,+ base: PathBuf,+ canonical_base: PathBuf,+) -> thread::JoinHandle<()> {+ thread::spawn(move || {+ while let Ok(first) = events.recv() {+ let Some(shared) = shared.upgrade() else {+ return;+ };+ let mut batch = vec![first];+ while let Ok(event) = events.try_recv() {+ batch.push(event);+ }+ let mut guard = shared.state.lock().unwrap_or_else(PoisonError::into_inner);+ loop {+ match &*guard {+ IndexState::Building => {+ guard = shared+ .ready+ .wait(guard)+ .unwrap_or_else(PoisonError::into_inner);+ }+ IndexState::Ready { .. } => {+ apply_batch(&mut guard, &batch, &base, &canonical_base);+ #[cfg(test)]+ shared.ready.notify_all();+ // Invalidation drops this watcher's ownership. Exit while+ // still holding the lock, before a query can rebuild and+ // expose its new state to any old queued callbacks.+ if !matches!(*guard, IndexState::Ready { .. }) {+ return;+ }+ break;+ }+ // The watcher that fed this batch is gone: the state is a+ // rebuilt or fallback one, and nothing applies to it.+ IndexState::Unbuilt | IndexState::Disabled => return,+ }+ }+ }+ })+}++/// Apply one watcher batch using each path's current existence. A structural event on+/// an indexed directory re-walks its subtree; ordinary file saves keep the typed-form+/// fast path. A watcher error disables the index; rescan notices, a structural event on+/// the base or a `.git` marker, an event on a `.git` file, or changed ignore rules+/// trigger a lazy rebuild. A batch exceeding [`MAX_INDEX_ENTRIES`] disables the index.+fn apply_batch(+ state: &mut IndexState,+ batch: &[notify::Result<Event>],+ base: &Path,+ canonical_base: &Path,+) {+ if batch.iter().any(Result::is_err) {+ *state = IndexState::Disabled;+ return;+ }+ let mut rels: BTreeSet<String> = BTreeSet::new();+ let mut replaced: BTreeSet<String> = BTreeSet::new();+ for event in batch.iter().flatten() {+ if event.need_rescan() {+ *state = IndexState::Unbuilt;+ return;+ }+ // Reads change nothing the index tracks — and the walker's own+ // directory opens (plus any search over the session tree) arrive as+ // an access flood, on Linux one event per opened directory.+ if event.kind.is_access() {+ continue;+ }+ for raw_rel in event+ .paths+ .iter()+ .filter_map(|path| event_rel(path, base, canonical_base))+ {+ let rel = to_display_path(&raw_rel.to_string_lossy());+ // Display keys normalize separators and replace invalid UTF-8. They+ // cannot locate every real filename (or distinguish all collisions).+ // Rebuild through the shared walker instead of mutating a wrong key.+ if Path::new(&rel) != raw_rel {+ *state = IndexState::Unbuilt;+ return;+ }+ // A directory's name event may be the only event for an entire+ // moved subtree. Its current existence cannot prove it is still+ // the indexed directory, especially after coalesced replacements.+ if matches!(+ event.kind,+ notify::EventKind::Create(_)+ | notify::EventKind::Remove(_)+ | notify::EventKind::Modify(+ notify::event::ModifyKind::Name(_)+ | notify::event::ModifyKind::Any+ | notify::event::ModifyKind::Other,+ )+ | notify::EventKind::Any+ | notify::EventKind::Other+ ) {+ replaced.insert(rel.clone());+ }+ rels.insert(rel);+ }+ }+ // Creating/removing a repository boundary changes whether gitignore+ // applies. A worktree's .git file may also change its gitdir pointer.+ // Directory metadata alone does not change the boundary, and ordinary+ // Git activity can emit those events. Check before pruning .git contents.+ if rels.iter().any(|rel| {+ rel.rsplit('/').next() == Some(GIT_DIR)+ && (replaced.contains(rel) || base.join(rel).is_file())+ }) {+ *state = IndexState::Unbuilt;+ return;+ }+ let rels: Vec<String> = rels+ .iter()+ .filter(|rel| !rel.split('/').any(|component| component == GIT_DIR))+ .cloned()+ .collect();+ if rels.iter().any(|rel| is_ignore_rules_name(rel)) {+ *state = IndexState::Unbuilt;+ return;+ }+ let IndexState::Ready { entries, .. } = state else {+ return;+ };+ // Every decision reads the batch's starting entries; the edits apply once per+ // batch, because each single edit of the sorted entries shifts their tail.+ let mut removals: Vec<Range<usize>> = Vec::new();+ let mut gone: Vec<String> = Vec::new();+ let mut rewalked: BTreeSet<&str> = BTreeSet::new();+ let mut inserted: BTreeMap<String, Vec<(String, bool)>> = BTreeMap::new();+ for rel in &rels {+ // An ancestor's scheduled walk already observes every child change;+ // do not walk or count them again. Check each component boundary: a+ // sibling such as dir-other can sort between dir and dir/nested.+ if rel+ .match_indices('/')+ .any(|(index, _)| rewalked.contains(&rel[..index]))+ {+ continue;+ }+ let metadata = std::fs::metadata(base.join(rel));+ let Ok(metadata) = metadata else {+ gone.push(rel.clone());+ continue;+ };+ let is_directory = metadata.is_dir();+ let form = if is_directory {+ format!("{rel}/")+ } else {+ rel.clone()+ };+ // Replacing an indexed directory may change every descendant without+ // child events. Only structural events re-walk its subtree (the base is the+ // whole tree: it rebuilds lazily); ordinary editor saves and directory+ // metadata changes remain no-ops.+ if is_directory && replaced.contains(rel) {+ if rel.is_empty() {+ *state = IndexState::Unbuilt;+ return;+ }+ if contains(entries, &form) {+ removals.push(range_under(entries, &form));+ rewalked.insert(rel);+ continue;+ }+ }+ // The typed-form no-op: the common editor save changes nothing.+ if contains(entries, &form) {+ continue;+ }+ let parent = parent_form(rel);+ // The parent is neither the base nor an indexed directory: the path sits+ // under an ignored or pruned tree, which the index never holds.+ if !parent.is_empty() && !contains(entries, &parent) {+ continue;+ }+ // A stale entry of the other type drops before the rule check: a directory+ // replaced by a file takes its descendants with it.+ if is_directory {+ removals.extend(find(entries, rel).map(|index| index..index + 1));+ } else {+ remove_subtree(entries, rel, &mut removals);+ }+ inserted+ .entry(parent)+ .or_default()+ .push((rel.clone(), is_directory));+ }+ for rel in &gone {+ if rel.is_empty() {+ // The base itself is gone: rebuild on the next query (into an empty+ // or failing walk) instead of serving the dead tree.+ *state = IndexState::Unbuilt;+ return;+ }+ remove_subtree(entries, rel, &mut removals);+ }+ remove_ranges(entries, removals);+ let mut additions: Vec<Box<str>> = Vec::new();+ for rel in rewalked {+ if !walk_subtree(base, rel, entries.len(), &mut additions) {+ *state = IndexState::Disabled;+ return;+ }+ }+ for (parent, paths) in inserted {+ // One depth-1 walk per parent per batch: the shared rules decide what may enter.+ let parent_dir = base.join(&parent);+ let listed: Vec<String> = walk_builder(&parent_dir)+ .max_depth(Some(1))+ .build()+ .filter_map(Result::ok)+ .filter_map(|entry| fd_printed_form(&entry, &parent_dir))+ .collect();+ let listed: HashSet<&str> = listed.iter().map(String::as_str).collect();+ for (rel, is_directory) in paths {+ let name = rel+ .trim_end_matches('/')+ .rsplit('/')+ .next()+ .unwrap_or_default();+ let name_form = if is_directory {+ format!("{name}/")+ } else {+ name.to_string()+ };+ if !listed.contains(name_form.as_str()) {+ continue;+ }+ additions.push(Box::from(if is_directory {+ format!("{rel}/")+ } else {+ rel.clone()+ }));+ if is_directory && !walk_subtree(base, &rel, entries.len(), &mut additions) {+ *state = IndexState::Disabled;+ return;+ }+ }+ }+ if !additions.is_empty() {+ entries.extend(additions);+ // The stable sort takes the sorted entries as one run, so the merge stays linear.+ entries.sort();+ entries.dedup();+ }+ if entries.len() > MAX_INDEX_ENTRIES {+ *state = IndexState::Disabled;+ }+}++/// Walk the directory `rel`'s subtree with the shared rules into `additions` — `false`+/// once the index (`indexed` entries plus the additions) passes [`MAX_INDEX_ENTRIES`]:+/// an over-cap batch disables the index, like an over-cap build does.+fn walk_subtree(base: &Path, rel: &str, indexed: usize, additions: &mut Vec<Box<str>>) -> bool {+ for form in walk_builder(&base.join(rel))+ .build()+ .filter_map(Result::ok)+ .filter_map(|entry| fd_printed_form(&entry, base))+ {+ additions.push(Box::from(form));+ if indexed + additions.len() > MAX_INDEX_ENTRIES {+ return false;+ }+ }+ true+}++/// The event path relative to the base — `None` outside it. Watcher paths may be+/// canonical (macOS reports `/private/var` for a `/var` tempdir), so both the base and+/// its canonical form are stripped.+fn event_rel<'a>(path: &'a Path, base: &Path, canonical_base: &Path) -> Option<&'a Path> {+ path.strip_prefix(base)+ .or_else(|_| path.strip_prefix(canonical_base))+ .ok()+}++/// The parent directory's index form: `""` for the base, else `a/b/`.+fn parent_form(rel: &str) -> String {+ match rel.rfind('/') {+ Some(index) => rel[..=index].to_string(),+ None => String::new(),+ }+}++/// An ignore-rules file: editing one rewrites what the walk lists.+fn is_ignore_rules_name(rel: &str) -> bool {+ matches!(+ rel.trim_end_matches('/').rsplit('/').next(),+ Some(".gitignore" | ".ignore")+ )+}++/// Whether the sorted entries hold `form`.+fn contains(entries: &[Box<str>], form: &str) -> bool {+ find(entries, form).is_some()+}++/// The position of `form` in the sorted entries.+fn find(entries: &[Box<str>], form: &str) -> Option<usize> {+ entries.binary_search_by(|entry| (**entry).cmp(form)).ok()+}++/// The sorted entries' range of forms under `prefix`, `prefix`'s own entry excluded;+/// the empty prefix spans every entry.+fn range_under(entries: &[Box<str>], prefix: &str) -> Range<usize> {+ let start = entries.partition_point(|entry| **entry <= *prefix);+ start..start + entries[start..].partition_point(|entry| entry.starts_with(prefix))+}++/// Mark a path's file form, its directory form, and every entry under it for removal.+fn remove_subtree(entries: &[Box<str>], rel: &str, removals: &mut Vec<Range<usize>>) {+ let prefix = format!("{rel}/");+ removals.extend(find(entries, rel).map(|index| index..index + 1));+ removals.extend(find(entries, &prefix).map(|index| index..index + 1));+ removals.push(range_under(entries, &prefix));+}++/// Drop the marked ranges of the entries in one pass.+fn remove_ranges(entries: &mut Vec<Box<str>>, mut removals: Vec<Range<usize>>) {+ removals.retain(|range| !range.is_empty());+ if removals.is_empty() {+ return;+ }+ removals.sort_unstable_by_key(|range| range.start);+ let mut removals = removals.into_iter().peekable();+ let mut index = 0;+ entries.retain(|_| {+ while removals.next_if(|range| range.end <= index).is_some() {}+ let removed = removals.peek().is_some_and(|range| range.start <= index);+ index += 1;+ !removed+ });+}++#[cfg(test)]+mod freshness_tests {+ use super::*;+ use notify::event::{CreateKind, ModifyKind, RemoveKind, RenameMode};++ fn snapshot(base: &Path) -> IndexState {+ IndexState::Ready {+ entries: walk_index_entries(base, MAX_INDEX_ENTRIES).expect("walk"),+ _watcher: notify::recommended_watcher(|_: notify::Result<Event>| {}).expect("watcher"),+ }+ }++ fn assert_matches_live_walk(state: &IndexState, base: &Path) {+ let expected = walk_index_entries(base, MAX_INDEX_ENTRIES).expect("live walk");+ let actual = match state {+ IndexState::Ready { entries, .. } => entries.clone(),+ IndexState::Unbuilt => walk_index_entries(base, MAX_INDEX_ENTRIES).expect("rebuild"),+ _ => panic!("unexpected state"),+ };+ assert_eq!(actual, expected);+ }++ #[test]+ fn invalidated_watcher_cannot_disable_a_rebuilt_index() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ std::fs::write(base.join("visible.rs"), "visible").expect("write");+ let shared = Arc::new(Shared {+ state: Mutex::new(snapshot(base)),+ ready: Condvar::new(),+ });+ let (sender, events) = mpsc::channel();+ let worker = spawn_applier(+ Arc::downgrade(&shared),+ events,+ base.to_path_buf(),+ base.to_path_buf(),+ );+ sender+ .send(Ok(Event::new(notify::EventKind::Create(CreateKind::File))+ .add_path(base.join(".ignore"))))+ .expect("invalidate");+ let guard = shared.state.lock().expect("lock");+ let (mut guard, timeout) = shared+ .ready+ .wait_timeout_while(guard, std::time::Duration::from_secs(30), |state| {+ matches!(state, IndexState::Ready { .. })+ })+ .expect("wait for invalidation");+ assert!(!timeout.timed_out(), "watcher never invalidated its index");+ assert!(matches!(*guard, IndexState::Unbuilt));+ // Complete a new generation while holding the same state lock. An old+ // callback may still have queued an error when its watcher was dropped.+ *guard = snapshot(base);+ let _ = sender.send(Err(notify::Error::generic("old watcher error")));+ drop(sender);+ drop(guard);+ worker.join().expect("applier exits");+ let guard = shared.state.lock().expect("lock");+ assert!(matches!(*guard, IndexState::Ready { .. }));+ assert_matches_live_walk(&guard, base);+ }++ #[test]+ fn unknown_directory_modify_refreshes_descendants() {+ for kind in [ModifyKind::Any, ModifyKind::Other] {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ std::fs::create_dir(base.join("dir")).expect("mkdir");+ std::fs::write(base.join("dir/old.rs"), "old").expect("write");+ let mut state = snapshot(base);+ std::fs::remove_dir_all(base.join("dir")).expect("remove");+ std::fs::create_dir(base.join("dir")).expect("mkdir");+ std::fs::write(base.join("dir/new.rs"), "new").expect("write");+ let event = Event::new(notify::EventKind::Modify(kind)).add_path(base.join("dir"));+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert_matches_live_walk(&state, base);+ }+ }++ #[cfg(unix)]+ #[test]+ fn saving_backslash_filename_preserves_live_walk_results() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ let path = base.join(r"a\b.rs");+ std::fs::write(&path, "before").expect("write");+ let mut state = snapshot(base);+ std::fs::write(&path, "after").expect("save");+ let event = Event::new(notify::EventKind::Modify(ModifyKind::Data(+ notify::event::DataChange::Content,+ )))+ .add_path(path);+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert_matches_live_walk(&state, base);+ }++ // macOS APFS rejects invalid UTF-8 names before the watcher can see them.+ #[cfg(target_os = "linux")]+ #[test]+ fn saving_non_utf8_filename_preserves_live_walk_results() {+ use std::os::unix::ffi::OsStringExt;++ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ let path = base.join(std::ffi::OsString::from_vec(b"invalid-\xff.rs".to_vec()));+ std::fs::write(&path, "before").expect("write");+ let mut state = snapshot(base);+ std::fs::write(&path, "after").expect("save");+ let event = Event::new(notify::EventKind::Modify(ModifyKind::Data(+ notify::event::DataChange::Content,+ )))+ .add_path(path);+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert_matches_live_walk(&state, base);+ }++ #[test]+ fn overlapping_directory_refreshes_count_each_descendant_once() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ std::fs::create_dir_all(base.join("dir/nested")).expect("mkdir");+ std::fs::create_dir(base.join("dir-other")).expect("mkdir sibling");+ std::fs::write(base.join("dir/nested/file.rs"), "file").expect("write");+ std::fs::write(base.join("dir-other/sibling.rs"), "sibling").expect("write");+ let mut state = snapshot(base);+ // Fill the unaffected portion in memory: exercising the cap must not+ // require creating a million files on the test runner.+ let IndexState::Ready { entries, .. } = &mut state else {+ panic!("ready snapshot");+ };+ for index in entries.len()..MAX_INDEX_ENTRIES {+ entries.push(format!("unaffected-{index:07}.rs").into_boxed_str());+ }+ entries.sort();+ let expected = entries.clone();+ let events = [+ Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))+ .add_path(base.join("dir"))),+ Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))+ .add_path(base.join("dir-other"))),+ Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))+ .add_path(base.join("dir/nested"))),+ ];+ apply_batch(&mut state, &events, base, base);+ let IndexState::Ready { entries, .. } = state else {+ panic!("overlapping refreshes must not disable an index within the cap");+ };+ assert_eq!(entries, expected);+ }++ #[test]+ fn directory_renamed_onto_indexed_empty_destination_refreshes_descendants() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path().join("base");+ std::fs::create_dir_all(base.join("dest")).expect("mkdir");+ let mut state = snapshot(&base);+ let source = outer.path().join("source");+ std::fs::create_dir(&source).expect("mkdir");+ std::fs::write(source.join("new.rs"), "new").expect("write");+ std::fs::remove_dir(base.join("dest")).expect("remove empty destination");+ std::fs::rename(&source, base.join("dest")).expect("rename");+ let event = Event::new(notify::EventKind::Modify(ModifyKind::Name(RenameMode::To)))+ .add_path(base.join("dest"));+ apply_batch(&mut state, &[Ok(event)], &base, &base);+ assert_matches_live_walk(&state, &base);+ }++ #[test]+ fn coalesced_directory_replacement_removes_old_descendants() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path().join("base");+ std::fs::create_dir_all(base.join("dest")).expect("mkdir");+ std::fs::write(base.join("dest/old.rs"), "old").expect("write");+ let mut state = snapshot(&base);+ std::fs::rename(base.join("dest"), outer.path().join("old")).expect("rename out");+ std::fs::create_dir(base.join("dest")).expect("mkdir");+ std::fs::write(base.join("dest/new.rs"), "new").expect("write");+ let events = [+ Ok(Event::new(notify::EventKind::Remove(RemoveKind::Folder))+ .add_path(base.join("dest"))),+ Ok(Event::new(notify::EventKind::Create(CreateKind::Folder))+ .add_path(base.join("dest"))),+ ];+ apply_batch(&mut state, &events, &base, &base);+ assert_matches_live_walk(&state, &base);+ }++ #[test]+ fn coalesced_base_replacement_drops_the_old_tree() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path().join("base");+ std::fs::create_dir(&base).expect("mkdir");+ std::fs::write(base.join("old.rs"), "old").expect("write");+ let mut state = snapshot(&base);+ std::fs::rename(&base, outer.path().join("old")).expect("rename out");+ std::fs::create_dir(&base).expect("mkdir");+ std::fs::write(base.join("new.rs"), "new").expect("write");+ let event =+ Event::new(notify::EventKind::Remove(RemoveKind::Folder)).add_path(base.clone());+ apply_batch(&mut state, &[Ok(event)], &base, &base);+ assert_matches_live_walk(&state, &base);+ }++ #[test]+ fn git_directory_metadata_keeps_the_ready_index() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ std::fs::create_dir(base.join(".git")).expect("git init");+ std::fs::write(base.join("visible.rs"), "visible").expect("write");+ let mut state = snapshot(base);+ let event = Event::new(notify::EventKind::Modify(ModifyKind::Metadata(+ notify::event::MetadataKind::WriteTime,+ )))+ .add_path(base.join(".git"));+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert!(+ matches!(state, IndexState::Ready { .. }),+ "ordinary Git directory metadata must not rebuild the index"+ );+ assert_matches_live_walk(&state, base);+ }++ #[test]+ fn git_marker_creation_and_removal_refresh_ignore_rules() {+ let outer = tempfile::tempdir().expect("tempdir");+ let base = outer.path();+ std::fs::write(base.join(".gitignore"), "generated/\n").expect("write");+ std::fs::create_dir(base.join("generated")).expect("mkdir");+ std::fs::write(base.join("generated/output.rs"), "generated").expect("write");+ let mut state = snapshot(base);+ std::fs::create_dir(base.join(".git")).expect("git init");+ let event =+ Event::new(notify::EventKind::Create(CreateKind::Folder)).add_path(base.join(".git"));+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert_matches_live_walk(&state, base);+ let mut state = snapshot(base);+ std::fs::remove_dir(base.join(".git")).expect("remove git marker");+ let event =+ Event::new(notify::EventKind::Remove(RemoveKind::Folder)).add_path(base.join(".git"));+ apply_batch(&mut state, &[Ok(event)], base, base);+ assert_matches_live_walk(&state, base);+ }+}diff --git a/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs b/crates/pa-tui/src/autocomplete/fuzzy_file_search.rsindex 16a9d0757f..ec2eb54674 100644--- a/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs+++ b/crates/pa-tui/src/autocomplete/fuzzy_file_search.rs@@ -1,27 +1,33 @@//! The `@` fuzzy file search: fd's own `ignore`-crate walk — files and directories, links//! followed, hidden entries included, `.git` pruned, fd's default gitignore rules — run//! on one background thread, because a no-match walk of a large tree takes seconds; the-//! result lands on the receiver.+//! result lands on the receiver. From the first query on, the session's file index+//! (see [`file_index`]) answers instead; the walk stays as the fallback for scopes the+//! index cannot serve.-use std::cmp::Reverse;+use std::cmp::Ordering;use std::path::{Path, PathBuf};-use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};use std::sync::mpsc::{self, Receiver};use std::sync::{Arc, Mutex, PoisonError};use std::thread;-use ignore::{WalkBuilder, WalkState};+use ignore::{DirEntry, WalkBuilder, WalkState};use regex::RegexBuilder;+use super::file_index::{Answer, FileIndex, IndexScope};use super::{build_completion_value, expand_home_path, parse_path_prefix, CompletionItem, SuggestionKind,Suggestions,};+/// The `.git` directory name: pruned from every walk.+pub(super) const GIT_DIR: &str = ".git";+/// fd's `--max-results`: the walk stops once this many entries match.-const MAX_WALK_RESULTS: usize = 100;+pub(super) const MAX_WALK_RESULTS: usize = 100;/// The menu's row cap.-const MAX_SUGGESTIONS: usize = 20;+pub(super) const MAX_SUGGESTIONS: usize = 20;/// An in-flight `@` file search: the walk thread sends its result over `results` (`None`/// when there is nothing to suggest). Dropping the handle cancels the walk.@@ -33,28 +39,124 @@ pub struct FileSearch {impl Drop for FileSearch {fn drop(&mut self) {- self.cancel.store(true, Ordering::Relaxed);+ self.cancel.store(true, AtomicOrdering::Relaxed);}}/// Start the background search for the typed `@` token against the/// session cwd.-pub(super) fn spawn(base: &Path, at_prefix: String) -> FileSearch {+pub(super) fn spawn(base: &Path, index: FileIndex, at_prefix: String) -> FileSearch {let (tx, results) = mpsc::channel();let cancel = Arc::new(AtomicBool::new(false));let base = base.to_path_buf();let thread_cancel = Arc::clone(&cancel);thread::spawn(move || {- let _ = tx.send(search(&base, &at_prefix, &thread_cancel));+ let _ = tx.send(search(&base, &index, &at_prefix, &thread_cancel));});FileSearch { results, cancel }}-/// One walk result in fd's printed form: relative to the walk base,-/// directories carrying their trailing `/`.-struct WalkedEntry {- path: String,- is_directory: bool,+/// The fd printed form without the directory marker.+pub(super) fn form_trimmed(form: &str) -> &str {+ form.strip_suffix('/').unwrap_or(form)+}++/// The final path segment of a fd printed form.+pub(super) fn form_name(form: &str) -> &str {+ form_trimmed(form).rsplit('/').next().unwrap_or_default()+}++/// The depth below the walk base: the separator count of the trimmed form.+fn form_depth(form: &str) -> usize {+ form_trimmed(form).matches('/').count()+}++/// fd's walk rules — the one configuration every walk in the `@` completion uses:+/// hidden entries included, links followed, `.git` pruned, fd's default ignore set.+pub(super) fn walk_builder(root: &Path) -> WalkBuilder {+ let mut builder = WalkBuilder::new(root);+ builder.hidden(false);+ builder.follow_links(true);+ builder.filter_entry(|entry| entry.file_name() != GIT_DIR);+ builder+}++/// The fd printed form of one walk entry: relative to `walk_base` (the walk may sit+/// deeper inside it), directories carrying their trailing `/`; the walk root itself+/// and non-file non-dir entries skip.+pub(super) fn fd_printed_form(entry: &DirEntry, walk_base: &Path) -> Option<String> {+ if entry.depth() == 0 {+ return None;+ }+ let file_type = entry.file_type()?;+ if !file_type.is_file() && !file_type.is_dir() {+ return None;+ }+ let relative = entry.path().strip_prefix(walk_base).unwrap_or(entry.path());+ let form = to_display_path(&relative.to_string_lossy());+ Some(if file_type.is_dir() {+ format!("{form}/")+ } else {+ form+ })+}++/// The walk-base-joined path full-path mode matches, built into a reused buffer.+/// `base` is the walk base as a string.+fn push_joined_path(target: &mut String, base: &str, form: &str) {+ target.clear();+ target.push_str(base);+ if !target.ends_with(std::path::MAIN_SEPARATOR) {+ target.push(std::path::MAIN_SEPARATOR);+ }+ target.push_str(form_trimmed(form));+}++/// One typed `@` token's resolved search: what the ranking needs — the typed query+/// lowercased once, the typed scope's display base rebuilt into the item paths, and the+/// token's quoting — and how candidates match and cancel.+pub(super) struct AtSearch<'a> {+ pub(super) lower_query: &'a str,+ pub(super) display_base: Option<&'a str>,+ pub(super) at_prefix: &'a str,+ pub(super) is_quoted_prefix: bool,+ pub(super) walk_base: &'a Path,+ pub(super) regex: Option<&'a regex::Regex>,+ pub(super) full_path_mode: bool,+ pub(super) cancel: &'a AtomicBool,+}++impl AtSearch<'_> {+ /// The one fd match of a printed form, shared by the walk and the index scan: the+ /// regex against the name, or in full-path mode against the walk-base-joined path+ /// (built into `target`). `base` is the walk base as a string; an empty query+ /// matches every form.+ pub(super) fn matches(&self, base: &str, form: &str, target: &mut String) -> bool {+ let Some(regex) = self.regex else {+ return true;+ };+ if self.full_path_mode {+ push_joined_path(target, base, form);+ regex.is_match(target)+ } else {+ regex.is_match(form_name(form))+ }+ }++ /// The one scoring of a matched form, shared by the walk and the index scan: `None`+ /// for a zero score; an empty query scores every form 1.+ pub(super) fn score<'form>(&self, form: &'form str) -> Option<RankedForm<'form>> {+ let score = if self.lower_query.is_empty() {+ 1+ } else {+ score_entry(form, self.lower_query, form.ends_with('/'))+ };+ (score > 0).then(|| RankedForm {+ score,+ depth: form_depth(form),+ form,+ })+ }}/// The `@dir/partial` scope: the walk runs inside `base_dir` for `query`, and@@ -91,66 +193,68 @@ fn resolve_scoped_query(base: &Path, raw_query: &str) -> Option<ScopedQuery> {/// Walk with fd's semantics: the parallel `ignore` walker, the base itself skipped, the/// walk cut at [`MAX_WALK_RESULTS`] or on cancel; results come back in fd's printed form.-fn walk_directory(- walk_base: &Path,- regex: Option<®ex::Regex>,- full_path_mode: bool,- cancel: &AtomicBool,-) -> Vec<WalkedEntry> {- let found: Mutex<Vec<WalkedEntry>> = Mutex::new(Vec::new());- WalkBuilder::new(walk_base)- .hidden(false)- .follow_links(true)- .filter_entry(|entry| entry.file_name() != ".git")- .build_parallel()- .run(|| {- Box::new(|entry| {- if cancel.load(Ordering::Relaxed) {- return WalkState::Quit;- }- let Ok(entry) = entry else {- return WalkState::Continue;- };- // Only files and directories reach the results (broken- // links and loops arrive as errors and skip).- if entry.depth() == 0 {- return WalkState::Continue;- }- let file_type = match entry.file_type() {- Some(file_type) if file_type.is_file() || file_type.is_dir() => file_type,- _ => return WalkState::Continue,- };- let target = if full_path_mode {- entry.path().to_string_lossy()- } else {- entry.file_name().to_string_lossy()- };- if regex.is_some_and(|regex| !regex.is_match(&target)) {- return WalkState::Continue;- }- let relative = entry.path().strip_prefix(walk_base).unwrap_or(entry.path());- let path = to_display_path(&relative.to_string_lossy());- let is_directory = file_type.is_dir();- let path = if is_directory {- format!("{path}/")- } else {- path- };- let mut found = found.lock().unwrap_or_else(PoisonError::into_inner);- if found.len() == MAX_WALK_RESULTS {- return WalkState::Quit;- }- found.push(WalkedEntry { path, is_directory });- WalkState::Continue- })- });+fn walk_directory(search: &AtSearch<'_>) -> Vec<String> {+ let found: Mutex<Vec<String>> = Mutex::new(Vec::new());+ let base = search.walk_base.to_string_lossy().into_owned();+ walk_builder(search.walk_base).build_parallel().run(|| {+ let (found, base) = (&found, base.as_str());+ let mut target = String::new();+ Box::new(move |entry| {+ if search.cancel.load(AtomicOrdering::Relaxed) {+ return WalkState::Quit;+ }+ let Ok(entry) = entry else {+ return WalkState::Continue;+ };+ let Some(form) = fd_printed_form(&entry, search.walk_base) else {+ return WalkState::Continue;+ };+ if !search.matches(base, &form, &mut target) {+ return WalkState::Continue;+ }+ let mut found = found.lock().unwrap_or_else(PoisonError::into_inner);+ if found.len() == MAX_WALK_RESULTS {+ return WalkState::Quit;+ }+ found.push(form);+ WalkState::Continue+ })+ });found.into_inner().unwrap_or_else(PoisonError::into_inner)}-/// The walk thread body: resolve the typed scope, walk with fd's semantics, score, and-/// build the items. `None` means no menu: fd matched nothing, the query was an invalid-/// regex (fd exits non-zero), or every entry scored zero.-fn search(base: &Path, at_prefix: &str, cancel: &AtomicBool) -> Option<Suggestions> {+/// The walk thread body: answer from the file index when it serves the query (walking+/// once to build it on first use), fall back to the live walk otherwise, and rank.+/// `None` means no menu: fd matched nothing, the query was an invalid regex (fd exits+/// non-zero), or every entry scored zero.+fn search(+ base: &Path,+ index: &FileIndex,+ at_prefix: &str,+ cancel: &AtomicBool,+) -> Option<Suggestions> {+ resolve_search(base, at_prefix, cancel, |search, scope| {+ match index.query(base, scope, search) {+ Answer::Served(answered) => answered,+ Answer::Walk => {+ let found = walk_directory(search);+ suggestions(+ top_ranked(found.iter().filter_map(|form| search.score(form))),+ search,+ )+ }+ }+ })+}++/// Resolve the typed `@` token — its scope, the fd pattern, the index scope — and hand+/// the resolved search to `answer`. An invalid regex answers `None` without it.+pub(super) fn resolve_search(+ base: &Path,+ at_prefix: &str,+ cancel: &AtomicBool,+ answer: impl FnOnce(&AtSearch<'_>, &IndexScope) -> Option<Suggestions>,+) -> Option<Suggestions> {let (raw_query, _is_at_prefix, is_quoted_prefix) = parse_path_prefix(at_prefix);let scoped_query = resolve_scoped_query(base, &raw_query);let (walk_base, query) = match &scoped_query {@@ -171,39 +275,113 @@ fn search(base: &Path, at_prefix: &str, cancel: &AtomicBool) -> Option<Suggestio.ok()?,)};- let found = walk_directory(&walk_base, regex.as_ref(), full_path_mode, cancel);- // The stable sort keeps fd's arrival order within a score.- let mut scored_entries: Vec<(i32, WalkedEntry)> = found- .into_iter()- .map(|entry| {- let score = if query.is_empty() {- 1- } else {- score_entry(&entry.path, &query, entry.is_directory)- };- (score, entry)- })- .filter(|(score, _)| *score > 0)- .collect();- scored_entries.sort_by_key(|(score, _)| Reverse(*score));- scored_entries.truncate(MAX_SUGGESTIONS);- let items: Vec<CompletionItem> = scored_entries+ let lower_query = query.to_lowercase();+ let search = AtSearch {+ lower_query: &lower_query,+ display_base: scoped_query+ .as_ref()+ .map(|scoped| scoped.display_base.as_str()),+ at_prefix,+ is_quoted_prefix,+ walk_base: &walk_base,+ regex: regex.as_ref(),+ full_path_mode,+ cancel,+ };+ let scope = match &scoped_query {+ Some(scoped) => index_scope(&scoped.display_base),+ None => IndexScope::Unscoped,+ };+ answer(&search, &scope)+}++/// The typed scope as an index prefix: relative, no `..` component, a leading `./`+/// normalized away — `Outside` when the scope can never be served by the index.+fn index_scope(display_base: &str) -> IndexScope {+ if display_base.starts_with('/') || display_base.starts_with('~') {+ return IndexScope::Outside;+ }+ let normalized = display_base.strip_prefix("./").unwrap_or(display_base);+ if normalized.split('/').any(|component| component == "..") {+ return IndexScope::Outside;+ }+ IndexScope::Prefix(normalized.to_string())+}++/// A scored candidate form, ordered by score (descending), then depth, then path.+pub(super) struct RankedForm<'a> {+ score: i32,+ depth: usize,+ form: &'a str,+}++impl RankedForm<'_> {+ fn cmp(&self, other: &Self) -> Ordering {+ other+ .score+ .cmp(&self.score)+ .then_with(|| self.depth.cmp(&other.depth))+ .then_with(|| form_trimmed(self.form).cmp(form_trimmed(other.form)))+ }+}++/// The one top-[`MAX_SUGGESTIONS`] selection, in rank order: over the walk's ranks, an+/// index chunk's, and the merged chunk tops alike. Bounded work — the full ranked set+/// never collects.+pub(super) fn top_ranked<'a>(+ ranked: impl IntoIterator<Item = RankedForm<'a>>,+) -> Vec<RankedForm<'a>> {+ let mut best: Vec<RankedForm<'a>> = Vec::with_capacity(MAX_SUGGESTIONS);+ for ranked in ranked {+ if best.len() < MAX_SUGGESTIONS {+ best.push(ranked);+ if best.len() == MAX_SUGGESTIONS {+ best.sort_unstable_by(RankedForm::cmp);+ }+ } else if let Some(worst) = best.last() {+ if RankedForm::cmp(&ranked, worst) == Ordering::Less {+ best.pop();+ let position =+ best.partition_point(|kept| RankedForm::cmp(kept, &ranked) == Ordering::Less);+ best.insert(position, ranked);+ }+ }+ }+ // The fill phase above only sorts at capacity; a short candidate list arrives in+ // its own order.+ best.sort_unstable_by(RankedForm::cmp);+ best+}++/// The menu of the top ranks, in their order. `None` means no menu.+pub(super) fn suggestions(best: Vec<RankedForm<'_>>, search: &AtSearch<'_>) -> Option<Suggestions> {+ if best.is_empty() {+ return None;+ }+ let AtSearch {+ display_base,+ at_prefix,+ is_quoted_prefix,+ ..+ } = search;+ let items: Vec<CompletionItem> = best.into_iter()- .map(|(_, entry)| {- let path = entry.path.strip_suffix('/').unwrap_or(&entry.path);- let display_path = match &scoped_query {- Some(scoped) => scoped_path_for_display(&scoped.display_base, path),+ .map(|ranked| {+ let path = form_trimmed(ranked.form);+ let display_path = match display_base {+ Some(display_base) => scoped_path_for_display(display_base, path),None => path.to_string(),};let name = path.rsplit('/').next().unwrap_or_default();- let completion_path = if entry.is_directory {+ let is_directory = ranked.form.ends_with('/');+ let completion_path = if is_directory {format!("{display_path}/")} else {display_path.clone()};CompletionItem {- value: build_completion_value(&completion_path, true, is_quoted_prefix),- label: if entry.is_directory {+ value: build_completion_value(&completion_path, true, *is_quoted_prefix),+ label: if is_directory {format!("{name}/")} else {name.to_string()@@ -214,33 +392,60 @@ fn search(base: &Path, at_prefix: &str, cancel: &AtomicBool) -> Option<Suggestio}}).collect();- if items.is_empty() {- return None;- }Some(Suggestions {- prefix: at_prefix.to_string(),+ prefix: (*at_prefix).to_string(),kind: Some(SuggestionKind::File),items,})}+/// Case-insensitive equality against a needle already lowercased: in place for an+/// ASCII pair (ASCII casing is per-byte), Unicode lowering otherwise.+fn eq_ignore_case(haystack: &str, lower_needle: &str) -> bool {+ if haystack.is_ascii() && lower_needle.is_ascii() {+ haystack.eq_ignore_ascii_case(lower_needle)+ } else {+ haystack.to_lowercase() == lower_needle+ }+}++/// Case-insensitive prefix test against a needle already lowercased: in place for an+/// ASCII pair (ASCII casing is per-byte), Unicode lowering otherwise.+fn starts_with_ignore_case(haystack: &str, lower_needle: &str) -> bool {+ if haystack.is_ascii() && lower_needle.is_ascii() {+ haystack.len() >= lower_needle.len()+ && haystack[..lower_needle.len()].eq_ignore_ascii_case(lower_needle)+ } else {+ haystack.to_lowercase().starts_with(lower_needle)+ }+}++/// Case-insensitive containment against a needle already lowercased: in place for an+/// ASCII pair (byte windows, because ASCII casing is per-byte), Unicode lowering+/// otherwise.+fn contains_ignore_case(haystack: &str, lower_needle: &str) -> bool {+ if haystack.is_ascii() && lower_needle.is_ascii() {+ haystack+ .as_bytes()+ .windows(lower_needle.len())+ .any(|window| window.eq_ignore_ascii_case(lower_needle.as_bytes()))+ } else {+ haystack.to_lowercase().contains(lower_needle)+ }+}+/// Exact name 100, name prefix 80, name contains 50, path contains 30,-/// +10 for directories (when positive), all case-insensitive.-fn score_entry(file_path: &str, query: &str, is_directory: bool) -> i32 {- let file_name = file_path- .trim_end_matches('/')- .rsplit('/')- .next()- .unwrap_or_default();- let lower_name = file_name.to_lowercase();- let lower_query = query.to_lowercase();- let mut score = if lower_name == lower_query {+/// +10 for directories (when positive), all case-insensitive against the+/// once-lowercased query.+fn score_entry(form: &str, lower_query: &str, is_directory: bool) -> i32 {+ let file_name = form_name(form);+ let mut score = if eq_ignore_case(file_name, lower_query) {100- } else if lower_name.starts_with(&lower_query) {+ } else if starts_with_ignore_case(file_name, lower_query) {80- } else if lower_name.contains(&lower_query) {+ } else if contains_ignore_case(file_name, lower_query) {50- } else if file_path.to_lowercase().contains(&lower_query) {+ } else if contains_ignore_case(form, lower_query) {30} else {0@@ -277,7 +482,7 @@ fn build_fd_path_query(query: &str) -> String {}/// Normalize backslashes to `/` on every platform.-fn to_display_path(value: &str) -> String {+pub(super) fn to_display_path(value: &str) -> String {value.replace('\\', "/")}diff --git a/crates/pa-tui/src/autocomplete/tests.rs b/crates/pa-tui/src/autocomplete/tests.rsindex d9b5001a80..7158847173 100644--- a/crates/pa-tui/src/autocomplete/tests.rs+++ b/crates/pa-tui/src/autocomplete/tests.rs@@ -600,3 +600,307 @@ fn render_shows_the_source_tag_as_a_trailing_segment() {"the selected description renders: {all}");}++/// The `@` suggestion items of one provider query, waiting for the background search.+fn at_values(provider: &CombinedAutocompleteProvider, text: &str) -> Vec<CompletionItem> {+ match provider.get_suggestions(&[text.to_string()], 0, text.chars().count(), false) {+ Some(SuggestionLookup::Searching(search)) => search+ .results+ .recv()+ .expect("the search finishes")+ .map(|suggestions| suggestions.items)+ .unwrap_or_default(),+ other => panic!("expected a searching lookup, got {other:?}"),+ }+}++/// The parity fixture: nested directories, a hidden file, a pruned `.git`, a gitignored+/// directory (the `.git` directory makes the gitignore rules apply), a directory entry,+/// and mixed case.+fn parity_fixture() -> tempfile::TempDir {+ let outer = tempfile::TempDir::new().expect("temp dir");+ let base = outer.path().join("base");+ std::fs::create_dir_all(base.join(".git/objects")).expect("mkdir");+ std::fs::write(base.join(".git/config"), "git").expect("write");+ std::fs::write(base.join(".gitignore"), "ignored/\n").expect("write");+ std::fs::create_dir_all(base.join("src/deep")).expect("mkdir");+ std::fs::create_dir_all(base.join("ignored")).expect("mkdir");+ std::fs::write(base.join("src/partial_match.rs"), "x").expect("write");+ std::fs::write(base.join("src/ExactCase.rs"), "x").expect("write");+ std::fs::write(base.join("src/deep/other.rs"), "x").expect("write");+ std::fs::write(base.join("ignored/never.rs"), "x").expect("write");+ std::fs::write(base.join(".hidden"), "x").expect("write");+ std::fs::write(base.join("README.md"), "x").expect("write");+ outer+}++/// The index answers exactly what the walk answers: one provider on the built index,+/// one forced onto the walk path (an over-cap build disables its index), across the+/// scope and case shapes the two paths could drift on.+#[test]+fn at_index_matches_the_walk_suggestions() {+ let outer = parity_fixture();+ let base = outer.path().join("base");+ let indexed = provider(base.to_str().expect("utf8"));+ let walked = provider(base.to_str().expect("utf8"));+ assert!(indexed+ .file_index+ .build(&base, file_index::MAX_INDEX_ENTRIES));+ assert!(!walked.file_index.build(&base, 1));+ for query in [+ "",+ "match",+ "par",+ "README.md",+ "ExactCase",+ "deep/other",+ "src/",+ "src/deep/",+ "src/par",+ "src/deep/o",+ "./READ",+ "ignored/ne",+ ] {+ assert_eq!(+ at_values(&indexed, &format!("@{query}")),+ at_values(&walked, &format!("@{query}")),+ "query @{query}"+ );+ }+}++/// A xorshift generator: the random trees need no dependency.+struct XorShift(u64);++impl XorShift {+ fn below(&mut self, bound: usize) -> usize {+ self.0 ^= self.0 << 13;+ self.0 ^= self.0 >> 7;+ self.0 ^= self.0 << 17;+ usize::try_from(self.0 % bound as u64).expect("below a usize bound")+ }++ fn pick<'a>(&mut self, items: &[&'a str]) -> &'a str {+ items[self.below(items.len())]+ }+}++/// A random tree of at least `entries` index entries under `base`, returned as their+/// printed forms: nested directories; names mixing ASCII case, Unicode (case-fold+/// outliers included), and regex metacharacters; a hidden file; and a gitignored+/// directory beside `.git`.+fn random_tree(base: &std::path::Path, rng: &mut XorShift, entries: usize) -> Vec<String> {+ const WORDS: [&str; 16] = [+ "src", "Lib", "main", "README", "Test", "util", "MODULE", "café", "Straße", "İzmir",+ "ſtate", "日本", "文件", "🦀crab", "naïve", "data",+ ];+ const MARKS: [&str; 12] = ["", ".", "-", "#", "&", "~", "+", "(", "[", "$", "_", ""];+ const EXTENSIONS: [&str; 6] = ["", ".rs", ".md", ".TXT", ".tar.gz", ".json"];+ std::fs::create_dir_all(base.join(".git")).expect("mkdir");+ std::fs::write(base.join(".git/config"), "git").expect("write");+ std::fs::write(base.join(".gitignore"), "ignored/\n").expect("write");+ std::fs::create_dir_all(base.join("ignored")).expect("mkdir");+ std::fs::write(base.join("ignored/Ignored-main.rs"), "x").expect("write");+ std::fs::write(base.join(".hidden-main.rs"), "x").expect("write");+ let mut dirs = vec![String::new()];+ let mut forms = vec![".gitignore".to_string(), ".hidden-main.rs".to_string()];+ while forms.len() < entries {+ let parent = dirs[rng.below(dirs.len())].clone();+ let stem = format!(+ "{parent}{}{}{}{}",+ rng.pick(&WORDS),+ rng.pick(&MARKS),+ rng.pick(&WORDS),+ forms.len()+ );+ if parent.matches('/').count() < 4 && rng.below(8) == 0 {+ std::fs::create_dir(base.join(&stem)).expect("mkdir");+ dirs.push(format!("{stem}/"));+ forms.push(format!("{stem}/"));+ } else {+ let rel = format!("{stem}{}", rng.pick(&EXTENSIONS));+ std::fs::write(base.join(&rel), "x").expect("write");+ forms.push(rel);+ }+ }+ forms+}++/// Queries over a random tree: the bare query, metacharacter and Unicode fragments (an+/// invalid regex among them), and from real entries their exact names, prefixes,+/// substrings, case variants, scoped, `./` scoped, and full-path shapes.+fn random_queries(forms: &[String], rng: &mut XorShift) -> Vec<String> {+ let mut queries: Vec<String> = [+ "", ".", "-", "#", "&", "~", "+", "(", "[", "$", ".*", "s.c", "[a-z]+0", "é", "ß", "SS",+ "İ", "i", "ſ", "s", "日本", "🦀", "MAIN", "./", "./main", "ignored/",+ ]+ .map(String::from)+ .to_vec();+ queries.push("ignored/Ig".to_string());+ for _ in 0..8 {+ let form = &forms[rng.below(forms.len())];+ let path = fuzzy_file_search::form_trimmed(form);+ let name: Vec<char> = fuzzy_file_search::form_name(form).chars().collect();+ let start = rng.below(name.len());+ let end = start + 1 + rng.below(name.len() - start);+ let part: String = name[start..end].iter().collect();+ queries.push(name.iter().collect());+ queries.push(name[..end].iter().collect());+ queries.push(part.to_uppercase());+ queries.push(part.to_lowercase());+ if let Some((parent, _)) = path.rsplit_once('/') {+ let parent_name = parent.rsplit('/').next().unwrap_or_default();+ queries.push(format!("{parent}/{part}"));+ queries.push(format!("./{parent}/"));+ queries.push(format!("{parent_name}/{part}"));+ }+ queries.push(part);+ }+ queries+}++/// The index answers what the full, uncut walk ranks — fd's walk rules, the shared+/// matcher, and the shared ranking — on random trees, one small and one large enough+/// for a multi-chunk scan; where the walk's result cut cannot apply (at most+/// [`fuzzy_file_search::MAX_WALK_RESULTS`] matches), the walk provider answers the same.+#[test]+fn at_index_matches_the_walk_on_random_trees() {+ const SEEDS: u64 = 1;+ let large = (2 * file_index::SCAN_CHUNK_MIN).max(32_768) + 1_000;+ for seed in 0..SEEDS {+ for entries in [300, large] {+ let mut rng = XorShift(0x9E37_79B9_7F4A_7C15 ^ seed);+ let outer = tempfile::TempDir::new().expect("temp dir");+ let base = outer.path().join("base");+ let forms = random_tree(&base, &mut rng, entries);+ let indexed = provider(base.to_str().expect("utf8"));+ let walked = provider(base.to_str().expect("utf8"));+ assert!(indexed+ .file_index+ .build(&base, file_index::MAX_INDEX_ENTRIES));+ assert!(!walked.file_index.build(&base, 1));+ let cancel = std::sync::atomic::AtomicBool::new(false);+ let mut walks: HashMap<std::path::PathBuf, Vec<String>> = HashMap::new();+ for query in random_queries(&forms, &mut rng) {+ let at_prefix = format!("@{query}");+ let mut matched = 0;+ let expected =+ fuzzy_file_search::resolve_search(&base, &at_prefix, &cancel, |search, _| {+ let walk =+ walks+ .entry(search.walk_base.to_path_buf())+ .or_insert_with(|| {+ fuzzy_file_search::walk_builder(search.walk_base)+ .build()+ .filter_map(Result::ok)+ .filter_map(|entry| {+ fuzzy_file_search::fd_printed_form(+ &entry,+ search.walk_base,+ )+ })+ .collect()+ });+ let walk_base = search.walk_base.to_string_lossy();+ let mut target = String::new();+ let found: Vec<&String> = walk+ .iter()+ .filter(|form| search.matches(&walk_base, form, &mut target))+ .collect();+ matched = found.len();+ fuzzy_file_search::suggestions(+ fuzzy_file_search::top_ranked(+ found.into_iter().filter_map(|form| search.score(form)),+ ),+ search,+ )+ })+ .map(|suggestions| suggestions.items)+ .unwrap_or_default();+ let context = format!("seed {seed}, {entries} entries, query {at_prefix}");+ assert_eq!(at_values(&indexed, &at_prefix), expected, "{context}");+ if matched <= fuzzy_file_search::MAX_WALK_RESULTS {+ assert_eq!(at_values(&walked, &at_prefix), expected, "{context}");+ }+ }+ }+ }+}++/// Watched changes land: a new file and a new directory with a file appear in the+/// index, and vanish with their deletion. The bounded polling waits on external+/// OS events.+#[test]+fn at_index_tracks_watcher_events() {+ let outer = parity_fixture();+ let base = outer.path().join("base");+ let provider = provider(base.to_str().expect("utf8"));+ assert!(provider+ .file_index+ .build(&base, file_index::MAX_INDEX_ENTRIES));+ let values = |query: &str| {+ at_values(&provider, &format!("@{query}"))+ .into_iter()+ .map(|item| item.value)+ .collect::<Vec<_>>()+ };+ let settle = |present: bool| {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ loop {+ let settled = if present {+ values("watched.rs").contains(&"@watched.rs".to_string())+ && values("watched_dir").contains(&"@watched_dir/".to_string())+ && values("inside").contains(&"@watched_dir/inside.rs".to_string())+ } else {+ values("watched.rs").is_empty()+ && values("watched_dir").is_empty()+ && values("inside").is_empty()+ };+ if settled {+ return;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "the watcher never settled the entries (present={present})"+ );+ std::thread::sleep(std::time::Duration::from_millis(25));+ }+ };+ std::fs::write(base.join("watched.rs"), "x").expect("write");+ std::fs::create_dir_all(base.join("watched_dir")).expect("mkdir");+ std::fs::write(base.join("watched_dir/inside.rs"), "x").expect("write");+ settle(true);+ // A directory replaced by a file drops the directory's descendants too.+ std::fs::remove_dir_all(base.join("watched_dir")).expect("remove");+ std::fs::write(base.join("watched_dir"), "x").expect("write");+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ loop {+ let replaced = values("inside").is_empty()+ && values("watched_dir").contains(&"@watched_dir".to_string());+ if replaced {+ break;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "the watcher never settled the replaced directory"+ );+ std::thread::sleep(std::time::Duration::from_millis(25));+ }+ std::fs::remove_file(base.join("watched.rs")).expect("remove");+ std::fs::remove_file(base.join("watched_dir")).expect("remove");+ settle(false);+ // A deleted session base drops the whole index: the next query rebuilds into+ // whatever the tree still holds (nothing) instead of serving stale entries.+ std::fs::remove_dir_all(base).expect("remove");+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);+ loop {+ if values("par").is_empty() {+ break;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "the watcher never dropped the deleted base's index"+ );+ std::thread::sleep(std::time::Duration::from_millis(25));+ }+}diff --git a/deny.toml b/deny.tomlindex 1bb17230ce..211af7f1bd 100644--- a/deny.toml+++ b/deny.toml@@ -40,6 +40,8 @@ allow = ["Zlib","Unicode-3.0","CDLA-Permissive-2.0",+ # notify (the `@` file index watcher) — CC0 public domain dedication.+ "CC0-1.0",]# Development-only tools are not shipped; we keep the graph tight instead of# adding private/exceptions stanzas.