Skip to main content

vtcode_core/tools/
validation_cache.rs

1use std::time::Duration;
2
3use crate::cache::{CacheKey, EvictionPolicy, UnifiedCache};
4
5/// Cache key for validation results.
6#[derive(Debug, Clone, Hash, PartialEq, Eq)]
7struct ValidationKey {
8    tool: String,
9    args_hash: u64,
10}
11
12impl CacheKey for ValidationKey {
13    fn to_cache_key(&self) -> String {
14        format!("{}:{}", self.tool, self.args_hash)
15    }
16}
17
18/// Cache for validation results to avoid repetitive checks for the same tool/args combination.
19pub struct ValidationCache {
20    cache: UnifiedCache<ValidationKey, bool>,
21}
22
23impl ValidationCache {
24    /// Create a new validation cache with the specified TTL.
25    pub fn new(ttl: Duration) -> Self {
26        Self {
27            cache: UnifiedCache::new(1000, ttl, EvictionPolicy::TtlOnly),
28        }
29    }
30
31    /// Check if a validation result is cached and valid.
32    pub fn check(&self, tool: &str, args_hash: u64) -> Option<bool> {
33        let key = ValidationKey {
34            tool: tool.to_string(),
35            args_hash,
36        };
37        self.cache.get_owned(&key)
38    }
39
40    /// Insert a validation result into the cache.
41    pub fn insert(&self, tool: &str, args_hash: u64, result: bool) {
42        let key = ValidationKey {
43            tool: tool.to_string(),
44            args_hash,
45        };
46        self.cache.insert(key, result, 1); // bool is tiny
47    }
48}
49
50impl Default for ValidationCache {
51    fn default() -> Self {
52        Self::new(Duration::from_secs(300)) // 5 minutes default
53    }
54}