vtcode-core 0.136.3

Core library for VT Code - a Rust-based terminal coding agent
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
//! LRU cache with TTL enforcement and observability hooks.
//!
//! Provides a production-ready cache for tool results, pattern data, and LLM responses.
//! Includes metrics collection and optional logging.

use hashbrown::{HashMap, HashSet};
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

// Arc brings shared ownership of cached values
/// Cache entry with TTL tracking.
#[derive(Debug)]
struct CacheEntry<V> {
    value: Arc<V>,
    inserted_at: Instant,
    accessed_at: Instant,
    access_count: u64,
}

/// Combined cache state: entries and access order protected by a single lock.
///
/// This eliminates the deadlock risk from acquiring multiple locks simultaneously.
#[derive(Debug)]
struct CacheState<V> {
    entries: HashMap<String, CacheEntry<V>>,
    access_order: VecDeque<String>,
}

impl<V> CacheEntry<V> {
    #[inline]
    fn is_expired(&self, ttl: Duration) -> bool {
        self.inserted_at.elapsed() > ttl
    }

    #[inline]
    fn update_access(&mut self) {
        self.accessed_at = Instant::now();
        self.access_count += 1;
    }
}

/// Statistics about cache performance.
#[derive(Clone, Copy, Debug, Default)]
pub struct CacheStats {
    /// Total hit count across all entries.
    pub hits: u64,
    /// Total miss count.
    pub misses: u64,
    /// Total evictions due to capacity.
    pub evictions: u64,
    /// Total expirations.
    pub expirations: u64,
}

impl CacheStats {
    /// Hit rate as percentage (0.0 to 100.0).
    #[inline]
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            (self.hits as f64 / total as f64) * 100.0
        }
    }
}

/// Observability hook for cache events.
#[async_trait::async_trait]
pub trait CacheObserver: Send + Sync {
    async fn on_hit(&self, key: &str, access_count: u64);
    async fn on_miss(&self, key: &str);
    async fn on_evict(&self, key: &str, reason: EvictionReason);
}

/// Why an entry was evicted.
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum EvictionReason {
    Capacity,
    Expired,
    Manual,
}

/// Noop observer (default).
pub struct NoopObserver;

#[async_trait::async_trait]
impl CacheObserver for NoopObserver {
    async fn on_hit(&self, _: &str, _: u64) {}
    async fn on_miss(&self, _: &str) {}
    async fn on_evict(&self, _: &str, _: EvictionReason) {}
}

/// LRU cache with TTL, capacity limits, and observability.
pub struct LruCache<V> {
    /// Maximum entries before LRU eviction.
    capacity: usize,
    /// TTL for all entries.
    ttl: Duration,
    /// Combined cache state: entries and access order protected by a single lock.
    state: Arc<RwLock<CacheState<V>>>,
    /// Stats tracking.
    stats: Arc<RwLock<CacheStats>>,
    /// Observability hook.
    observer: Arc<dyn CacheObserver>,
}

impl<V: Send + Sync> LruCache<V> {
    /// Create a new cache with capacity and TTL.
    pub fn new(capacity: usize, ttl: Duration) -> Self {
        Self::with_observer(capacity, ttl, Arc::new(NoopObserver))
    }

    /// Create a cache with a custom observer.
    pub fn with_observer(capacity: usize, ttl: Duration, observer: Arc<dyn CacheObserver>) -> Self {
        Self {
            capacity,
            ttl,
            state: Arc::new(RwLock::new(CacheState {
                entries: HashMap::new(),
                access_order: VecDeque::new(),
            })),
            stats: Arc::new(RwLock::new(CacheStats::default())),
            observer,
        }
    }

