unfault 0.9.11

Unfault — a cognitive context engine for thoughtful engineers
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
405
//! Semantics cache for accelerating repeated analysis runs.
//!
//! This module provides a file-based cache for parsed semantics. When a file
//! hasn't changed (same content hash), we can skip re-parsing and use the
//! cached semantics instead.
//!
//! ## Design
//!
//! - Cache key: (relative_path, content_hash_xxh3_64)
//! - Cache value: MessagePack-serialized SourceSemantics
//! - Storage: `.unfault/cache/semantics/<hash>.msgpack`
//! - Invalidation: Content hash mismatch or cache version bump
//!
//! ## Performance
//!
//! - xxh3 hashing: ~6GB/s (vs ~500MB/s for SHA-256)
//! - MessagePack: 2-10x smaller than JSON, faster to serialize
//! - Expected: ~50-100ms for warm cache vs ~2000ms for cold parse

use std::collections::HashMap;
use std::fs;
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use xxhash_rust::xxh3::xxh3_64;

use unfault_core::semantics::SourceSemantics;

/// Current cache format version. Bump this when semantics structure changes.
const CACHE_VERSION: u32 = 3;

/// Cache metadata stored in meta.json
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct CacheMeta {
    version: u32,
    created_at: u64,
}

/// Entry in the in-memory cache index
#[derive(Debug, Clone)]
struct CacheEntry {
    /// Path to the cached file
    cache_path: PathBuf,
    /// Content hash of the source file
    content_hash: u64,
}

/// Statistics about cache usage during a build
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of files with cache hits
    pub hits: usize,
    /// Number of files with cache misses (had to parse)
    pub misses: usize,
    /// Total bytes saved by using cache
    pub bytes_saved: usize,
}

impl CacheStats {
    /// Returns the hit rate as a percentage
    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
        }
    }
}

/// A file-based cache for parsed semantics.
///
/// The cache stores semantics indexed by (path_hash, content_hash).
/// When a file's content hash matches, we can skip parsing.
#[derive(Debug)]
pub struct SemanticsCache {
    /// Root directory of the cache (e.g., `.unfault/cache/semantics`)
    cache_dir: PathBuf,
    /// In-memory index: path_hash -> CacheEntry
    index: HashMap<u64, CacheEntry>,
    /// Statistics for this session
    stats: CacheStats,
    /// Whether the cache is enabled
    enabled: bool,
}

impl SemanticsCache {
    /// Open or create a cache in the given workspace directory.
    ///
    /// Creates `.unfault/cache/semantics/` if it doesn't exist.
    pub fn open(workspace_path: &Path) -> Result<Self> {
        let cache_dir = workspace_path
            .join(".unfault")
            .join("cache")
            .join("semantics");

        // Create cache directory if it doesn't exist
        if !cache_dir.exists() {
            fs::create_dir_all(&cache_dir).context("Failed to create cache directory")?;
        }

        // Check cache version
        let meta_path = cache_dir.join("meta.json");
        let should_clear = if meta_path.exists() {
            let meta: CacheMeta = serde_json::from_reader(BufReader::new(fs::File::open(
                &meta_path,
            )?))
            .unwrap_or(CacheMeta {
                version: 0,
                created_at: 0,
            });

            meta.version != CACHE_VERSION
        } else {
            false
        };

        if should_clear {
            // Clear old cache
            for entry in fs::read_dir(&cache_dir)? {
                let entry = entry?;
                if entry.path().extension().map_or(false, |e| e == "msgpack") {
                    let _ = fs::remove_file(entry.path());
                }
            }
        }

        // Write current version
        let meta = CacheMeta {
            version: CACHE_VERSION,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        };
        let meta_file = fs::File::create(&meta_path)?;
        serde_json::to_writer(BufWriter::new(meta_file), &meta)?;

        // Build index from existing cache files
        let mut index = HashMap::new();
        for entry in fs::read_dir(&cache_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().map_or(false, |e| e == "msgpack") {
                // Parse filename: <content_hash>_<path_hash>_<truncated_path>.msgpack
                let filename = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
                let parts: Vec<&str> = filename.splitn(3, '_').collect();
                if parts.len() >= 2 {
                    if let (Ok(content_hash), Ok(path_hash)) = (
                        u64::from_str_radix(parts[0], 16),
                        u64::from_str_radix(parts[1], 16),
                    ) {
                        index.insert(
                            path_hash,
                            CacheEntry {
                                cache_path: path.clone(),
                                content_hash,
                            },
                        );
                    }
                }
            }
        }

        Ok(Self {
            cache_dir,
            index,
            stats: CacheStats::default(),
            enabled: true,
        })
    }

    /// Create a disabled cache (always returns None for lookups)
    pub fn disabled() -> Self {
        Self {
            cache_dir: PathBuf::new(),
            index: HashMap::new(),
            stats: CacheStats::default(),
            enabled: false,
        }
    }

    /// Compute the content hash of file content.
    pub fn hash_content(content: &str) -> u64 {
        xxh3_64(content.as_bytes())
    }

