agentvfs 0.1.6

Virtual filesystem CLI backed by embedded databases for AI agents
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
//! In-memory cache layer using rkyv for zero-copy serialization.
//!
//! This module provides a thread-safe LRU cache for frequently accessed
//! filesystem metadata, reducing database queries for hot paths.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

use parking_lot::RwLock;
use rkyv::{
    Archive, Deserialize, Serialize,
    rancor::Error as RkyvError,
    to_bytes, access,
};

/// Maximum number of entries in the cache.
const DEFAULT_MAX_ENTRIES: usize = 10_000;

/// Maximum number of directory listing entries to cache.
const DEFAULT_MAX_DIR_ENTRIES: usize = 1_000;

/// A cached file entry with rkyv serialization support.
#[derive(Archive, Deserialize, Serialize, Debug, Clone)]
pub struct CachedFileEntry {
    /// Unique identifier.
    pub id: i64,
    /// Parent directory ID.
    pub parent_id: Option<i64>,
    /// File or directory name.
    pub name: String,
    /// Whether this is a directory.
    pub is_dir: bool,
    /// Size in bytes.
    pub size: u64,
    /// SHA-256 hash as hex string (None for directories).
    pub content_hash: Option<String>,
    /// Creation timestamp (Unix timestamp).
    pub created_at: i64,
    /// Last modification timestamp (Unix timestamp).
    pub modified_at: i64,
}

/// A cached directory listing.
#[derive(Archive, Deserialize, Serialize, Debug, Clone)]
pub struct CachedDirListing {
    /// Parent directory path.
    pub path: String,
    /// List of entries in the directory.
    pub entries: Vec<CachedDirEntry>,
    /// When this listing was cached (Unix timestamp).
    pub cached_at: i64,
}

/// A single entry in a cached directory listing.
#[derive(Archive, Deserialize, Serialize, Debug, Clone)]
pub struct CachedDirEntry {
    /// File or directory name.
    pub name: String,
    /// Whether this is a directory.
    pub is_dir: bool,
    /// Size in bytes.
    pub size: u64,
    /// Last modification timestamp (Unix timestamp).
    pub modified_at: i64,
}

/// Cache statistics.
#[derive(Debug, Default)]
pub struct CacheStats {
    /// Number of cache hits.
    pub hits: AtomicU64,
    /// Number of cache misses.
    pub misses: AtomicU64,
    /// Number of entries currently in cache.
    pub entries: AtomicU64,
}

impl CacheStats {
    /// Record a cache hit.
    pub fn hit(&self) {
        self.hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a cache miss.
    pub fn miss(&self) {
        self.misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get the hit rate as a percentage.
    pub fn hit_rate(&self) -> f64 {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            (hits as f64 / total as f64) * 100.0
        }
    }
}

/// Thread-safe LRU cache for file entries.
pub struct FileEntryCache {
    /// Cached entries keyed by path.
    entries: RwLock<HashMap<String, Vec<u8>>>,
    /// Maximum number of entries.
    max_entries: usize,
    /// Cache statistics.
    pub stats: CacheStats,
}

impl FileEntryCache {
    /// Create a new file entry cache.
    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_MAX_ENTRIES)
    }

    /// Create a new file entry cache with specified capacity.
    pub fn with_capacity(max_entries: usize) -> Self {
        Self {
            entries: RwLock::new(HashMap::with_capacity(max_entries / 4)),
            max_entries,
            stats: CacheStats::default(),
        }
    }

    /// Get a cached file entry by path.
    pub fn get(&self, path: &str) -> Option<CachedFileEntry> {
        let entries = self.entries.read();
        if let Some(bytes) = entries.get(path) {
            // Zero-copy access using rkyv
            match access::<ArchivedCachedFileEntry, RkyvError>(bytes) {
                Ok(archived) => {
                    self.stats.hit();
                    // Deserialize to owned type
                    match rkyv::deserialize::<CachedFileEntry, RkyvError>(archived) {
                        Ok(entry) => Some(entry),
                        Err(_) => None,
                    }
                }
                Err(_) => {
                    self.stats.miss();
                    None
                }
            }
        } else {
            self.stats.miss();
            None
        }
    }

    /// Insert a file entry into the cache.
    pub fn insert(&self, path: String, entry: CachedFileEntry) {
        // Serialize using rkyv
        match to_bytes::<RkyvError>(&entry) {
            Ok(bytes) => {
                let mut entries = self.entries.write();

                // Simple eviction: if at capacity, clear half the cache
                if entries.len() >= self.max_entries {
                    let to_remove: Vec<_> = entries.keys()
                        .take(self.max_entries / 2)
                        .cloned()
                        .collect();
                    for key in to_remove {
                        entries.remove(&key);
                    }
                }

                entries.insert(path, bytes.to_vec());
                self.stats.entries.store(entries.len() as u64, Ordering::Relaxed);
            }
            Err(_) => {
                // Failed to serialize, skip caching
            }
        }
    }

    /// Invalidate a cached entry by path.
    pub fn invalidate(&self, path: &str) {
        let mut entries = self.entries.write();
        entries.remove(path);
        self.stats.entries.store(entries.len() as u64, Ordering::Relaxed);
    }

    /// Invalidate all entries under a path prefix.
    pub fn invalidate_prefix(&self, prefix: &str) {
        let mut entries = self.entries.write();
        entries.retain(|k, _| !k.starts_with(prefix));
        self.stats.entries.store(entries.len() as u64, Ordering::Relaxed);
    }

    /// Clear all cached entries.
    pub fn clear(&self) {
        let mut entries = self.entries.write();
        entries.clear();
        self.stats.entries.store(0, Ordering::Relaxed);
    }

    /// Get the number of cached entries.
    pub fn len(&self) -> usize {
        self.entries.read().len()
    }

    /// Check if the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.read().is_empty()
    }
}