    /// Get a value from the cache.
    pub async fn get(&self, key: &str) -> Option<Arc<V>> {
        // Perform all state mutations under a single lock acquisition.
        enum GetOutcome<V> {
            Hit { value: Arc<V>, access_count: u64 },
            Miss,
            Expired,
        }

        let outcome = {
            let mut state = self.state.write().await;

            // Check for expiration first.
            if let Some(entry) = state.entries.get(key) {
                if entry.is_expired(self.ttl) {
                    state.entries.remove(key);
                    state.access_order.retain(|k| k != key);
                    GetOutcome::Expired
                } else {
                    // Valid hit - extract value and access count, then update entry.
                    let value = Arc::clone(&entry.value);
                    let access_count = entry.access_count;

                    // Update the entry's access metadata.
                    if let Some(entry) = state.entries.get_mut(key) {
                        entry.update_access();
                    }

                    // Move to back of access order (most recently used).
                    state.access_order.retain(|k| k != key);
                    state.access_order.push_back(key.to_string());

                    GetOutcome::Hit { value, access_count }
                }
            } else {
                GetOutcome::Miss
            }
        };

        // Handle observer callbacks outside the lock.
        match outcome {
            GetOutcome::Hit { value, access_count } => {
                let mut stats = self.stats.write().await;
                stats.hits += 1;
                self.observer.on_hit(key, access_count).await;
                Some(value)
            }
            GetOutcome::Expired => {
                let mut stats = self.stats.write().await;
                stats.expirations += 1;
                stats.misses += 1;
                self.observer.on_evict(key, EvictionReason::Expired).await;
                None
            }
            GetOutcome::Miss => {
                let mut stats = self.stats.write().await;
                stats.misses += 1;
                self.observer.on_miss(key).await;
                None
            }
        }
    }

    /// Get a value as an owned clone from the cache (compatibility helper).
    pub async fn get_owned(&self, key: &str) -> Option<V>
    where
        V: Clone,
    {
        self.get(key).await.map(|arc| V::clone(&arc))
    }

    /// Alias to return `Arc<V>` explicitly (clarifies intent).
    pub async fn get_arc(&self, key: &str) -> Option<Arc<V>> {
        self.get(key).await
    }

    /// Insert a value into the cache.
    pub async fn insert(&self, key: String, value: V) {
        self.insert_arc(key, Arc::new(value)).await;
    }

    /// Insert an Arc-wrapped value into the cache to avoid extra cloning.
    pub async fn insert_arc(&self, key: String, value: Arc<V>) {
        let capacity_evicted = {
            let mut state = self.state.write().await;
            let mut evicted: Option<String> = None;

            // If at capacity and key doesn't exist, evict LRU entry.
            if state.entries.len() >= self.capacity && !state.entries.contains_key(&key) {
                if let Some(lru_key) = state.access_order.pop_front() {
                    state.entries.remove(&lru_key);
                    evicted = Some(lru_key);
                }
            }

            let entry = CacheEntry {
                value: Arc::clone(&value),
                inserted_at: Instant::now(),
                accessed_at: Instant::now(),
                access_count: 0,
            };

            state.entries.insert(key.clone(), entry);
            state.access_order.retain(|existing| existing != &key);
            state.access_order.push_back(key);
            evicted
        };

        // Avoid awaiting external observer hooks while cache lock is held.
        if let Some(evicted_key) = capacity_evicted {
            self.observer.on_evict(&evicted_key, EvictionReason::Capacity).await;
            let mut stats = self.stats.write().await;
            stats.evictions += 1;
        }
    }

    /// Remove a specific key.
    pub async fn remove(&self, key: &str) -> Option<Arc<V>> {
        let removed = {
            let mut state = self.state.write().await;
            state.access_order.retain(|k| k != key);
            state.entries.remove(key).map(|e| e.value)
        };

        if removed.is_some() {
            self.observer.on_evict(key, EvictionReason::Manual).await;
        }
        removed
    }

    /// Clear all entries.
    pub async fn clear(&self) {
        let mut state = self.state.write().await;
        state.entries.clear();
        state.access_order.clear();
        let mut stats = self.stats.write().await;
        *stats = CacheStats::default();
    }

