agentdir 0.1.5

Virtual filesystem for agent-optimized file exploration using CoW reflinks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::error::{AgentdirError, Result};
use crate::types::{
    CatalogEntry, EntryType, Manifest, SourceMetadata, SourcePath, SourceRoot, VirtualPath,
};

/// In-memory virtual filesystem catalog.
///
/// Maps virtual paths to source file locations. Pure data — no filesystem operations.
#[derive(Clone)]
pub struct Catalog {
    /// The persisted manifest (source of truth for entries and source roots).
    pub manifest: Manifest,
    /// The root directory where files are materialized.
    pub materialized_root: PathBuf,
    /// O(1) lookup index: virtual path string → index into manifest.entries.
    entry_index: HashMap<String, usize>,
}

impl Catalog {
    /// Create a new empty catalog.
    pub fn new(materialized_root: PathBuf) -> Self {
        Self {
            manifest: Manifest::new(),
            materialized_root,
            entry_index: HashMap::new(),
        }
    }

    /// Create a catalog from an existing manifest.
    pub fn from_manifest(manifest: Manifest, materialized_root: PathBuf) -> Self {
        let mut catalog = Self {
            manifest,
            materialized_root,
            entry_index: HashMap::new(),
        };
        catalog.rebuild_index();
        catalog
    }

    /// Rebuild the entry index from manifest entries.
    fn rebuild_index(&mut self) {
        self.entry_index.clear();
        for (index, entry) in self.manifest.entries.iter().enumerate() {
            self.entry_index
                .insert(entry.virtual_path.as_str().to_string(), index);
        }
    }

    /// Register a source root mapping.
    ///
    /// Rejects overlaps with the materialized root and with already-registered
    /// source roots so the same real files cannot be cataloged twice.
    pub fn add_source_root(&mut self, source_root: SourceRoot) -> Result<()> {
        Self::validate_no_overlap(source_root.source_path.as_path(), &self.materialized_root)?;

        for existing in &self.manifest.source_roots {
            let new_path = comparable_path(source_root.source_path.as_path());
            let existing_path = comparable_path(existing.source_path.as_path());
            if new_path.starts_with(&existing_path) || existing_path.starts_with(&new_path) {
                return Err(AgentdirError::PathOverlap(format!(
                    "source root {:?} overlaps existing source root {:?}",
                    source_root.source_path.as_path(),
                    existing.source_path.as_path()
                )));
            }
        }

        self.manifest.source_roots.push(source_root);
        self.manifest.touch();
        Ok(())
    }

    /// Add pre-scanned entries to the catalog. Validates no duplicate virtual paths.
    ///
    /// The scanning itself is done by a backend; Catalog stays pure.
    pub fn add_entries(&mut self, entries: Vec<CatalogEntry>) -> Result<()> {
        for entry in entries {
            let key = entry.virtual_path.as_str().to_string();
            if self.entry_index.contains_key(&key) {
                return Err(AgentdirError::EntryExists(key));
            }

            let index = self.manifest.entries.len();
            self.entry_index.insert(key, index);
            self.manifest.entries.push(entry);
        }
        self.manifest.touch();
        Ok(())
    }

    /// Remove all entries under a virtual mount point. Returns the removed entries.
    pub fn unmap(&mut self, virtual_mount: &VirtualPath) -> Result<Vec<CatalogEntry>> {
        let prefix = virtual_mount.as_str();
        let child_prefix = child_prefix(prefix);
        let mut removed = Vec::new();
        let mut remaining = Vec::new();

        for entry in self.manifest.entries.drain(..) {
            let entry_path = entry.virtual_path.as_str();
            if entry_path == prefix || entry_path.starts_with(&child_prefix) {
                removed.push(entry);
            } else {
                remaining.push(entry);
            }
        }

        self.manifest.entries = remaining;
        self.manifest
            .source_roots
            .retain(|root| root.virtual_mount.as_str() != prefix);
        self.rebuild_index();
        self.manifest.touch();
        Ok(removed)
    }

    /// Create a virtual directory with no source backing.
    pub fn mkdir(&mut self, path: &VirtualPath) -> Result<()> {
        let key = path.as_str().to_string();
        if self.entry_index.contains_key(&key) {
            return Err(AgentdirError::EntryExists(key));
        }

        let entry = CatalogEntry {
            virtual_path: path.clone(),
            source_path: SourcePath::new(PathBuf::new()),
            content_hash: None,
            metadata: SourceMetadata {
                mtime_ns: 0,
                size_bytes: 0,
                entry_type: EntryType::Directory,
            },
            materialized: false,
        };

        let index = self.manifest.entries.len();
        self.entry_index.insert(key, index);
        self.manifest.entries.push(entry);
        self.manifest.touch();
        Ok(())
    }

