Skip to main content

a3s_code_core/workspace/
manifest.rs

1//! Manifest-backed local workspace services.
2//!
3//! The manifest is an in-memory index of workspace files. It is built
4//! asynchronously, refreshed from filesystem notifications, and used by the
5//! local search backend (`glob`/`grep`) to avoid walking the filesystem for
6//! every agent tool call. File I/O, command execution, and git operations still
7//! delegate to [`LocalWorkspaceBackend`].
8
9use super::{
10    escape_control_chars_for_display, validate_relative_pattern, CommandOutput, CommandRequest,
11    LocalWorkspaceAccessPolicy, LocalWorkspaceBackend, WorkspaceCommandRunner, WorkspaceDirEntry,
12    WorkspaceFileSystem, WorkspaceGit, WorkspaceGitBranch, WorkspaceGitCheckoutOutput,
13    WorkspaceGitCheckoutRequest, WorkspaceGitCommit, WorkspaceGitCreateBranchRequest,
14    WorkspaceGitCreateWorktreeRequest, WorkspaceGitDiffRequest, WorkspaceGitRemote,
15    WorkspaceGitRemoveWorktreeRequest, WorkspaceGitStash, WorkspaceGitStashProvider,
16    WorkspaceGitStashRequest, WorkspaceGitStatus, WorkspaceGitWorktree,
17    WorkspaceGitWorktreeMutation, WorkspaceGitWorktreeProvider, WorkspaceGlobRequest,
18    WorkspaceGlobResult, WorkspaceGrepOutcome, WorkspaceGrepRequest, WorkspaceGrepResult,
19    WorkspacePath, WorkspacePathResolver, WorkspaceResult, WorkspaceSearch, WorkspaceTextRange,
20    WorkspaceTextReader, WorkspaceWriteOutcome,
21};
22use anyhow::{anyhow, Result};
23use async_trait::async_trait;
24use std::collections::{hash_map::DefaultHasher, HashMap, HashSet};
25use std::hash::{Hash, Hasher};
26use std::path::{Component, Path, PathBuf};
27use std::sync::{
28    atomic::{AtomicBool, Ordering},
29    Arc, RwLock,
30};
31#[cfg(test)]
32use std::time::Duration;
33use std::time::{SystemTime, UNIX_EPOCH};
34use tokio::sync::broadcast;
35
36mod scanner;
37mod watcher;
38use scanner::is_relevant_event;
39pub use scanner::scan_workspace_files;
40use watcher::run_manifest_task;
41
42const SNAPSHOT_CHANNEL_CAPACITY: usize = 16;
43const FILE_CHANGE_CHANNEL_CAPACITY: usize = 256;
44const RECENT_FILE_LIMIT: usize = 128;
45const RECENT_DECAY_HALF_LIFE_MS: f32 = 10.0 * 60.0 * 1000.0;
46const RECENT_FREQUENCY_NORMALIZER: f32 = 16.0;
47const RECENT_RECENCY_WEIGHT: f32 = 0.75;
48
49/// Git/workspace status for a file in the manifest.
50#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
51pub enum LocalWorkspaceFileStatus {
52    Tracked,
53    Untracked,
54    Unknown,
55}
56
57/// One manifest entry.
58#[derive(Clone, Debug, Eq, PartialEq, Hash)]
59pub struct LocalWorkspaceFile {
60    pub path: String,
61    pub size: u64,
62    pub modified_ms: Option<u64>,
63    pub language: Option<String>,
64    pub status: LocalWorkspaceFileStatus,
65    pub binary: bool,
66    pub generated: bool,
67}
68
69/// Recency/usage score for a workspace file the user or agent touched.
70///
71/// Hosts should treat this as a ranking hint, not as an authoritative file
72/// list. The manifest filters deleted files when exposing recent entries.
73#[derive(Clone, Debug, PartialEq)]
74pub struct RecentWorkspaceFile {
75    pub path: String,
76    pub score: f32,
77    pub touched_at_ms: u64,
78    pub touch_count: u32,
79}
80
81/// Immutable manifest snapshot.
82#[derive(Clone, Debug, Eq, PartialEq)]
83pub struct LocalWorkspaceManifestSnapshot {
84    pub version: u64,
85    pub root: PathBuf,
86    pub files: Vec<LocalWorkspaceFile>,
87    pub scanned_at_ms: u64,
88}
89
90/// The normalized kind of a workspace file change.
91#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
92pub enum WorkspaceFileChangeKind {
93    Created,
94    Changed,
95    Deleted,
96}
97
98/// A filesystem change for one normalized workspace-relative file path.
99#[derive(Clone, Debug, Eq, PartialEq, Hash)]
100pub struct WorkspaceFileChange {
101    pub path: WorkspacePath,
102    pub kind: WorkspaceFileChangeKind,
103}
104
105impl LocalWorkspaceManifestSnapshot {
106    pub fn empty(root: PathBuf) -> Self {
107        Self {
108            version: 0,
109            root,
110            files: Vec::new(),
111            scanned_at_ms: now_ms(),
112        }
113    }
114
115    pub fn file_paths(&self) -> Vec<String> {
116        self.files.iter().map(|file| file.path.clone()).collect()
117    }
118}
119
120/// Shared in-memory workspace manifest.
121pub struct LocalWorkspaceManifest {
122    state: Arc<RwLock<ManifestState>>,
123    recent: Arc<RwLock<RecentFiles>>,
124    snapshots: broadcast::Sender<LocalWorkspaceManifestSnapshot>,
125    changes: broadcast::Sender<WorkspaceFileChange>,
126    scan_cancelled: Arc<AtomicBool>,
127    task: tokio::task::JoinHandle<()>,
128}
129
130impl LocalWorkspaceManifest {
131    /// Start the manifest scanner/watcher for `root`.
132    pub fn start(root: impl Into<PathBuf>) -> Arc<Self> {
133        let root = root.into();
134        let root = root.canonicalize().unwrap_or_else(|_| root.clone());
135        let initial = LocalWorkspaceManifestSnapshot::empty(root.clone());
136        let state = Arc::new(RwLock::new(ManifestState {
137            fingerprint: fingerprint_files(&initial.files),
138            index: Arc::new(ManifestIndex::build(&initial.files)),
139            snapshot: Arc::new(initial),
140        }));
141        let recent = Arc::new(RwLock::new(RecentFiles::default()));
142        let (snapshots, _) = broadcast::channel(SNAPSHOT_CHANNEL_CAPACITY);
143        let (changes, _) = broadcast::channel(FILE_CHANGE_CHANNEL_CAPACITY);
144        let scan_cancelled = Arc::new(AtomicBool::new(false));
145        let task_state = Arc::clone(&state);
146        let task_snapshots = snapshots.clone();
147        let task_changes = changes.clone();
148        let task_scan_cancelled = Arc::clone(&scan_cancelled);
149        let task = tokio::spawn(async move {
150            run_manifest_task(
151                root,
152                task_state,
153                task_snapshots,
154                task_changes,
155                task_scan_cancelled,
156            )
157            .await;
158        });
159        Arc::new(Self {
160            state,
161            recent,
162            snapshots,
163            changes,
164            scan_cancelled,
165            task,
166        })
167    }
168
169    pub fn snapshot(&self) -> LocalWorkspaceManifestSnapshot {
170        self.state
171            .read()
172            .map(|state| (*state.snapshot).clone())
173            .unwrap_or_else(|_| LocalWorkspaceManifestSnapshot::empty(PathBuf::new()))
174    }
175
176    pub fn subscribe(&self) -> broadcast::Receiver<LocalWorkspaceManifestSnapshot> {
177        self.snapshots.subscribe()
178    }
179
180    /// Subscribe to debounced, workspace-relative filesystem changes.
181    pub fn subscribe_changes(&self) -> broadcast::Receiver<WorkspaceFileChange> {
182        self.changes.subscribe()
183    }
184
185    /// Stop background discovery without waiting for an in-flight synchronous scan.
186    ///
187    /// Hosts with an explicit lifecycle should call this before shutting down
188    /// their Tokio runtime. [`Drop`] is only a fallback because other background
189    /// services may retain an `Arc` to the manifest until runtime teardown.
190    pub fn shutdown(&self) {
191        self.scan_cancelled.store(true, Ordering::Release);
192        self.task.abort();
193    }
194
195    /// Record that a workspace-relative file was opened, read, or written.
196    ///
197    /// This intentionally does not require the initial manifest scan to have
198    /// completed. The public recent-file views filter against the current
199    /// manifest index, so early touches become visible after the file is indexed
200    /// and deleted files disappear automatically.
201    pub fn touch_file(&self, path: impl AsRef<str>) -> bool {
202        let Some(path) = normalize_recent_file_path(path.as_ref()) else {
203            return false;
204        };
205        let Ok(mut recent) = self.recent.write() else {
206            return false;
207        };
208        recent.touch(path, now_ms());
209        true
210    }
211
212    /// Return the hottest known files, newest/frequently used first.
213    pub fn recent_file_entries(&self, limit: usize) -> Vec<RecentWorkspaceFile> {
214        if limit == 0 {
215            return Vec::new();
216        }
217        let Some(index) = self.state.read().ok().map(|state| Arc::clone(&state.index)) else {
218            return Vec::new();
219        };
220        self.recent
221            .read()
222            .map(|recent| recent.entries(Some(&index), limit, now_ms()))
223            .unwrap_or_default()
224    }
225
226    /// Return recent file paths only, preserving hot-file order.
227    pub fn recent_file_paths(&self, limit: usize) -> Vec<String> {
228        self.recent_file_entries(limit)
229            .into_iter()
230            .map(|entry| entry.path)
231            .collect()
232    }
233}
234
235impl Drop for LocalWorkspaceManifest {
236    fn drop(&mut self) {
237        // Aborting the async owner does not stop synchronous discovery that has
238        // already begun. Signal the scanner first so a detached traversal stops
239        // consuming filesystem resources after its host has gone away.
240        self.shutdown();
241    }
242}
243
244struct ManifestState {
245    fingerprint: u64,
246    index: Arc<ManifestIndex>,
247    snapshot: Arc<LocalWorkspaceManifestSnapshot>,
248}
249
250#[derive(Debug, Default)]
251struct RecentFiles {
252    entries: HashMap<String, RecentFileState>,
253    next_sequence: u64,
254}
255
256impl RecentFiles {
257    fn touch(&mut self, path: String, now: u64) {
258        self.next_sequence = self.next_sequence.saturating_add(1);
259        let sequence = self.next_sequence;
260        self.entries
261            .entry(path.clone())
262            .and_modify(|entry| {
263                entry.touched_at_ms = now;
264                entry.touch_count = entry.touch_count.saturating_add(1);
265                entry.sequence = sequence;
266            })
267            .or_insert(RecentFileState {
268                path,
269                touched_at_ms: now,
270                touch_count: 1,
271                sequence,
272            });
273        self.prune(now);
274    }
275
276    fn entries(
277        &self,
278        index: Option<&ManifestIndex>,
279        limit: usize,
280        now: u64,
281    ) -> Vec<RecentWorkspaceFile> {
282        let mut entries = self
283            .entries
284            .values()
285            .filter(|entry| {
286                index
287                    .map(|index| index.by_path.contains_key(&entry.path))
288                    .unwrap_or(true)
289            })
290            .map(|entry| {
291                let score = recent_score(entry, now);
292                (
293                    entry.sequence,
294                    RecentWorkspaceFile {
295                        path: entry.path.clone(),
296                        score,
297                        touched_at_ms: entry.touched_at_ms,
298                        touch_count: entry.touch_count,
299                    },
300                )
301            })
302            .collect::<Vec<_>>();
303
304        entries.sort_by(|(left_sequence, left), (right_sequence, right)| {
305            right
306                .score
307                .total_cmp(&left.score)
308                .then_with(|| right.touched_at_ms.cmp(&left.touched_at_ms))
309                .then_with(|| right_sequence.cmp(left_sequence))
310                .then_with(|| left.path.cmp(&right.path))
311        });
312        entries
313            .into_iter()
314            .take(limit)
315            .map(|(_, entry)| entry)
316            .collect()
317    }
318
319    fn prune(&mut self, now: u64) {
320        if self.entries.len() <= RECENT_FILE_LIMIT {
321            return;
322        }
323
324        let keep = self
325            .entries
326            .values()
327            .map(|entry| (entry.path.clone(), recent_score(entry, now), entry.sequence))
328            .collect::<Vec<_>>();
329        let mut keep = keep;
330        keep.sort_by(|left, right| {
331            right
332                .1
333                .total_cmp(&left.1)
334                .then_with(|| right.2.cmp(&left.2))
335                .then_with(|| left.0.cmp(&right.0))
336        });
337        let keep = keep
338            .into_iter()
339            .take(RECENT_FILE_LIMIT)
340            .map(|(path, _, _)| path)
341            .collect::<HashSet<_>>();
342        self.entries.retain(|path, _| keep.contains(path));
343    }
344}
345
346#[derive(Debug)]
347struct RecentFileState {
348    path: String,
349    touched_at_ms: u64,
350    touch_count: u32,
351    sequence: u64,
352}
353
354#[derive(Debug, Default)]
355struct ManifestIndex {
356    all: Vec<usize>,
357    by_path: HashMap<String, usize>,
358    by_basename: HashMap<String, Vec<usize>>,
359    by_extension: HashMap<String, Vec<usize>>,
360}
361
362impl ManifestIndex {
363    fn build(files: &[LocalWorkspaceFile]) -> Self {
364        let mut index = Self {
365            all: Vec::with_capacity(files.len()),
366            by_path: HashMap::with_capacity(files.len()),
367            by_basename: HashMap::new(),
368            by_extension: HashMap::new(),
369        };
370
371        for (file_index, file) in files.iter().enumerate() {
372            index.all.push(file_index);
373            index.by_path.insert(file.path.clone(), file_index);
374            if let Some(name) = Path::new(&file.path)
375                .file_name()
376                .and_then(|name| name.to_str())
377            {
378                index
379                    .by_basename
380                    .entry(name.to_string())
381                    .or_default()
382                    .push(file_index);
383            }
384            if let Some(extension) = Path::new(&file.path)
385                .extension()
386                .and_then(|extension| extension.to_str())
387                .filter(|extension| !extension.is_empty())
388            {
389                index
390                    .by_extension
391                    .entry(extension.to_string())
392                    .or_default()
393                    .push(file_index);
394            }
395        }
396
397        index
398    }
399}
400
401struct ManifestSearchSnapshot {
402    snapshot: Arc<LocalWorkspaceManifestSnapshot>,
403    index: Arc<ManifestIndex>,
404}
405
406/// Local backend that uses an in-memory manifest for search.
407pub struct ManifestWorkspaceBackend {
408    local: Arc<LocalWorkspaceBackend>,
409    manifest: Arc<LocalWorkspaceManifest>,
410}
411
412impl ManifestWorkspaceBackend {
413    pub fn new(root: impl Into<PathBuf>) -> Arc<Self> {
414        Self::new_with_access_policy(root, LocalWorkspaceAccessPolicy::Unrestricted)
415    }
416
417    pub fn new_with_access_policy(
418        root: impl Into<PathBuf>,
419        access_policy: LocalWorkspaceAccessPolicy,
420    ) -> Arc<Self> {
421        let local = Arc::new(LocalWorkspaceBackend::new_with_access_policy(
422            root.into(),
423            access_policy,
424        ));
425        let manifest = LocalWorkspaceManifest::start(local.root.clone());
426        Self::from_manifest(local, manifest)
427    }
428
429    pub fn from_manifest(
430        local: Arc<LocalWorkspaceBackend>,
431        manifest: Arc<LocalWorkspaceManifest>,
432    ) -> Arc<Self> {
433        Arc::new(Self { local, manifest })
434    }
435
436    pub fn manifest(&self) -> Arc<LocalWorkspaceManifest> {
437        Arc::clone(&self.manifest)
438    }
439
440    pub fn local_root(&self) -> &Path {
441        &self.local.root
442    }
443
444    fn manifest_ready(&self) -> Option<ManifestSearchSnapshot> {
445        let state = self.manifest.state.read().ok()?;
446        (state.snapshot.version > 0).then(|| ManifestSearchSnapshot {
447            snapshot: Arc::clone(&state.snapshot),
448            index: Arc::clone(&state.index),
449        })
450    }
451
452    fn fallback_search(&self) -> Arc<LocalWorkspaceBackend> {
453        Arc::clone(&self.local)
454    }
455
456    fn recent_path_ranks(&self, index: &ManifestIndex) -> HashMap<String, usize> {
457        self.manifest
458            .recent
459            .read()
460            .map(|recent| {
461                recent
462                    .entries(Some(index), RECENT_FILE_LIMIT, now_ms())
463                    .into_iter()
464                    .enumerate()
465                    .map(|(rank, entry)| (entry.path, rank))
466                    .collect()
467            })
468            .unwrap_or_default()
469    }
470}
471
472impl WorkspacePathResolver for ManifestWorkspaceBackend {
473    fn normalize(&self, input: &str) -> Result<WorkspacePath> {
474        self.local.normalize(input)
475    }
476}
477
478#[async_trait]
479impl WorkspaceFileSystem for ManifestWorkspaceBackend {
480    async fn read_text(&self, path: &WorkspacePath) -> WorkspaceResult<String> {
481        let content = self.local.read_text(path).await?;
482        self.manifest.touch_file(path.as_str());
483        Ok(content)
484    }
485
486    async fn write_text(
487        &self,
488        path: &WorkspacePath,
489        content: &str,
490    ) -> WorkspaceResult<WorkspaceWriteOutcome> {
491        let outcome = self.local.write_text(path, content).await?;
492        self.manifest.touch_file(path.as_str());
493        Ok(outcome)
494    }
495
496    async fn list_dir(&self, path: &WorkspacePath) -> WorkspaceResult<Vec<WorkspaceDirEntry>> {
497        self.local.list_dir(path).await
498    }
499}
500
501#[async_trait]
502impl WorkspaceTextReader for ManifestWorkspaceBackend {
503    async fn read_text_range(
504        &self,
505        path: &WorkspacePath,
506        offset: usize,
507        limit: usize,
508    ) -> WorkspaceResult<WorkspaceTextRange> {
509        let range = self.local.read_text_range(path, offset, limit).await?;
510        self.manifest.touch_file(path.as_str());
511        Ok(range)
512    }
513}
514
515#[async_trait]
516impl WorkspaceCommandRunner for ManifestWorkspaceBackend {
517    async fn exec(&self, request: CommandRequest) -> Result<CommandOutput> {
518        self.local.exec(request).await
519    }
520}
521
522#[async_trait]
523impl WorkspaceSearch for ManifestWorkspaceBackend {
524    async fn glob(&self, request: WorkspaceGlobRequest) -> Result<WorkspaceGlobResult> {
525        validate_relative_pattern(&request.pattern, "glob pattern")?;
526        let Some(search_snapshot) = self.manifest_ready() else {
527            return self.fallback_search().glob(request).await;
528        };
529        let pattern = glob::Pattern::new(&request.pattern)
530            .map_err(|e| anyhow!("Invalid glob pattern '{}': {}", request.pattern, e))?;
531        let candidates =
532            candidate_indices_for_glob(&search_snapshot.index, &request.base, &request.pattern);
533        let recent_ranks = self.recent_path_ranks(&search_snapshot.index);
534
535        let mut matches = Vec::new();
536        for file_index in
537            recent_first_candidate_indices(&candidates, &search_snapshot.index, &recent_ranks)
538        {
539            let Some(file) = search_snapshot.snapshot.files.get(file_index) else {
540                continue;
541            };
542            let Some(relative_to_base) = relative_to_base(&file.path, &request.base) else {
543                continue;
544            };
545            if glob_matches(&pattern, relative_to_base) {
546                matches.push(WorkspacePath::from_normalized(file.path.clone()));
547            }
548        }
549
550        sort_paths_by_recent(&mut matches, &recent_ranks);
551        Ok(WorkspaceGlobResult { matches })
552    }
553
554    async fn grep(&self, request: WorkspaceGrepRequest) -> Result<WorkspaceGrepResult> {
555        Ok(self.grep_with_sources(request).await?.result)
556    }
557
558    async fn grep_with_sources(
559        &self,
560        request: WorkspaceGrepRequest,
561    ) -> Result<WorkspaceGrepOutcome> {
562        if let Some(ref glob) = request.glob {
563            validate_relative_pattern(glob, "grep glob filter")?;
564        }
565        self.local.ensure_search_base_allowed(&request.base)?;
566        let Some(search_snapshot) = self.manifest_ready() else {
567            return self.fallback_search().grep_with_sources(request).await;
568        };
569
570        let regex_pattern = if request.case_insensitive {
571            format!("(?i){}", request.pattern)
572        } else {
573            request.pattern.clone()
574        };
575        let regex = regex::Regex::new(&regex_pattern)
576            .map_err(|e| anyhow!("Invalid regex pattern '{}': {}", request.pattern, e))?;
577        let glob = request
578            .glob
579            .as_deref()
580            .map(glob::Pattern::new)
581            .transpose()
582            .map_err(|e| anyhow!("Invalid grep glob filter: {e}"))?;
583
584        let mut output = String::new();
585        let mut match_count = 0;
586        let mut file_count = 0;
587        let mut total_size = 0;
588        let mut matched_paths = Vec::new();
589
590        let candidates = request
591            .glob
592            .as_deref()
593            .map(|glob| candidate_indices_for_glob(&search_snapshot.index, &request.base, glob))
594            .unwrap_or_else(|| CandidateIndices::Indexed(&search_snapshot.index.all));
595        let recent_ranks = self.recent_path_ranks(&search_snapshot.index);
596
597        for file_index in
598            recent_first_candidate_indices(&candidates, &search_snapshot.index, &recent_ranks)
599        {
600            let Some(file) = search_snapshot.snapshot.files.get(file_index) else {
601                continue;
602            };
603            if file.binary {
604                continue;
605            }
606            let Some(relative_to_base) = relative_to_base(&file.path, &request.base) else {
607                continue;
608            };
609            if let Some(glob) = &glob {
610                if !glob_matches(glob, relative_to_base) {
611                    continue;
612                }
613            }
614
615            let workspace_path = WorkspacePath::from_normalized(file.path.clone());
616            let Some(content) = self.local.read_search_file(&workspace_path) else {
617                continue;
618            };
619            let lines: Vec<&str> = content.lines().collect();
620            let file_matches = lines
621                .iter()
622                .enumerate()
623                .filter_map(|(line_idx, line)| regex.is_match(line).then_some(line_idx))
624                .collect::<Vec<_>>();
625
626            if file_matches.is_empty() {
627                continue;
628            }
629
630            file_count += 1;
631            let display_path = escape_control_chars_for_display(&file.path);
632            let mut path_recorded = false;
633            for &match_idx in &file_matches {
634                if total_size > request.max_output_size {
635                    return Ok(WorkspaceGrepOutcome {
636                        result: WorkspaceGrepResult {
637                            output,
638                            match_count,
639                            file_count,
640                            truncated: true,
641                        },
642                        matched_paths: Some(matched_paths),
643                    });
644                }
645
646                if !path_recorded {
647                    matched_paths.push(workspace_path.clone());
648                    path_recorded = true;
649                }
650                match_count += 1;
651                let start = match_idx.saturating_sub(request.context_lines);
652                let end = (match_idx + request.context_lines + 1).min(lines.len());
653
654                for (i, line) in lines[start..end].iter().enumerate() {
655                    let abs_i = start + i;
656                    let prefix = if abs_i == match_idx { ">" } else { " " };
657                    let line = format!("{}{}:{}: {}\n", prefix, display_path, abs_i + 1, line);
658                    total_size += line.len();
659                    output.push_str(&line);
660                }
661
662                if request.context_lines > 0 {
663                    output.push_str("--\n");
664                    total_size += 3;
665                }
666            }
667        }
668
669        Ok(WorkspaceGrepOutcome {
670            result: WorkspaceGrepResult {
671                output,
672                match_count,
673                file_count,
674                truncated: false,
675            },
676            matched_paths: Some(matched_paths),
677        })
678    }
679}
680
681#[async_trait]
682impl WorkspaceGit for ManifestWorkspaceBackend {
683    async fn is_repository(&self) -> Result<bool> {
684        self.local.is_repository().await
685    }
686
687    async fn status(&self) -> Result<WorkspaceGitStatus> {
688        self.local.status().await
689    }
690
691    async fn log(&self, max_count: usize) -> Result<Vec<WorkspaceGitCommit>> {
692        self.local.log(max_count).await
693    }
694
695    async fn list_branches(&self) -> Result<Vec<WorkspaceGitBranch>> {
696        self.local.list_branches().await
697    }
698
699    async fn create_branch(&self, request: WorkspaceGitCreateBranchRequest) -> Result<()> {
700        self.local.create_branch(request).await
701    }
702
703    async fn checkout(
704        &self,
705        request: WorkspaceGitCheckoutRequest,
706    ) -> Result<WorkspaceGitCheckoutOutput> {
707        self.local.checkout(request).await
708    }
709
710    async fn diff(&self, request: WorkspaceGitDiffRequest) -> Result<String> {
711        self.local.diff(request).await
712    }
713
714    async fn list_remotes(&self) -> Result<Vec<WorkspaceGitRemote>> {
715        self.local.list_remotes().await
716    }
717}
718
719#[async_trait]
720impl WorkspaceGitStashProvider for ManifestWorkspaceBackend {
721    async fn list_stashes(&self) -> Result<Vec<WorkspaceGitStash>> {
722        self.local.list_stashes().await
723    }
724
725    async fn stash(&self, request: WorkspaceGitStashRequest) -> Result<()> {
726        self.local.stash(request).await
727    }
728}
729
730#[async_trait]
731impl WorkspaceGitWorktreeProvider for ManifestWorkspaceBackend {
732    async fn list_worktrees(&self) -> Result<Vec<WorkspaceGitWorktree>> {
733        self.local.list_worktrees().await
734    }
735
736    async fn create_worktree(
737        &self,
738        request: WorkspaceGitCreateWorktreeRequest,
739    ) -> Result<WorkspaceGitWorktreeMutation> {
740        self.local.create_worktree(request).await
741    }
742
743    async fn remove_worktree(
744        &self,
745        request: WorkspaceGitRemoveWorktreeRequest,
746    ) -> Result<WorkspaceGitWorktreeMutation> {
747        self.local.remove_worktree(request).await
748    }
749}
750
751fn update_state(
752    state: &Arc<RwLock<ManifestState>>,
753    files: Vec<LocalWorkspaceFile>,
754) -> Option<LocalWorkspaceManifestSnapshot> {
755    let fingerprint = fingerprint_files(&files);
756    let index = Arc::new(ManifestIndex::build(&files));
757    let Ok(mut state) = state.write() else {
758        return None;
759    };
760    if state.snapshot.version > 0 && state.fingerprint == fingerprint {
761        return None;
762    }
763    state.fingerprint = fingerprint;
764    state.index = index;
765    state.snapshot = Arc::new(LocalWorkspaceManifestSnapshot {
766        version: state.snapshot.version + 1,
767        root: state.snapshot.root.clone(),
768        files,
769        scanned_at_ms: now_ms(),
770    });
771    Some((*state.snapshot).clone())
772}
773
774enum CandidateIndices<'a> {
775    Indexed(&'a [usize]),
776    Single(Option<usize>),
777}
778
779impl<'a> CandidateIndices<'a> {
780    fn iter(&self) -> Box<dyn Iterator<Item = usize> + '_> {
781        match self {
782            Self::Indexed(indices) => Box::new(indices.iter().copied()),
783            Self::Single(Some(index)) => Box::new(std::iter::once(*index)),
784            Self::Single(None) => Box::new(std::iter::empty()),
785        }
786    }
787
788    fn len(&self) -> usize {
789        match self {
790            Self::Indexed(indices) => indices.len(),
791            Self::Single(Some(_)) => 1,
792            Self::Single(None) => 0,
793        }
794    }
795
796    fn contains(&self, index: usize) -> bool {
797        match self {
798            Self::Indexed(indices) => indices.contains(&index),
799            Self::Single(Some(candidate)) => *candidate == index,
800            Self::Single(None) => false,
801        }
802    }
803}
804
805fn recent_first_candidate_indices(
806    candidates: &CandidateIndices<'_>,
807    index: &ManifestIndex,
808    recent_ranks: &HashMap<String, usize>,
809) -> Vec<usize> {
810    if recent_ranks.is_empty() {
811        return candidates.iter().collect();
812    }
813
814    let mut hot = recent_ranks
815        .iter()
816        .filter_map(|(path, rank)| {
817            let file_index = *index.by_path.get(path)?;
818            candidates
819                .contains(file_index)
820                .then_some((*rank, file_index))
821        })
822        .collect::<Vec<_>>();
823    hot.sort_unstable_by_key(|(rank, _)| *rank);
824
825    let mut out = Vec::with_capacity(candidates.len());
826    let mut seen = HashSet::with_capacity(hot.len());
827    for (_, file_index) in hot {
828        if seen.insert(file_index) {
829            out.push(file_index);
830        }
831    }
832    out.extend(
833        candidates
834            .iter()
835            .filter(|file_index| !seen.contains(file_index)),
836    );
837    out
838}
839
840fn sort_paths_by_recent(paths: &mut [WorkspacePath], recent_ranks: &HashMap<String, usize>) {
841    paths.sort_by(|left, right| {
842        recent_ranks
843            .get(left.as_str())
844            .copied()
845            .unwrap_or(usize::MAX)
846            .cmp(
847                &recent_ranks
848                    .get(right.as_str())
849                    .copied()
850                    .unwrap_or(usize::MAX),
851            )
852            .then_with(|| left.as_str().cmp(right.as_str()))
853    });
854}
855
856fn candidate_indices_for_glob<'a>(
857    index: &'a ManifestIndex,
858    base: &WorkspacePath,
859    pattern: &str,
860) -> CandidateIndices<'a> {
861    if !has_glob_meta(pattern) && pattern.contains('/') {
862        return CandidateIndices::Single(
863            literal_workspace_path(base, pattern)
864                .and_then(|path| index.by_path.get(&path).copied()),
865        );
866    }
867
868    if let Some(name) = literal_terminal_segment(pattern) {
869        return index
870            .by_basename
871            .get(name)
872            .map(|indices| CandidateIndices::Indexed(indices))
873            .unwrap_or(CandidateIndices::Single(None));
874    }
875
876    if let Some(extension) = simple_extension_terminal(pattern) {
877        return index
878            .by_extension
879            .get(extension)
880            .map(|indices| CandidateIndices::Indexed(indices))
881            .unwrap_or(CandidateIndices::Single(None));
882    }
883
884    CandidateIndices::Indexed(&index.all)
885}
886
887fn literal_workspace_path(base: &WorkspacePath, pattern: &str) -> Option<String> {
888    let pattern = normalize_relative_path_lossy(Path::new(pattern))?;
889    if pattern.is_empty() {
890        return None;
891    }
892    if base.is_root() {
893        Some(pattern)
894    } else {
895        Some(format!(
896            "{}/{}",
897            base.as_str().trim_end_matches('/'),
898            pattern
899        ))
900    }
901}
902
903fn literal_terminal_segment(pattern: &str) -> Option<&str> {
904    let terminal = pattern
905        .trim_end_matches('/')
906        .rsplit('/')
907        .next()
908        .filter(|segment| !segment.is_empty())?;
909    (!has_glob_meta(terminal)).then_some(terminal)
910}
911
912fn simple_extension_terminal(pattern: &str) -> Option<&str> {
913    let terminal = pattern.trim_end_matches('/').rsplit('/').next()?;
914    let extension = terminal.strip_prefix("*.")?;
915    (!extension.is_empty() && !has_glob_meta(extension)).then_some(extension)
916}
917
918fn has_glob_meta(pattern: &str) -> bool {
919    pattern
920        .bytes()
921        .any(|byte| matches!(byte, b'*' | b'?' | b'[' | b']' | b'{' | b'}'))
922}
923
924fn fingerprint_files(files: &[LocalWorkspaceFile]) -> u64 {
925    let mut hasher = DefaultHasher::new();
926    files.hash(&mut hasher);
927    hasher.finish()
928}
929
930fn recent_score(entry: &RecentFileState, now: u64) -> f32 {
931    let age_ms = now.saturating_sub(entry.touched_at_ms) as f32;
932    let recency = (-age_ms / RECENT_DECAY_HALF_LIFE_MS).exp();
933    let frequency =
934        ((entry.touch_count as f32) + 1.0).ln() / (RECENT_FREQUENCY_NORMALIZER + 1.0).ln();
935    RECENT_RECENCY_WEIGHT * recency + (1.0 - RECENT_RECENCY_WEIGHT) * frequency.min(1.0)
936}
937
938fn normalize_recent_file_path(path: &str) -> Option<String> {
939    let path = path.trim();
940    if path.is_empty() {
941        return None;
942    }
943    let normalized = normalize_relative_path_lossy(Path::new(path))?;
944    (!normalized.is_empty()).then_some(normalized)
945}
946
947fn normalize_relative_path_lossy(path: &Path) -> Option<String> {
948    let mut parts = Vec::new();
949    for component in path.components() {
950        match component {
951            Component::Normal(part) => parts.push(part.to_string_lossy().into_owned()),
952            Component::CurDir => {}
953            _ => return None,
954        }
955    }
956    Some(parts.join("/"))
957}
958
959fn relative_to_base<'a>(path: &'a str, base: &WorkspacePath) -> Option<&'a str> {
960    if base.is_root() {
961        return Some(path);
962    }
963    let base = base.as_str().trim_end_matches('/');
964    if path == base {
965        Some("")
966    } else {
967        path.strip_prefix(base)
968            .and_then(|tail| tail.strip_prefix('/'))
969            .filter(|tail| !tail.is_empty())
970    }
971}
972
973fn glob_matches(pattern: &glob::Pattern, path: &str) -> bool {
974    let path = Path::new(path);
975    pattern.matches_path(path)
976        || path
977            .file_name()
978            .and_then(|name| name.to_str())
979            .is_some_and(|name| pattern.matches(name))
980}
981
982fn now_ms() -> u64 {
983    system_time_ms(SystemTime::now())
984}
985
986fn system_time_ms(time: SystemTime) -> u64 {
987    time.duration_since(UNIX_EPOCH)
988        .map(|duration| duration.as_millis() as u64)
989        .unwrap_or_default()
990}
991
992#[cfg(test)]
993#[path = "manifest/tests.rs"]
994mod tests;