    /// Get current cache statistics.
    pub async fn stats(&self) -> CacheStats {
        *self.stats.read().await
    }

    /// Get number of entries in cache.
    pub async fn len(&self) -> usize {
        self.state.read().await.entries.len()
    }

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

    /// Get all keys in cache (excluding expired).
    pub async fn keys(&self) -> Vec<String> {
        let state = self.state.read().await;
        state
            .entries
            .iter()
            .filter(|(_, entry)| !entry.is_expired(self.ttl))
            .map(|(k, _)| k.clone())
            .collect()
    }

    /// Remove expired entries.
    pub async fn prune_expired(&self) {
        let expired = {
            let mut state = self.state.write().await;

            let mut expired = Vec::new();
            state.entries.retain(|key, entry| {
                let keep = !entry.is_expired(self.ttl);
                if !keep {
                    expired.push(key.clone());
                }
                keep
            });

            if !expired.is_empty() {
                let expired_set: HashSet<_> = expired.iter().cloned().collect();
                state.access_order.retain(|k| !expired_set.contains(k));
            }

            expired
        };

        if expired.is_empty() {
            return;
        }

        for key in &expired {
            self.observer.on_evict(key, EvictionReason::Expired).await;
        }

        let mut stats = self.stats.write().await;
        stats.expirations += expired.len() as u64;
    }
}

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

    #[tokio::test]
    async fn test_basic_operations() {
        let cache: LruCache<String> = LruCache::new(3, Duration::from_secs(60));

        cache.insert_arc("a".into(), Arc::new("value_a".into())).await;
        cache.insert_arc("b".into(), Arc::new("value_b".into())).await;

        assert_eq!(cache.get("a").await.map(|v| (*v).clone()), Some("value_a".into()));
        assert_eq!(cache.get("b").await.map(|v| (*v).clone()), Some("value_b".into()));
        assert_eq!(cache.get("c").await, None);
    }

    #[tokio::test]
    async fn test_capacity_eviction() {
        let cache: LruCache<i32> = LruCache::new(2, Duration::from_secs(60));

        cache.insert("a".into(), 1).await;
        cache.insert("b".into(), 2).await;
        cache.insert("c".into(), 3).await; // Should evict "a"

        assert_eq!(cache.get("a").await, None);
        assert_eq!(cache.get("b").await.map(|v| *v), Some(2));
        assert_eq!(cache.get("c").await.map(|v| *v), Some(3));
    }

    #[tokio::test]
    async fn test_ttl_expiration() {
        let cache: LruCache<String> = LruCache::new(10, Duration::from_millis(50));

        cache.insert_arc("a".into(), Arc::new("value".into())).await;
        assert_eq!(cache.get("a").await.map(|v| (*v).clone()), Some("value".into()));

        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(cache.get("a").await, None);
    }

    #[tokio::test]
    async fn test_stats() {
        let cache: LruCache<String> = LruCache::new(10, Duration::from_secs(60));

        cache.insert_arc("a".into(), Arc::new("value".into())).await;
        cache.get("a").await; // hit
        cache.get("b").await; // miss

        let stats = cache.stats().await;
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[tokio::test]
    async fn test_prune_expired() {
        let cache: LruCache<i32> = LruCache::new(10, Duration::from_millis(50));

        cache.insert("a".into(), 1).await;
        cache.insert("b".into(), 2).await;
        tokio::time::sleep(Duration::from_millis(100)).await;
        cache.prune_expired().await;

        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn insert_arc_avoids_clone() {
        let cache = LruCache::new(2, Duration::from_secs(60));
        let v = Arc::new(42);
        cache.insert_arc("k1".to_string(), Arc::clone(&v)).await;
        let got = cache.get("k1").await;
        assert_eq!(got.unwrap().as_ref(), &42);
    }
}