    /// Try to get cached semantics for a file.
    ///
    /// Returns `Some(semantics)` if the file is in the cache and the content
    /// hash matches. Returns `None` if there's a cache miss.
    pub fn get(&mut self, relative_path: &str, content_hash: u64) -> Option<SourceSemantics> {
        if !self.enabled {
            return None;
        }

        // Compute path hash for lookup
        let path_hash = xxh3_64(relative_path.as_bytes());

        let entry = match self.index.get(&path_hash) {
            Some(e) => e,
            None => {
                // No entry in index = cache miss
                self.stats.misses += 1;
                return None;
            }
        };

        // Check if content hash matches
        if entry.content_hash != content_hash {
            self.stats.misses += 1;
            return None;
        }

        // Try to read from cache file
        let file = fs::File::open(&entry.cache_path).ok()?;
        let reader = BufReader::new(file);

        match rmp_serde::from_read(reader) {
            Ok(semantics) => {
                self.stats.hits += 1;
                Some(semantics)
            }
            Err(_) => {
                // Cache file corrupted, treat as miss
                self.stats.misses += 1;
                None
            }
        }
    }

    /// Store semantics in the cache.
    pub fn set(&mut self, relative_path: &str, content_hash: u64, semantics: &SourceSemantics) {
        if !self.enabled {
            return;
        }

        // Compute path hash for indexing
        let path_hash = xxh3_64(relative_path.as_bytes());

        // Generate cache filename: <content_hash>_<path_hash>_<truncated_path>.msgpack
        // We include the truncated path for debugging/readability
        let safe_path = relative_path.replace(['/', '\\', ':'], "_");
        let truncated_path: String = safe_path.chars().take(50).collect();
        let filename = format!(
            "{:016x}_{:016x}_{}.msgpack",
            content_hash, path_hash, truncated_path
        );
        let cache_path = self.cache_dir.join(&filename);

        // Serialize and write
        if let Ok(file) = fs::File::create(&cache_path) {
            let mut writer = BufWriter::new(file);
            if rmp_serde::encode::write(&mut writer, semantics).is_ok() {
                // Update index using path_hash as key
                self.index.insert(
                    path_hash,
                    CacheEntry {
                        cache_path,
                        content_hash,
                    },
                );
            }
        }
    }

    /// Record a cache miss (for stats tracking when we skip the cache lookup)
    pub fn record_miss(&mut self) {
        self.stats.misses += 1;
    }

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

    /// Clear the cache.
    pub fn clear(&self) -> Result<()> {
        if !self.enabled {
            return Ok(());
        }

        for entry in fs::read_dir(&self.cache_dir)? {
            let entry = entry?;
            if entry.path().extension().map_or(false, |e| e == "msgpack") {
                fs::remove_file(entry.path())?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use unfault_core::parse::ast::FileId;
    use unfault_core::parse::python;
    use unfault_core::semantics::python::model::PyFileSemantics;
    use unfault_core::types::context::{Language, SourceFile};

    /// Create a simple Python semantics for testing
    fn create_test_semantics() -> SourceSemantics {
        let source_file = SourceFile {
            path: "test.py".to_string(),
            language: Language::Python,
            content: "def hello():\n    pass\n".to_string(),
        };
        let parsed = python::parse_python_file(FileId(1), &source_file).unwrap();
        let sem = PyFileSemantics::from_parsed(&parsed);
        SourceSemantics::Python(sem)
    }

    #[test]
    fn test_hash_content() {
        let hash1 = SemanticsCache::hash_content("hello world");
        let hash2 = SemanticsCache::hash_content("hello world");
        let hash3 = SemanticsCache::hash_content("hello world!");

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_cache_miss_on_empty() {
        let temp_dir = TempDir::new().unwrap();
        let mut cache = SemanticsCache::open(temp_dir.path()).unwrap();

        let result = cache.get("test.py", 12345);
        assert!(result.is_none());
        assert_eq!(cache.stats().misses, 1);
    }

    #[test]
    fn test_cache_hit() {
        let temp_dir = TempDir::new().unwrap();
        let mut cache = SemanticsCache::open(temp_dir.path()).unwrap();

        let semantics = create_test_semantics();
        let content_hash = 12345u64;

        cache.set("test.py", content_hash, &semantics);

        let result = cache.get("test.py", content_hash);
        assert!(result.is_some());
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_cache_miss_on_hash_change() {
        let temp_dir = TempDir::new().unwrap();
        let mut cache = SemanticsCache::open(temp_dir.path()).unwrap();

        let semantics = create_test_semantics();

        cache.set("test.py", 12345, &semantics);

        // Different hash should miss
        let result = cache.get("test.py", 99999);
        assert!(result.is_none());
        assert_eq!(cache.stats().misses, 1);
    }

    #[test]
    fn test_cache_persistence() {
        let temp_dir = TempDir::new().unwrap();
        let content_hash = 12345u64;

        // Write to cache
        {
            let mut cache = SemanticsCache::open(temp_dir.path()).unwrap();
            let semantics = create_test_semantics();
            cache.set("test.py", content_hash, &semantics);
        }

        // Read from cache with new instance
        {
            let mut cache = SemanticsCache::open(temp_dir.path()).unwrap();
            let result = cache.get("test.py", content_hash);
            assert!(result.is_some());
        }
    }

    #[test]
    fn test_hit_rate() {
        let stats = CacheStats {
            hits: 80,
            misses: 20,
            bytes_saved: 0,
        };
        assert!((stats.hit_rate() - 80.0).abs() < 0.01);
    }

    #[test]
    fn test_disabled_cache() {
        let mut cache = SemanticsCache::disabled();

        let semantics = create_test_semantics();
        cache.set("test.py", 12345, &semantics);

        let result = cache.get("test.py", 12345);
        assert!(result.is_none());
    }
}