    /// Remove a virtual directory. Fails if not empty unless recursive is true.
    pub fn rmdir(&mut self, path: &VirtualPath, recursive: bool) -> Result<()> {
        let prefix = path.as_str();
        let child_prefix = child_prefix(prefix);
        let has_children = self.manifest.entries.iter().any(|entry| {
            let entry_path = entry.virtual_path.as_str();
            entry_path != prefix && entry_path.starts_with(&child_prefix)
        });

        if has_children && !recursive {
            return Err(AgentdirError::EntryExists(format!(
                "directory {prefix} is not empty"
            )));
        }

        self.manifest.entries.retain(|entry| {
            let entry_path = entry.virtual_path.as_str();
            if recursive {
                entry_path != prefix && !entry_path.starts_with(&child_prefix)
            } else {
                entry_path != prefix
            }
        });

        self.rebuild_index();
        self.manifest.touch();
        Ok(())
    }

    /// Move/rename an entry or directory subtree in the virtual namespace.
    pub fn mv(&mut self, from: &VirtualPath, to: &VirtualPath) -> Result<()> {
        let from_key = from.as_str().to_string();
        let to_key = to.as_str().to_string();

        if !self.entry_index.contains_key(&from_key) {
            return Err(AgentdirError::EntryNotFound(from_key));
        }
        if self.entry_index.contains_key(&to_key) {
            return Err(AgentdirError::EntryExists(to_key));
        }

        let affected = self.affected_indices(from);
        let rebased: Vec<VirtualPath> = affected
            .iter()
            .map(|&index| rebase_virtual_path(&self.manifest.entries[index].virtual_path, from, to))
            .collect::<Result<_>>()?;

        for new_path in &rebased {
            if self.entry_index.contains_key(new_path.as_str()) {
                return Err(AgentdirError::EntryExists(new_path.as_str().to_string()));
            }
        }

        for (index, new_path) in affected.into_iter().zip(rebased) {
            self.manifest.entries[index].virtual_path = new_path;
        }
        self.rebuild_index();
        self.manifest.touch();
        Ok(())
    }

    /// Copy an entry or directory subtree to a new virtual path, preserving source references.
    pub fn cp(&mut self, from: &VirtualPath, to: &VirtualPath) -> Result<()> {
        let from_key = from.as_str().to_string();
        let to_key = to.as_str().to_string();

        if !self.entry_index.contains_key(&from_key) {
            return Err(AgentdirError::EntryNotFound(from_key));
        }
        if self.entry_index.contains_key(&to_key) {
            return Err(AgentdirError::EntryExists(to_key));
        }

        let affected = self.affected_indices(from);
        let mut new_entries = Vec::with_capacity(affected.len());
        for index in affected {
            let mut new_entry = self.manifest.entries[index].clone();
            new_entry.virtual_path = rebase_virtual_path(&new_entry.virtual_path, from, to)?;
            new_entry.materialized = false;
            if self
                .entry_index
                .contains_key(new_entry.virtual_path.as_str())
            {
                return Err(AgentdirError::EntryExists(
                    new_entry.virtual_path.as_str().to_string(),
                ));
            }
            new_entries.push(new_entry);
        }

        for entry in new_entries {
            let key = entry.virtual_path.as_str().to_string();
            let new_index = self.manifest.entries.len();
            self.entry_index.insert(key, new_index);
            self.manifest.entries.push(entry);
        }
        self.manifest.touch();
        Ok(())
    }

    /// Return cloned entries affected by an exact path or subtree operation.
    pub fn entries_under(&self, path: &VirtualPath) -> Result<Vec<CatalogEntry>> {
        if !self.entry_index.contains_key(path.as_str()) {
            return Err(AgentdirError::EntryNotFound(path.as_str().to_string()));
        }
        Ok(self
            .affected_indices(path)
            .into_iter()
            .map(|index| self.manifest.entries[index].clone())
            .collect())
    }

    fn affected_indices(&self, path: &VirtualPath) -> Vec<usize> {
        let prefix = path.as_str();
        let child_prefix = child_prefix(prefix);
        self.manifest
            .entries
            .iter()
            .enumerate()
            .filter_map(|(index, entry)| {
                let entry_path = entry.virtual_path.as_str();
                (entry_path == prefix || entry_path.starts_with(&child_prefix)).then_some(index)
            })
            .collect()
    }

    /// Rename an entry without changing its parent directory.
    pub fn rename(&mut self, path: &VirtualPath, new_name: &str) -> Result<()> {
        validate_new_name(new_name)?;
        let parent = path
            .parent()
            .ok_or_else(|| AgentdirError::InvalidPath("cannot rename root".into()))?;
        let separator = if parent.as_str() == "/" { "" } else { "/" };
        let new_path = VirtualPath::new(format!("{}{separator}{new_name}", parent.as_str()))?;
        self.mv(path, &new_path)
    }

