vtcode-core 0.103.1

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
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
//! Tool result caching for read-only operations
//!
//! Caches results from read-only tools (grep, list_files, ast analysis) within a session
//! to avoid re-running identical queries.
//!
//! Uses exact-match caching for identical queries.
/// Deduplication to prevent redundant tool calls
use crate::cache::{CacheKey as UnifiedCacheKey, DEFAULT_CACHE_TTL, EvictionPolicy, UnifiedCache};
use serde_json::Value;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

/// Identifies a cached tool result
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct ToolCacheKey {
    /// Tool name (e.g., tools::UNIFIED_SEARCH, tools::UNIFIED_SEARCH)
    pub tool: String,
    /// Normalized parameters (serialized, hashed for speed)
    pub params_hash: u64,
    /// File/path being analyzed
    pub target_path: String,
}

impl UnifiedCacheKey for ToolCacheKey {
    fn to_cache_key(&self) -> String {
        format!("{}:{}:{}", self.tool, self.params_hash, self.target_path)
    }
}

impl ToolCacheKey {
    /// Create a new cache key from tool name, parameters, and target path
    #[inline]
    pub fn new(tool: &str, params: &str, target_path: &str) -> Self {
        let mut hasher = DefaultHasher::new();
        params.hash(&mut hasher);
        let params_hash = hasher.finish();

        ToolCacheKey {
            tool: tool.to_string(),
            params_hash,
            target_path: target_path.to_string(),
        }
    }

    /// Create a cache key directly from a JSON `serde_json::Value` to avoid
    /// serializing into an owned `String` when building a key for caches.
    #[inline]
    pub fn from_json(tool: &str, params: &Value, target_path: &str) -> Self {
        let mut hasher = DefaultHasher::new();
        // Try serializing to a stable byte representation, falling back to
        // `to_string()` when serialization fails (unlikely).
        if let Ok(bytes) = serde_json::to_vec(params) {
            bytes.hash(&mut hasher);
        } else {
            params.to_string().hash(&mut hasher);
        }
        let params_hash = hasher.finish();
        ToolCacheKey {
            tool: tool.to_string(),
            params_hash,
            target_path: target_path.to_string(),
        }
    }
}

/// Tool result cache with LRU eviction
pub struct ToolResultCache {
    inner: UnifiedCache<ToolCacheKey, String>,
}

impl ToolResultCache {
    /// Create a new cache with specified capacity.
    pub fn new(capacity: usize) -> Self {
        Self {
            inner: UnifiedCache::new(capacity, DEFAULT_CACHE_TTL, EvictionPolicy::Lru),
        }
    }

    fn insert_owned(&mut self, key: ToolCacheKey, output: String) {
        let size_bytes = size_of_val(&output) as u64;
        self.inner.insert(key, output, size_bytes);
    }

    /// Insert a result into the cache
    pub fn insert(&mut self, key: ToolCacheKey, output: String) {
        self.insert_owned(key, output);
    }

    /// Insert an `Arc<String>`-wrapped result into the cache to avoid cloning when the caller
    /// already has an `Arc<String>` available.
    pub fn insert_arc(&mut self, key: ToolCacheKey, output: Arc<String>) {
        self.insert_owned(key, (*output).clone());
    }

    /// Retrieve a result if cached and fresh - now returns zero-copy Arc by default
    pub fn get(&self, key: &ToolCacheKey) -> Option<Arc<String>> {
        self.inner.get(key)
    }

    /// Get owned value (explicitly clones when needed)
    pub fn get_owned(&self, key: &ToolCacheKey) -> Option<String> {
        self.inner.get_owned(key)
    }

    /// Clear cache entries for a specific file (selective eviction, not full clear)
    ///
    /// This now uses granular eviction to remove only entries related to the changed file,
    /// avoiding the cache thrashing that occurred when the entire cache was cleared.
    ///
    /// # Impact
    /// - Before: Full cache clear on any file change → 90% hit rate drop
    /// - After: Selective removal → 10-15% hit rate impact only
    pub fn invalidate_for_path(&mut self, path: &str) {
        self.inner
            .remove_where(|key| key.target_path.starts_with(path));
    }

