frigg 0.3.2

Local-first MCP server for code understanding.
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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::domain::{FriggError, FriggResult};
use crate::indexer::{FileMetadataDigest, ManifestBuilder};
use crate::languages::semantic_chunk_language_for_path;
use crate::settings::SemanticRuntimeConfig;
use crate::storage::{ManifestEntry, ManifestMetadataEntry, RepositoryManifestSnapshot, Storage};

pub(crate) fn system_time_to_unix_nanos(system_time: SystemTime) -> Option<u64> {
    system_time
        .duration_since(UNIX_EPOCH)
        .ok()
        .and_then(|duration| u64::try_from(duration.as_nanos()).ok())
}

pub(crate) fn validate_manifest_digests_for_root(
    root: &Path,
    file_digests: &[FileMetadataDigest],
) -> Option<Vec<FileMetadataDigest>> {
    if file_digests.is_empty() {
        let live_entries = ManifestBuilder::default()
            .build_metadata_with_diagnostics(root)
            .ok()?
            .entries;
        if !live_entries.is_empty() {
            return None;
        }
    }

    let mut validated = Vec::with_capacity(file_digests.len());
    for digest in file_digests {
        let path = if digest.path.is_absolute() {
            digest.path.clone()
        } else {
            root.join(&digest.path)
        };
        if !path.starts_with(root) {
            return None;
        }

        let metadata = fs::metadata(&path).ok()?;
        if !metadata.is_file() || metadata.len() != digest.size_bytes {
            return None;
        }

        let mtime_ns = metadata.modified().ok().and_then(system_time_to_unix_nanos);
        if mtime_ns != digest.mtime_ns {
            return None;
        }

        validated.push(FileMetadataDigest {
            path,
            size_bytes: metadata.len(),
            mtime_ns,
        });
    }

    Some(validated)
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ValidatedManifestCandidateCacheEntry {
    Dirty,
    Ready {
        snapshot_id: String,
        digests: Arc<Vec<FileMetadataDigest>>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct ValidatedManifestCandidateCacheKey {
    root: PathBuf,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct ValidatedManifestCandidateCacheStats {
    pub hits: usize,
    pub misses: usize,
    pub dirty_bypasses: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ValidatedManifestCandidateCacheLookup {
    Hit(Arc<Vec<FileMetadataDigest>>),
    Miss,
    Dirty,
}

#[derive(Debug, Default)]
pub struct ValidatedManifestCandidateCache {
    entries: BTreeMap<ValidatedManifestCandidateCacheKey, ValidatedManifestCandidateCacheEntry>,
    stats: ValidatedManifestCandidateCacheStats,
}

impl ValidatedManifestCandidateCache {
    pub(crate) fn lookup(
        &mut self,
        root: &Path,
        snapshot_id: &str,
    ) -> ValidatedManifestCandidateCacheLookup {
        let key = Self::cache_key(root);
        match self.entries.get(&key) {
            Some(ValidatedManifestCandidateCacheEntry::Ready {
                snapshot_id: cached_snapshot_id,
                digests,
            }) if cached_snapshot_id == snapshot_id => {
                self.stats.hits += 1;
                ValidatedManifestCandidateCacheLookup::Hit(digests.clone())
            }
            Some(ValidatedManifestCandidateCacheEntry::Dirty) => {
                self.stats.dirty_bypasses += 1;
                ValidatedManifestCandidateCacheLookup::Dirty
            }
            Some(ValidatedManifestCandidateCacheEntry::Ready { .. }) => {
                self.entries.remove(&key);
                self.stats.misses += 1;
                ValidatedManifestCandidateCacheLookup::Miss
            }
            None => {
                self.stats.misses += 1;
                ValidatedManifestCandidateCacheLookup::Miss
            }
        }
    }

    #[cfg(test)]
    pub(crate) fn store_validated(
        &mut self,
        root: &Path,
        snapshot_id: &str,
        digests: &[FileMetadataDigest],
    ) {
        self.store_validated_shared(root, snapshot_id, Arc::new(digests.to_vec()));
    }

    pub(crate) fn store_validated_shared(
        &mut self,
        root: &Path,
        snapshot_id: &str,
        digests: Arc<Vec<FileMetadataDigest>>,
    ) {
        self.entries.insert(
            Self::cache_key(root),
            ValidatedManifestCandidateCacheEntry::Ready {
                snapshot_id: snapshot_id.to_owned(),
                digests,
            },
        );
    }

    pub(crate) fn invalidate_root(&mut self, root: &Path) {
        self.entries.remove(&Self::cache_key(root));
    }

    pub(crate) fn mark_dirty_root(&mut self, root: &Path) {
        self.entries.insert(
            Self::cache_key(root),
            ValidatedManifestCandidateCacheEntry::Dirty,
        );
    }

    pub(crate) fn is_dirty_root(&self, root: &Path) -> bool {
        matches!(
            self.entries.get(&Self::cache_key(root)),
            Some(ValidatedManifestCandidateCacheEntry::Dirty)
        )
    }

    fn cache_key(root: &Path) -> ValidatedManifestCandidateCacheKey {
        ValidatedManifestCandidateCacheKey {
            root: root.to_path_buf(),
        }
    }

    #[cfg(test)]
    pub(crate) fn stats(&self) -> ValidatedManifestCandidateCacheStats {
        self.stats
    }

    #[cfg(test)]
    pub(crate) fn has_entry_for_root(&self, root: &Path) -> bool {
        self.entries.contains_key(&Self::cache_key(root))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ValidatedManifestSnapshot {
    pub snapshot_id: String,
    pub digests: Vec<FileMetadataDigest>,
}

#[derive(Debug, Clone)]
pub(crate) struct SharedValidatedManifestSnapshot {
    pub snapshot_id: String,
    pub digests: Arc<Vec<FileMetadataDigest>>,
}

pub(crate) fn latest_validated_manifest_snapshot(
    storage: &Storage,
    repository_id: &str,
    root: &Path,
    cache: Option<&Arc<RwLock<ValidatedManifestCandidateCache>>>,
) -> Option<ValidatedManifestSnapshot> {
    let shared = latest_validated_manifest_snapshot_shared(storage, repository_id, root, cache)?;
    Some(ValidatedManifestSnapshot {
        snapshot_id: shared.snapshot_id,
        digests: shared.digests.as_ref().clone(),
    })
}

pub(crate) fn latest_validated_manifest_snapshot_shared(
    storage: &Storage,
    repository_id: &str,
    root: &Path,
    cache: Option<&Arc<RwLock<ValidatedManifestCandidateCache>>>,
) -> Option<SharedValidatedManifestSnapshot> {
    let latest = storage
        .load_latest_manifest_metadata_for_repository(repository_id)
        .ok()??;
    let snapshot_id = latest.snapshot_id.clone();

    if let Some(cache) = cache {
        let lookup = cache
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .lookup(root, &snapshot_id);
        match lookup {
            ValidatedManifestCandidateCacheLookup::Hit(digests) => {
                if validate_manifest_digests_for_root(root, digests.as_ref()).is_some() {
                    return Some(SharedValidatedManifestSnapshot {
                        snapshot_id,
                        digests,
                    });
                }

                cache
                    .write()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .invalidate_root(root);
                return None;
            }
            ValidatedManifestCandidateCacheLookup::Dirty => return None,
            ValidatedManifestCandidateCacheLookup::Miss => {}
        }
    }

    let snapshot_digests = manifest_digests_from_metadata_entries(&latest.entries);
    let validated_digests = Arc::new(validate_manifest_digests_for_root(root, &snapshot_digests)?);

    if let Some(cache) = cache {
        cache
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .store_validated_shared(root, &snapshot_id, Arc::clone(&validated_digests));
    }

    Some(SharedValidatedManifestSnapshot {
        snapshot_id,
        digests: validated_digests,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum RepositoryManifestFreshness {
    MissingSnapshot,
    StaleSnapshot,
    Ready,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum RepositorySemanticFreshness {
    Disabled,
    MissingManifestSnapshot,
    StaleManifestSnapshot,
    NoEligibleEntries,
    MissingForActiveModel,
    Ready,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RepositorySemanticTarget {
    pub provider: String,
    pub model: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RepositoryFreshnessStatus {
    pub snapshot_id: Option<String>,
    pub manifest_entry_count: Option<usize>,
    pub manifest: RepositoryManifestFreshness,
    pub semantic: RepositorySemanticFreshness,
    pub validated_manifest_digests: Option<Vec<FileMetadataDigest>>,
    pub semantic_target: Option<RepositorySemanticTarget>,
}

impl RepositoryFreshnessStatus {
    pub(crate) fn should_refresh_watch(&self) -> bool {
        matches!(
            self.manifest,
            RepositoryManifestFreshness::MissingSnapshot
                | RepositoryManifestFreshness::StaleSnapshot
        ) || matches!(
            self.semantic,
            RepositorySemanticFreshness::MissingForActiveModel
        )
    }

    pub(crate) fn watch_reason(&self) -> &'static str {
        match self.manifest {
            RepositoryManifestFreshness::MissingSnapshot => "missing_manifest_snapshot",
            RepositoryManifestFreshness::StaleSnapshot => "stale_manifest_snapshot",
            RepositoryManifestFreshness::Ready => match self.semantic {
                RepositorySemanticFreshness::Disabled => "manifest_valid",
                RepositorySemanticFreshness::MissingManifestSnapshot => "missing_manifest_snapshot",
                RepositorySemanticFreshness::StaleManifestSnapshot => "stale_manifest_snapshot",
                RepositorySemanticFreshness::NoEligibleEntries => {
                    "manifest_valid_no_semantic_eligible_entries"
                }
                RepositorySemanticFreshness::MissingForActiveModel => {
                    "semantic_snapshot_missing_for_active_model"
                }
                RepositorySemanticFreshness::Ready => "manifest_and_semantic_snapshot_valid",
            },
        }
    }
}

pub(crate) fn manifest_digests_from_entries(entries: &[ManifestEntry]) -> Vec<FileMetadataDigest> {
    entries
        .iter()
        .map(|entry| FileMetadataDigest {
            path: entry.path.clone().into(),
            size_bytes: entry.size_bytes,
            mtime_ns: entry.mtime_ns,
        })
        .collect()
}

fn manifest_digests_from_metadata_entries(
    entries: &[ManifestMetadataEntry],
) -> Vec<FileMetadataDigest> {
    entries
        .iter()
        .map(|entry| FileMetadataDigest {
            path: entry.path.clone().into(),
            size_bytes: entry.size_bytes,
            mtime_ns: entry.mtime_ns,
        })
        .collect()
}

pub(crate) fn validate_manifest_snapshot_for_root(
    root: &Path,
    snapshot: &RepositoryManifestSnapshot,
) -> Option<Vec<FileMetadataDigest>> {
    let digests = manifest_digests_from_entries(&snapshot.entries);
    validate_manifest_digests_for_root(root, &digests)
}

pub(crate) fn repository_freshness_status<F>(
    storage: &Storage,
    repository_id: &str,
    root: &Path,
    semantic_runtime: &SemanticRuntimeConfig,
    should_ignore_path: F,
) -> FriggResult<RepositoryFreshnessStatus>
where
    F: Fn(&Path) -> bool,
{
    let latest = storage.load_latest_manifest_for_repository(repository_id)?;
    let Some(snapshot) = latest else {
        return Ok(RepositoryFreshnessStatus {
            snapshot_id: None,
            manifest_entry_count: None,
            manifest: RepositoryManifestFreshness::MissingSnapshot,
            semantic: if semantic_runtime.enabled {
                RepositorySemanticFreshness::MissingManifestSnapshot
            } else {
                RepositorySemanticFreshness::Disabled
            },
            validated_manifest_digests: None,
            semantic_target: None,
        });
    };

    let snapshot_id = snapshot.snapshot_id.clone();
    let manifest_entry_count = Some(snapshot.entries.len());
    let Some(validated_manifest_digests) = validate_manifest_snapshot_for_root(root, &snapshot)
    else {
        return Ok(RepositoryFreshnessStatus {
            snapshot_id: Some(snapshot_id),
            manifest_entry_count,
            manifest: RepositoryManifestFreshness::StaleSnapshot,
            semantic: if semantic_runtime.enabled {
                RepositorySemanticFreshness::StaleManifestSnapshot
            } else {
                RepositorySemanticFreshness::Disabled
            },
            validated_manifest_digests: None,
            semantic_target: None,
        });
    };

    if !semantic_runtime.enabled {
        return Ok(RepositoryFreshnessStatus {
            snapshot_id: Some(snapshot_id),
            manifest_entry_count,
            manifest: RepositoryManifestFreshness::Ready,
            semantic: RepositorySemanticFreshness::Disabled,
            validated_manifest_digests: Some(validated_manifest_digests),
            semantic_target: None,
        });
    }

    semantic_runtime
        .validate()
        .map_err(|err| FriggError::InvalidInput(format!("{err}")))?;
    let provider = semantic_runtime.provider.ok_or_else(|| {
        FriggError::Internal("semantic runtime provider missing after validation".to_owned())
    })?;
    let model = semantic_runtime.normalized_model().ok_or_else(|| {
        FriggError::Internal("semantic runtime model missing after validation".to_owned())
    })?;
    let semantic_target = RepositorySemanticTarget {
        provider: provider.as_str().to_owned(),
        model: model.to_owned(),
    };

    let has_semantic_eligible_entries = snapshot.entries.iter().any(|entry| {
        let path = Path::new(&entry.path);
        !should_ignore_path(path) && semantic_chunk_language_for_path(path).is_some()
    });
    if !has_semantic_eligible_entries {
        return Ok(RepositoryFreshnessStatus {
            snapshot_id: Some(snapshot_id),
            manifest_entry_count,
            manifest: RepositoryManifestFreshness::Ready,
            semantic: RepositorySemanticFreshness::NoEligibleEntries,
            validated_manifest_digests: Some(validated_manifest_digests),
            semantic_target: Some(semantic_target),
        });
    }

    let has_rows = storage.has_semantic_embeddings_for_repository_snapshot_model(
        repository_id,
        &snapshot.snapshot_id,
        &semantic_target.provider,
        &semantic_target.model,
    )?;

    Ok(RepositoryFreshnessStatus {
        snapshot_id: Some(snapshot.snapshot_id),
        manifest_entry_count,
        manifest: RepositoryManifestFreshness::Ready,
        semantic: if has_rows {
            RepositorySemanticFreshness::Ready
        } else {
            RepositorySemanticFreshness::MissingForActiveModel
        },
        validated_manifest_digests: Some(validated_manifest_digests),
        semantic_target: Some(semantic_target),
    })
}

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

    fn temp_workspace_root(test_name: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system clock should be after unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("frigg-manifest-validation-{test_name}-{unique}"))
    }

    fn cleanup_workspace(path: &Path) {
        if path.exists() {
            fs::remove_dir_all(path).expect("temp manifest-validation workspace should remove");
        }
    }

    fn seed_manifest_snapshot(
        storage: &Storage,
        repository_id: &str,
        snapshot_id: &str,
        file_path: &Path,
    ) -> FriggResult<()> {
        let metadata = fs::metadata(file_path).map_err(|err| {
            FriggError::Internal(format!(
                "failed to stat manifest-validation fixture '{}': {err}",
                file_path.display()
            ))
        })?;
        storage.upsert_manifest(
            repository_id,
            snapshot_id,
            &[ManifestEntry {
                path: file_path.display().to_string(),
                sha256: "fixture-sha".to_owned(),
                size_bytes: metadata.len(),
                mtime_ns: metadata.modified().ok().and_then(system_time_to_unix_nanos),
            }],
        )
    }

    #[test]
    fn latest_validated_manifest_snapshot_shared_reuses_cached_digest_arc() -> FriggResult<()> {
        let workspace_root = temp_workspace_root("shared-cache-hit");
        fs::create_dir_all(&workspace_root)
            .expect("manifest-validation workspace root should be creatable");
        let file_path = workspace_root.join("src.rs");
        fs::write(&file_path, "fn alpha() {}\n")
            .expect("manifest-validation fixture file should be writable");

        let db_path = workspace_root.join(".frigg/provenance.db");
        fs::create_dir_all(
            db_path
                .parent()
                .expect("manifest-validation db path should have a parent"),
        )
        .expect("manifest-validation db parent should be creatable");
        let storage = Storage::new(&db_path);
        storage.initialize()?;
        seed_manifest_snapshot(&storage, "repo-001", "snapshot-001", &file_path)?;

        let cache = Arc::new(RwLock::new(ValidatedManifestCandidateCache::default()));
        let first = latest_validated_manifest_snapshot_shared(
            &storage,
            "repo-001",
            &workspace_root,
            Some(&cache),
        )
        .expect("first shared manifest validation should succeed");
        let second = latest_validated_manifest_snapshot_shared(
            &storage,
            "repo-001",
            &workspace_root,
            Some(&cache),
        )
        .expect("second shared manifest validation should hit cache");

        assert!(Arc::ptr_eq(&first.digests, &second.digests));
        let stats = cache
            .read()
            .expect("validated manifest candidate cache should not be poisoned")
            .stats();
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 1);

        cleanup_workspace(&workspace_root);
        Ok(())
    }

    #[test]
    fn latest_validated_manifest_snapshot_shared_respects_dirty_root_bypass() -> FriggResult<()> {
        let workspace_root = temp_workspace_root("shared-dirty-bypass");
        fs::create_dir_all(&workspace_root)
            .expect("manifest-validation workspace root should be creatable");
        let file_path = workspace_root.join("src.rs");
        fs::write(&file_path, "fn beta() {}\n")
            .expect("manifest-validation fixture file should be writable");

        let db_path = workspace_root.join(".frigg/provenance.db");
        fs::create_dir_all(
            db_path
                .parent()
                .expect("manifest-validation db path should have a parent"),
        )
        .expect("manifest-validation db parent should be creatable");
        let storage = Storage::new(&db_path);
        storage.initialize()?;
        seed_manifest_snapshot(&storage, "repo-001", "snapshot-001", &file_path)?;

        let cache = Arc::new(RwLock::new(ValidatedManifestCandidateCache::default()));
        assert!(
            latest_validated_manifest_snapshot_shared(
                &storage,
                "repo-001",
                &workspace_root,
                Some(&cache),
            )
            .is_some(),
            "initial shared manifest validation should populate cache"
        );
        cache
            .write()
            .expect("validated manifest candidate cache should not be poisoned")
            .mark_dirty_root(&workspace_root);

        assert!(
            latest_validated_manifest_snapshot_shared(
                &storage,
                "repo-001",
                &workspace_root,
                Some(&cache),
            )
            .is_none(),
            "dirty roots should bypass shared manifest snapshot reuse"
        );
        let stats = cache
            .read()
            .expect("validated manifest candidate cache should not be poisoned")
            .stats();
        assert_eq!(stats.dirty_bypasses, 1);

        cleanup_workspace(&workspace_root);
        Ok(())
    }
}