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 { tool: tool.to_string(), args_hash };
34        self.cache.get_owned(&key)
35    }
36
37    /// Insert a validation result into the cache.
38    pub fn insert(&self, tool: &str, args_hash: u64, result: bool) {
39        let key = ValidationKey { tool: tool.to_string(), args_hash };
40        self.cache.insert(key, result, 1); // bool is tiny
41    }
42}
43
44impl Default for ValidationCache {
45    fn default() -> Self {
46        Self::new(Duration::from_secs(300)) // 5 minutes default
47    }
48}