frigg 0.9.2

Frigg gives AI agents local, source-backed code search and navigation without sending whole repositories through every prompt.
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
//! Runtime cache helpers used by the MCP server.
//!
//! These helpers make the declared cache budgets operational by trimming process-wide response
//! caches with approximate serialized-size accounting instead of entry count alone.

use super::*;
use serde::Serialize;

impl FriggMcpServer {
    pub(super) fn runtime_text_searcher(&self, config: FriggConfig) -> TextSearcher {
        TextSearcher::with_runtime_projection_store_service(
            config,
            Arc::clone(&self.runtime_state.validated_manifest_candidate_cache),
            self.runtime_state.searcher_projection_store_service.clone(),
        )
    }

    pub(super) fn runtime_text_searcher_with_repository_ids(
        &self,
        config: FriggConfig,
        repository_ids: Vec<String>,
    ) -> TextSearcher {
        self.runtime_text_searcher(config)
            .with_runtime_repository_ids(repository_ids)
    }

    pub(super) fn record_runtime_cache_event(
        &self,
        family: RuntimeCacheFamily,
        event: RuntimeCacheEvent,
        count: usize,
    ) {
        if count == 0 {
            return;
        }
        let mut telemetry = self
            .runtime_state
            .runtime_cache_telemetry
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        telemetry.entry(family).or_default().record(event, count);
    }

    /// Trims a process-wide cache against its configured entry and byte budget.
    /// The byte estimator is intentionally approximate; the goal is bounded residency for
    /// long-lived servers rather than exact heap accounting.
    pub(super) fn trim_runtime_cache_to_budget<K, V, F>(
        &self,
        family: RuntimeCacheFamily,
        cache: &mut BTreeMap<K, V>,
        estimate_entry_bytes: F,
    ) where
        K: Ord,
        F: Fn(&K, &V) -> usize,
    {
        let budget = self.runtime_cache_budget(family);
        let mut evictions = 0usize;

        if let Some(limit) = budget.max_entries {
            while cache.len() > limit {
                let _ = cache.pop_first();
                evictions = evictions.saturating_add(1);
            }
        }

        if let Some(max_bytes) = budget.max_bytes {
            let mut total_bytes = cache
                .iter()
                .map(|(key, value)| estimate_entry_bytes(key, value))
                .sum::<usize>();
            while total_bytes > max_bytes {
                let Some((key, value)) = cache.pop_first() else {
                    break;
                };
                total_bytes = total_bytes.saturating_sub(estimate_entry_bytes(&key, &value));
                evictions = evictions.saturating_add(1);
            }
        }

        if evictions > 0 {
            self.record_runtime_cache_event(family, RuntimeCacheEvent::Eviction, evictions);
        }
    }

    pub(super) fn runtime_cache_budget(&self, family: RuntimeCacheFamily) -> RuntimeCacheBudget {
        self.runtime_state
            .runtime_cache_registry
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .policy(family)
            .map(|policy| policy.budget)
            .expect("runtime cache family policy should exist")
    }

    fn read_file_content_bytes_bounded(&self, canonical_path: &Path) -> Result<Vec<u8>, ErrorData> {
        let max_file_bytes = self.config.max_file_bytes;
        let metadata = fs::metadata(canonical_path).map_err(|err| {
            Self::internal(
                format!("failed to stat file {}: {err}", canonical_path.display()),
                None,
            )
        })?;
        let file_bytes = usize::try_from(metadata.len()).unwrap_or(usize::MAX);
        if file_bytes > max_file_bytes {
            return Err(Self::invalid_params(
                format!("file exceeds max_file_bytes={max_file_bytes}"),
                Some(json!({
                    "path": canonical_path.display().to_string(),
                    "bytes": file_bytes,
                    "max_file_bytes": max_file_bytes,
                })),
            ));
        }
        fs::read(canonical_path).map_err(|err| {
            Self::internal(
                format!("failed to read file {}: {err}", canonical_path.display()),
                None,
            )
        })
    }

    pub(super) fn file_content_snapshot_for_workspace(
        &self,
        workspace: &AttachedWorkspace,
        canonical_path: &Path,
    ) -> Result<Arc<FileContentSnapshot>, ErrorData> {
        let _freshness = self.repository_response_cache_freshness(
            std::slice::from_ref(workspace),
            RepositoryResponseCacheFreshnessMode::ManifestOnly,
        )?;
        let bytes = self.read_file_content_bytes_bounded(canonical_path)?;
        Ok(Arc::new(FileContentSnapshot::from_bytes(bytes)))
    }

    pub(super) fn runtime_cache_contract_summary(&self, families: &[RuntimeCacheFamily]) -> Value {
        let registry = self
            .runtime_state
            .runtime_cache_registry
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let telemetry = self
            .runtime_state
            .runtime_cache_telemetry
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner());