    /// List all direct children of a virtual directory.
    pub fn list(&self, path: &VirtualPath) -> Result<Vec<&CatalogEntry>> {
        let prefix = path.as_str();
        let child_prefix = child_prefix(prefix);
        let mut children = Vec::new();

        for entry in &self.manifest.entries {
            let entry_path = entry.virtual_path.as_str();
            if entry_path == prefix {
                continue;
            }

            if let Some(rest) = entry_path.strip_prefix(&child_prefix) {
                if !rest.contains('/') {
                    children.push(entry);
                }
            }
        }

        Ok(children)
    }

    /// Get a single entry by virtual path.
    pub fn get(&self, path: &VirtualPath) -> Result<&CatalogEntry> {
        let key = path.as_str();
        let index = self
            .entry_index
            .get(key)
            .ok_or_else(|| AgentdirError::EntryNotFound(key.to_string()))?;
        Ok(&self.manifest.entries[*index])
    }

    /// Get a mutable reference to a single entry by virtual path.
    pub fn get_mut(&mut self, path: &VirtualPath) -> Result<&mut CatalogEntry> {
        let key = path.as_str().to_string();
        let index = *self
            .entry_index
            .get(&key)
            .ok_or(AgentdirError::EntryNotFound(key))?;
        Ok(&mut self.manifest.entries[index])
    }

    /// Resolve a virtual path to its source path.
    pub fn resolve(&self, virtual_path: &VirtualPath) -> Result<&SourcePath> {
        Ok(&self.get(virtual_path)?.source_path)
    }

    /// Find an entry by its source path.
    pub fn find_by_source(&self, source: &SourcePath) -> Option<&CatalogEntry> {
        self.manifest
            .entries
            .iter()
            .find(|entry| entry.source_path.as_path() == source.as_path())
    }

    /// Find ALL entries that reference a given source path.
    /// Returns entries for 1:N mappings (e.g., files duplicated via `cp`).
    pub fn find_all_by_source(&self, source: &SourcePath) -> Vec<&CatalogEntry> {
        self.manifest
            .entries
            .iter()
            .filter(|entry| entry.source_path.as_path() == source.as_path())
            .collect()
    }

    /// All entries in the catalog.
    pub fn entries(&self) -> &[CatalogEntry] {
        &self.manifest.entries
    }

    /// All source roots.
    pub fn source_roots(&self) -> &[SourceRoot] {
        &self.manifest.source_roots
    }

    /// Total number of entries.
    pub fn len(&self) -> usize {
        self.manifest.entries.len()
    }

    /// Whether the catalog is empty.
    pub fn is_empty(&self) -> bool {
        self.manifest.entries.is_empty()
    }

    /// Validate that source and materialized paths don't overlap.
    pub fn validate_no_overlap(source: &Path, materialized: &Path) -> Result<()> {
        let comparable_source = comparable_path(source);
        let comparable_materialized = comparable_path(materialized);
        if comparable_source.starts_with(&comparable_materialized)
            || comparable_materialized.starts_with(&comparable_source)
        {
            return Err(AgentdirError::PathOverlap(format!(
                "source {source:?} and materialized {materialized:?} overlap"
            )));
        }
        Ok(())
    }
}

fn comparable_path(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| {
        if path.is_absolute() {
            path.to_path_buf()
        } else {
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(path)
        }
    })
}

fn child_prefix(path: &str) -> String {
    if path == "/" {
        "/".to_string()
    } else {
        format!("{path}/")
    }
}

fn rebase_virtual_path(
    path: &VirtualPath,
    from: &VirtualPath,
    to: &VirtualPath,
) -> Result<VirtualPath> {
    if path.as_str() == from.as_str() {
        return Ok(to.clone());
    }

    let from_child_prefix = child_prefix(from.as_str());
    let rest = path
        .as_str()
        .strip_prefix(&from_child_prefix)
        .ok_or_else(|| AgentdirError::InvalidPath(format!("{} is not under {}", path, from)))?;
    let separator = if to.as_str() == "/" { "" } else { "/" };
    VirtualPath::new(format!("{}{separator}{rest}", to.as_str()))
}