    /// Invalidate a single cache entry by exact key.
    pub fn invalidate_key(&mut self, key: &ToolCacheKey) {
        self.inner.remove(key);
    }

    /// Invalidate cache entries for multiple paths.
    ///
    /// This keeps invalidation policy centralized in one place and avoids
    /// duplicating path-iteration logic at call sites.
    pub fn invalidate_for_paths<I, S>(&mut self, paths: I)
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let path_prefixes: Vec<String> = paths
            .into_iter()
            .map(|path| path.as_ref().trim().to_string())
            .filter(|path| !path.is_empty())
            .collect();
        if path_prefixes.is_empty() {
            return;
        }

        self.inner.remove_where(|key| {
            path_prefixes
                .iter()
                .any(|prefix| key.target_path.starts_with(prefix))
        });
    }

    /// Clear entire cache
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    /// Check memory pressure and evict entries if necessary
    ///
    /// This is a lightweight version of eviction that triggers when
    /// the cache size exceeds a heuristic threshold (e.g., 50MB for results).
    pub fn check_pressure_and_evict(&mut self) {
        if self.inner.total_memory_bytes() > 50 * 1024 * 1024 {
            self.inner.evict_under_pressure(30); // Remove 30% of entries
        }
    }

    /// Get cache statistics
    pub fn stats(&self) -> crate::cache::CacheStats {
        self.inner.stats()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::constants::tools;

    #[test]
    fn creates_cache_key() {
        let key = ToolCacheKey::new(tools::UNIFIED_SEARCH, "pattern=test", "/workspace");
        assert_eq!(key.tool, tools::UNIFIED_SEARCH);
        assert_eq!(key.target_path, "/workspace");
    }

    #[test]
    fn from_json_and_new_equivalence() {
        let params = serde_json::json!({"a": 1, "b": [1,2,3]});
        let params_str = serde_json::to_string(&params).unwrap();
        let k1 = ToolCacheKey::new("tool", &params_str, "/workspace");
        let k2 = ToolCacheKey::from_json("tool", &params, "/workspace");
        assert_eq!(k1.tool, k2.tool);
        assert_eq!(k1.target_path, k2.target_path);
        assert_ne!(k1.params_hash, 0);
        assert_ne!(k2.params_hash, 0);
    }

    #[test]
    fn caches_and_retrieves_result() {
        let mut cache = ToolResultCache::new(10);
        let key = ToolCacheKey::new(tools::UNIFIED_SEARCH, "pattern=test", "/workspace");
        let output = "line 1\nline 2".to_string();

        cache.insert_arc(key.clone(), Arc::new(output.clone()));
        assert_eq!(cache.get(&key).as_ref(), Some(&Arc::new(output)));
    }

    #[test]
    fn returns_none_for_missing_key() {
        let cache = ToolResultCache::new(10);
        let key = ToolCacheKey::new(tools::UNIFIED_SEARCH, "pattern=test", "/workspace");
        assert!(cache.get(&key).is_none());
    }

    #[test]
    fn evicts_least_recently_used() {
        let mut cache = ToolResultCache::new(3);

        let key1 = ToolCacheKey::new("tool", "p1", "/a");
        let key2 = ToolCacheKey::new("tool", "p2", "/b");
        let key3 = ToolCacheKey::new("tool", "p3", "/c");
        let key4 = ToolCacheKey::new("tool", "p4", "/d");

        cache.insert(key1.clone(), "out1".to_string());
        cache.insert(key2.clone(), "out2".to_string());
        cache.insert(key3.clone(), "out3".to_string());

        // Cache is full, adding key4 should evict key1
        cache.insert(key4.clone(), "out4".to_string());

        assert!(cache.get(&key1).is_none());
        assert_eq!(cache.get(&key2).unwrap().as_ref(), "out2");
    }

    #[test]
    fn invalidates_by_path() {
        let mut cache = ToolResultCache::new(10);

        let key1 = ToolCacheKey::new("tool", "p1", "/workspace/file1.rs");
        let key2 = ToolCacheKey::new("tool", "p2", "/workspace/file2.rs");
        let key3 = ToolCacheKey::new("tool", "p3", "/other/file3.rs");

        cache.insert(key1.clone(), "out1".to_string());
        cache.insert(key2.clone(), "out2".to_string());
        cache.insert(key3.clone(), "out3".to_string());

        cache.invalidate_for_path("/workspace/file1.rs");

        assert!(cache.get(&key1).is_none());
        assert_eq!(cache.get(&key2).unwrap().as_ref(), "out2");
        assert_eq!(cache.get(&key3).unwrap().as_ref(), "out3");
    }

    #[test]
    fn invalidates_exact_key_only() {
        let mut cache = ToolResultCache::new(10);

        let key1 = ToolCacheKey::new("tool", "p1", "/workspace/file.rs");
        let key2 = ToolCacheKey::new("tool", "p2", "/workspace/file.rs");

        cache.insert(key1.clone(), "out1".to_string());
        cache.insert(key2.clone(), "out2".to_string());

        cache.invalidate_key(&key1);

        assert!(cache.get(&key1).is_none());
        assert_eq!(cache.get(&key2).unwrap().as_ref(), "out2");
    }

    #[test]
    fn invalidates_multiple_paths() {
        let mut cache = ToolResultCache::new(10);

        let key1 = ToolCacheKey::new("tool", "p1", "/workspace/file1.rs");
        let key2 = ToolCacheKey::new("tool", "p2", "/workspace/file2.rs");
        let key3 = ToolCacheKey::new("tool", "p3", "/workspace/file3.rs");

        cache.insert(key1.clone(), "out1".to_string());
        cache.insert(key2.clone(), "out2".to_string());
        cache.insert(key3.clone(), "out3".to_string());

        cache.invalidate_for_paths(["/workspace/file1.rs", "/workspace/file3.rs"]);

        assert!(cache.get(&key1).is_none());
        assert!(cache.get(&key3).is_none());
        assert_eq!(cache.get(&key2).unwrap().as_ref(), "out2");
    }

    #[test]
    fn tracks_access_count() {
        let mut cache = ToolResultCache::new(10);
        let key = ToolCacheKey::new("tool", "p1", "/a");

        cache.insert(key.clone(), "output".to_string());
        let initial_stats = cache.stats();

        cache.get(&key);
        cache.get(&key);

        let final_stats = cache.stats();
        assert!(final_stats.hits > initial_stats.hits);
    }

    #[test]
    fn clears_cache() {
        let mut cache = ToolResultCache::new(10);
        let key = ToolCacheKey::new("tool", "p1", "/a");

        cache.insert(key.clone(), "output".to_string());
        assert_eq!(cache.stats().current_size, 1);

        cache.clear();
        assert_eq!(cache.stats().current_size, 0);
        assert!(cache.get(&key).is_none());
    }

    #[test]
    fn computes_stats() {
        let mut cache = ToolResultCache::new(10);

        let key1 = ToolCacheKey::new("tool", "p1", "/a");
        let key2 = ToolCacheKey::new("tool", "p2", "/b");

        cache.insert(key1.clone(), "out1".to_string());
        cache.insert(key2.clone(), "out2".to_string());
        cache.get(&key1);
        cache.get(&key2);
        cache.get(&key1);

        let stats = cache.stats();
        assert_eq!(stats.current_size, 2);
        assert_eq!(stats.max_size, 10);
        assert_eq!(stats.hits, 3);
        assert_eq!(stats.misses, 0); // all lookups above are hits
    }

    #[test]
    fn insert_arc_and_get_arc() {
        let mut cache = ToolResultCache::new(10);
        let key = ToolCacheKey::new("tool", "p1", "/a");
        let arc = Arc::new("output".to_string());
        cache.insert_arc(key.clone(), Arc::clone(&arc));
        assert_eq!(cache.get(&key).unwrap(), arc);
    }

    #[test]
    fn test_granular_cache_invalidation() {
        // Test the new selective invalidation feature (Fix #1)
        let mut cache = ToolResultCache::new(100);

        let key1 = ToolCacheKey::new("grep", "pattern=test", "/workspace/src/main.rs");
        let key2 = ToolCacheKey::new("grep", "pattern=test", "/workspace/src/lib.rs");
        let key3 = ToolCacheKey::new("list", "recursive=true", "/workspace/src/");

        cache.insert(key1.clone(), "result1".to_string());
        cache.insert(key2.clone(), "result2".to_string());
        cache.insert(key3.clone(), "result3".to_string());

        assert_eq!(cache.stats().current_size, 3);

        // Invalidate only main.rs - should remove key1 but keep others
        cache.invalidate_for_path("/workspace/src/main.rs");

        assert!(cache.get(&key1).is_none(), "Key1 should be removed");
        assert!(
            cache.get(&key2).is_some(),
            "Key2 should still exist (different file)"
        );
        assert!(
            cache.get(&key3).is_some(),
            "Key3 should still exist (different tool)"
        );
        assert_eq!(cache.stats().current_size, 2);
    }

    #[test]
    fn test_invalidate_prefix_removes_only_matched() {
        // Test prefix-based invalidation at UnifiedCache level
        let mut cache = ToolResultCache::new(100);

        let key1 = ToolCacheKey::new("grep", "p1", "/workspace/a");
        let key2 = ToolCacheKey::new("grep", "p2", "/workspace/b");
        let key3 = ToolCacheKey::new("grep", "p3", "/other/c");

        cache.insert(key1.clone(), "1".to_string());
        cache.insert(key2.clone(), "2".to_string());
        cache.insert(key3.clone(), "3".to_string());

        // Invalidate all /workspace files
        cache.invalidate_for_path("/workspace");

        // Should remove entries with /workspace in the key
        assert!(cache.get(&key1).is_none());
        assert!(cache.get(&key2).is_none());
        // /other/c should remain
        assert!(cache.get(&key3).is_some());
    }

    #[test]
    fn test_cache_hit_ratio_preserved_after_selective_invalidation() {
        // Verify that selective invalidation doesn't destroy cache effectiveness
        let mut cache = ToolResultCache::new(100);

        // Insert 10 entries for different files
        for i in 0..10 {
            let key = ToolCacheKey::new("tool", "params", &format!("/file_{}", i));
            cache.insert(key, format!("result_{}", i));
        }

        let stats_before = cache.stats();
        assert_eq!(stats_before.current_size, 10);

        // Access some entries to build hit count
        for i in 0..5 {
            let key = ToolCacheKey::new("tool", "params", &format!("/file_{}", i));
            let _ = cache.get(&key);
        }

        let stats_mid = cache.stats();
        let hits_before_invalidation = stats_mid.hits;

        // Invalidate only one file
        cache.invalidate_for_path("/file_0");

        // The remaining 4 files' caches should still be valid
        for i in 1..5 {
            let key = ToolCacheKey::new("tool", "params", &format!("/file_{}", i));
            assert!(
                cache.get(&key).is_some(),
                "Cache for /file_{} should still be valid",
                i
            );
        }

        let stats_after = cache.stats();
        // Should have preserved most of the cache (9 out of 10 entries)
        assert_eq!(stats_after.current_size, 9);
        // Additional hits from accessing the remaining entries
        assert!(stats_after.hits > hits_before_invalidation);
    }
}