        Value::Array(
            families
                .iter()
                .filter_map(|family| {
                    let policy = registry.policy(*family)?;
                    let counters = telemetry.get(family).copied().unwrap_or_default();
                    Some(json!({
                        "family": family.as_str(),
                        "residency": match policy.residency {
                            crate::mcp::server_cache::RuntimeCacheResidency::ProcessWide => "process_wide",
                            crate::mcp::server_cache::RuntimeCacheResidency::RequestLocal => "request_local",
                        },
                        "reuse_class": match policy.reuse_class {
                            crate::mcp::server_cache::RuntimeCacheReuseClass::SnapshotScopedReusable => "snapshot_scoped_reusable",
                            crate::mcp::server_cache::RuntimeCacheReuseClass::ProcessMetadata => "process_metadata",
                            crate::mcp::server_cache::RuntimeCacheReuseClass::RequestLocalOnly => "request_local_only",
                            crate::mcp::server_cache::RuntimeCacheReuseClass::DeferredUntilReadOnly => "deferred_until_read_only",
                        },
                        "freshness_contract": match policy.freshness_contract {
                            crate::mcp::server_cache::RuntimeCacheFreshnessContract::RepositorySnapshot => "repository_snapshot",
                            crate::mcp::server_cache::RuntimeCacheFreshnessContract::RepositoryId => "repository_id",
                            crate::mcp::server_cache::RuntimeCacheFreshnessContract::ExactInput => "exact_input",
                            crate::mcp::server_cache::RuntimeCacheFreshnessContract::RequestLocal => "request_local",
                        },
                        "budget": {
                            "max_entries": policy.budget.max_entries,
                            "max_bytes": policy.budget.max_bytes,
                        },
                        "dirty_root_bypass": policy.dirty_root_bypass,
                        "telemetry": {
                            "hits": counters.hits,
                            "misses": counters.misses,
                            "bypasses": counters.bypasses,
                            "inserts": counters.inserts,
                            "evictions": counters.evictions,
                            "invalidations": counters.invalidations,
                        },
                    }))
                })
                .collect::<Vec<_>>(),
        )
    }

    #[cfg(test)]
    pub(super) fn runtime_cache_telemetry(
        &self,
        family: RuntimeCacheFamily,
    ) -> RuntimeCacheTelemetry {
        self.runtime_state
            .runtime_cache_telemetry
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&family)
            .copied()
            .unwrap_or_default()
    }

    #[cfg(test)]
    pub(super) fn runtime_cache_policy(
        &self,
        family: RuntimeCacheFamily,
    ) -> crate::mcp::server_cache::RuntimeCacheFamilyPolicy {
        *self
            .runtime_state
            .runtime_cache_registry
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .policy(family)
            .expect("runtime cache family policy should exist")
    }

    pub(super) fn prewarm_precise_graph_for_workspace(
        &self,
        workspace: &AttachedWorkspace,
    ) -> Result<(), String> {
        let discovery = Self::collect_scip_artifact_digests(&workspace.root);
        if discovery.artifact_digests.is_empty() {
            return Ok(());
        }
        let corpus = self
            .collect_repository_symbol_corpus(
                workspace.repository_id.clone(),
                workspace.runtime_repository_id.clone(),
                workspace.root.clone(),
            )
            .map_err(|err| err.message.to_string())?;

        self.precise_graph_for_corpus(corpus.as_ref(), self.find_references_resource_budgets())
            .map(|_| ())
            .map_err(|err| err.message.to_string())
    }

    pub(super) fn runtime_status_summary(&self) -> RuntimeStatusSummary {
        let (active_tasks, recent_tasks) = {
            let registry = self
                .runtime_state
                .runtime_task_registry
                .read()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            (registry.active_tasks(), registry.recent_tasks())
        };

        let mut tools_exposed = self.runtime_registered_tool_names();
        tools_exposed.sort();
        tools_exposed.dedup();

        let session_workspace = self.current_workspace();
        let watch_status =
            Some(self.watch_status_summary(session_workspace.as_ref(), &active_tasks));

        RuntimeStatusSummary {
            profile: self.runtime_state.runtime_profile,
            persistent_state_available: self
                .runtime_state
                .runtime_profile
                .persistent_state_available(),
            watch_active: self.runtime_state.runtime_watch_active,
            watch_status,
            tool_surface_profile: self.tool_surface_profile.as_str().to_owned(),
            tools_exposed,
            status_tool: "workspace".to_owned(),
            active_tasks,
            recent_tasks,
        }
    }

    /// Compact agent-facing watch projection from mode, leases, dual-class queue, and tasks.
    ///
    /// Does not dump raw `WatchEvent` history. Queue depth / dirty counts (EXP-hotpath-queue D)
    /// help choose `wait_watch` vs path-scoped live-disk; dual-class only (no third queue).
    ///
    /// Lease lookups use `runtime_repository_id` (watch supervisor key). The public
    /// `repository_id` field on the summary remains the stable agent-facing id.
    pub(super) fn watch_status_summary(
        &self,
        workspace: Option<&crate::mcp::workspace_registry::AttachedWorkspace>,
        active_tasks: &[crate::mcp::types::RuntimeTaskSummary],
    ) -> crate::mcp::types::WatchStatusSummary {
        use crate::mcp::types::{
            RuntimeTaskKind, RuntimeTaskStatus, WatchStatusReason, WatchStatusSummary,
        };

        let public_repo_id = workspace.map(|ws| ws.repository_id.as_str());
        let runtime_repo_id = workspace.map(|ws| ws.runtime_repository_id.as_str());

        let (
            lease_count,
            has_runtime,
            queue_depth,
            dirty_from_scheduler,
            oldest_age_ms,
            queue_pending,
            queue_in_flight,
        ) = {
            let guard = self
                .runtime_state
                .watch_runtime
                .read()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            match (guard.as_ref(), runtime_repo_id) {
                (Some(runtime), Some(repo_id)) => {
                    let lease = runtime.lease_status(repo_id);
                    if let Some(snap) = runtime.queue_status(repo_id) {
                        let now = tokio::time::Instant::now();
                        let in_flight =
                            snap.manifest_fast_in_flight || snap.semantic_followup_in_flight;
                        let pending = snap.manifest_fast_pending || snap.semantic_followup_pending;
                        (
                            lease.lease_count,
                            true,
                            Some(snap.refresh_queue_depth()),
                            Some(snap.dirty_path_hint_count),
                            snap.oldest_pending_age_ms(now),
                            pending && !in_flight,
                            in_flight,
                        )
                    } else {
                        (lease.lease_count, true, None, None, None, false, false)
                    }
                }
                (Some(_), None) => (0, true, None, None, None, false, false),
                (None, _) => (0, false, None, None, None, false, false),
            }
        };

        let dirty_from_gate =
            public_repo_id.map(|id| self.changed_paths_since_snapshot_for_gate(id).len());
        let pending_dirty_path_count = match (dirty_from_scheduler, dirty_from_gate) {
            (Some(a), Some(b)) => Some(a.max(b)),
            (Some(a), None) => Some(a),
            (None, Some(b)) if b > 0 => Some(b),
            (None, _) => None,
        };

        let queue_fields = |reason: WatchStatusReason,
                            lease_count: usize,
                            detail: Option<String>|
         -> WatchStatusSummary {
            WatchStatusSummary {
                reason,
                lease_count,
                repository_id: public_repo_id.map(ToOwned::to_owned),
                detail,
                refresh_queue_depth: queue_depth,
                pending_dirty_path_count,
                oldest_pending_age_ms: oldest_age_ms,
            }
        };

        if !self.runtime_state.runtime_watch_active {
            return queue_fields(
                WatchStatusReason::ModeOff,
                0,
                Some("watch mode disabled for this transport/profile".to_owned()),
            );
        }

        let task_matches_session = |task_repo: &str| -> bool {
            public_repo_id.is_some_and(|id| id == task_repo)
                || runtime_repo_id.is_some_and(|id| id == task_repo)
        };

        let refresh_running = active_tasks.iter().any(|task| {
            task.status == RuntimeTaskStatus::Running
                && matches!(
                    task.kind,
                    RuntimeTaskKind::ChangedIndex | RuntimeTaskKind::SemanticRefresh
                )
                && (public_repo_id.is_none() && runtime_repo_id.is_none()
                    || task_matches_session(&task.repository_id))
        });

        if refresh_running || queue_in_flight {
            let detail = if refresh_running {
                "incremental refresh task running"
            } else {
                "dual-class refresh in flight"
            };
            return queue_fields(
                WatchStatusReason::Refreshing,
                lease_count,
                Some(detail.to_owned()),
            );
        }

        if !has_runtime {
            return queue_fields(
                WatchStatusReason::NoLease,
                0,
                Some("watch runtime not started".to_owned()),
            );
        }

        if lease_count == 0 {
            return queue_fields(
                WatchStatusReason::NoLease,
                0,
                Some("no active watch lease for session repository".to_owned()),
            );
        }

        if queue_pending {
            return queue_fields(
                WatchStatusReason::Debouncing,
                lease_count,
                Some("dual-class watch queue has pending work".to_owned()),
            );
        }

        queue_fields(WatchStatusReason::Active, lease_count, None)
    }
}

/// Best-effort serialized size estimator for cached response values.
pub(super) fn serialized_value_estimated_bytes<T>(value: &T) -> usize
where
    T: Serialize,
{
    serde_json::to_vec(value)
        .map(|bytes| bytes.len())
        .unwrap_or(0)
}