fn validate_new_name(new_name: &str) -> Result<()> {
    if new_name.contains('/') || new_name.contains('\\') {
        return Err(AgentdirError::InvalidPath(
            "new name must not contain path separators".into(),
        ));
    }
    if new_name.is_empty() {
        return Err(AgentdirError::InvalidPath(
            "new name must not be empty".into(),
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_entry(virtual_path: &str, source_path: &str) -> CatalogEntry {
        CatalogEntry {
            virtual_path: VirtualPath::new(virtual_path).unwrap(),
            source_path: SourcePath::new(PathBuf::from(source_path)),
            content_hash: None,
            metadata: SourceMetadata {
                mtime_ns: 1000,
                size_bytes: 100,
                entry_type: EntryType::File,
            },
            materialized: false,
        }
    }

    fn make_catalog() -> Catalog {
        Catalog::new(std::env::temp_dir().join("agentdir_test_materialized"))
    }

    #[test]
    fn test_add_entries_and_list() {
        let mut catalog = make_catalog();
        let entries = vec![
            make_entry("/docs/readme.md", "/src/readme.md"),
            make_entry("/docs/guide.md", "/src/guide.md"),
        ];
        catalog.add_entries(entries).unwrap();

        let children = catalog.list(&VirtualPath::new("/docs").unwrap()).unwrap();
        assert_eq!(children.len(), 2);
    }

    #[test]
    fn test_mv_preserves_source() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![make_entry("/docs/old.md", "/src/old.md")])
            .unwrap();

        catalog
            .mv(
                &VirtualPath::new("/docs/old.md").unwrap(),
                &VirtualPath::new("/docs/new.md").unwrap(),
            )
            .unwrap();

        assert!(catalog
            .get(&VirtualPath::new("/docs/old.md").unwrap())
            .is_err());
        let entry = catalog
            .get(&VirtualPath::new("/docs/new.md").unwrap())
            .unwrap();
        assert_eq!(entry.source_path.as_path(), Path::new("/src/old.md"));
    }

    #[test]
    fn test_cp_same_source() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![make_entry("/docs/file.md", "/src/file.md")])
            .unwrap();

        catalog
            .cp(
                &VirtualPath::new("/docs/file.md").unwrap(),
                &VirtualPath::new("/backup/file.md").unwrap(),
            )
            .unwrap();

        let orig = catalog
            .get(&VirtualPath::new("/docs/file.md").unwrap())
            .unwrap();
        let copy = catalog
            .get(&VirtualPath::new("/backup/file.md").unwrap())
            .unwrap();
        assert_eq!(orig.source_path.as_path(), copy.source_path.as_path());
    }

    #[test]
    fn test_overlap_rejection() {
        let tmp = std::env::temp_dir();
        let result = Catalog::validate_no_overlap(
            &tmp.join("materialized/subdir"),
            &tmp.join("materialized"),
        );
        assert!(matches!(result, Err(AgentdirError::PathOverlap(_))));

        let result = Catalog::validate_no_overlap(&tmp.join("source"), &tmp.join("source/mat"));
        assert!(matches!(result, Err(AgentdirError::PathOverlap(_))));

        let result = Catalog::validate_no_overlap(&tmp.join("source"), &tmp.join("materialized"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_unmap_removes_entries() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![
                make_entry("/docs/a.md", "/src/a.md"),
                make_entry("/docs/b.md", "/src/b.md"),
                make_entry("/other/c.md", "/src/c.md"),
            ])
            .unwrap();

        let removed = catalog.unmap(&VirtualPath::new("/docs").unwrap()).unwrap();
        assert_eq!(removed.len(), 2);
        assert_eq!(catalog.len(), 1);
        assert!(catalog
            .get(&VirtualPath::new("/other/c.md").unwrap())
            .is_ok());
    }

    #[test]
    fn test_mkdir_and_rmdir() {
        let mut catalog = make_catalog();
        catalog.mkdir(&VirtualPath::new("/mydir").unwrap()).unwrap();
        assert!(catalog.get(&VirtualPath::new("/mydir").unwrap()).is_ok());

        catalog
            .rmdir(&VirtualPath::new("/mydir").unwrap(), false)
            .unwrap();
        assert!(catalog.get(&VirtualPath::new("/mydir").unwrap()).is_err());
    }

    #[test]
    fn test_rmdir_fails_if_not_empty() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![make_entry("/docs/file.md", "/src/file.md")])
            .unwrap();

        let result = catalog.rmdir(&VirtualPath::new("/docs").unwrap(), false);
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_returns_source() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![make_entry("/docs/readme.md", "/src/readme.md")])
            .unwrap();

        let source = catalog
            .resolve(&VirtualPath::new("/docs/readme.md").unwrap())
            .unwrap();
        assert_eq!(source.as_path(), Path::new("/src/readme.md"));
    }

    #[test]
    fn test_entry_index_consistency_after_mv() {
        let mut catalog = make_catalog();
        catalog
            .add_entries(vec![make_entry("/a/b.md", "/src/b.md")])
            .unwrap();
        catalog
            .mv(
                &VirtualPath::new("/a/b.md").unwrap(),
                &VirtualPath::new("/c/d.md").unwrap(),
            )
            .unwrap();

        assert!(catalog.get(&VirtualPath::new("/a/b.md").unwrap()).is_err());
        assert!(catalog.get(&VirtualPath::new("/c/d.md").unwrap()).is_ok());
        assert_eq!(catalog.len(), 1);
    }
}