impl Default for FileEntryCache {
    fn default() -> Self {
        Self::new()
    }
}

/// Thread-safe cache for directory listings.
pub struct DirListingCache {
    /// Cached listings keyed by directory path.
    listings: RwLock<HashMap<String, Vec<u8>>>,
    /// Maximum number of listings.
    max_entries: usize,
    /// Cache statistics.
    pub stats: CacheStats,
}

impl DirListingCache {
    /// Create a new directory listing cache.
    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_MAX_DIR_ENTRIES)
    }

    /// Create a new directory listing cache with specified capacity.
    pub fn with_capacity(max_entries: usize) -> Self {
        Self {
            listings: RwLock::new(HashMap::with_capacity(max_entries / 4)),
            max_entries,
            stats: CacheStats::default(),
        }
    }

    /// Get a cached directory listing by path.
    pub fn get(&self, path: &str) -> Option<CachedDirListing> {
        let listings = self.listings.read();
        if let Some(bytes) = listings.get(path) {
            match access::<ArchivedCachedDirListing, RkyvError>(bytes) {
                Ok(archived) => {
                    self.stats.hit();
                    match rkyv::deserialize::<CachedDirListing, RkyvError>(archived) {
                        Ok(listing) => Some(listing),
                        Err(_) => None,
                    }
                }
                Err(_) => {
                    self.stats.miss();
                    None
                }
            }
        } else {
            self.stats.miss();
            None
        }
    }

    /// Insert a directory listing into the cache.
    pub fn insert(&self, path: String, listing: CachedDirListing) {
        match to_bytes::<RkyvError>(&listing) {
            Ok(bytes) => {
                let mut listings = self.listings.write();

                if listings.len() >= self.max_entries {
                    let to_remove: Vec<_> = listings.keys()
                        .take(self.max_entries / 2)
                        .cloned()
                        .collect();
                    for key in to_remove {
                        listings.remove(&key);
                    }
                }

                listings.insert(path, bytes.to_vec());
                self.stats.entries.store(listings.len() as u64, Ordering::Relaxed);
            }
            Err(_) => {
                // Failed to serialize, skip caching
            }
        }
    }

    /// Invalidate a cached listing by path.
    pub fn invalidate(&self, path: &str) {
        let mut listings = self.listings.write();
        listings.remove(path);
        // Also invalidate parent directory
        if let Some(parent) = path.rsplit_once('/').map(|(p, _)| p) {
            let parent_path = if parent.is_empty() { "/" } else { parent };
            listings.remove(parent_path);
        }
        self.stats.entries.store(listings.len() as u64, Ordering::Relaxed);
    }

    /// Clear all cached listings.
    pub fn clear(&self) {
        let mut listings = self.listings.write();
        listings.clear();
        self.stats.entries.store(0, Ordering::Relaxed);
    }
}

impl Default for DirListingCache {
    fn default() -> Self {
        Self::new()
    }
}

/// Global cache instance for the application.
pub struct Cache {
    /// File entry cache.
    pub files: FileEntryCache,
    /// Directory listing cache.
    pub dirs: DirListingCache,
}

impl Cache {
    /// Create a new cache with default settings.
    pub fn new() -> Self {
        Self {
            files: FileEntryCache::new(),
            dirs: DirListingCache::new(),
        }
    }

    /// Invalidate all caches for a given path.
    pub fn invalidate(&self, path: &str) {
        self.files.invalidate(path);
        self.dirs.invalidate(path);
    }

    /// Clear all caches.
    pub fn clear(&self) {
        self.files.clear();
        self.dirs.clear();
    }
}

impl Default for Cache {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_file_entry_cache() {
        let cache = FileEntryCache::new();

        let entry = CachedFileEntry {
            id: 1,
            parent_id: Some(0),
            name: "test.txt".to_string(),
            is_dir: false,
            size: 1024,
            content_hash: Some("abc123".to_string()),
            created_at: 1234567890,
            modified_at: 1234567890,
        };

        cache.insert("/test.txt".to_string(), entry.clone());

        let cached = cache.get("/test.txt").unwrap();
        assert_eq!(cached.name, "test.txt");
        assert_eq!(cached.size, 1024);

        cache.invalidate("/test.txt");
        assert!(cache.get("/test.txt").is_none());
    }

    #[test]
    fn test_dir_listing_cache() {
        let cache = DirListingCache::new();

        let listing = CachedDirListing {
            path: "/docs".to_string(),
            entries: vec![
                CachedDirEntry {
                    name: "readme.txt".to_string(),
                    is_dir: false,
                    size: 512,
                    modified_at: 1234567890,
                },
            ],
            cached_at: 1234567890,
        };

        cache.insert("/docs".to_string(), listing);

        let cached = cache.get("/docs").unwrap();
        assert_eq!(cached.entries.len(), 1);
        assert_eq!(cached.entries[0].name, "readme.txt");
    }
}