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
use std::path::Path;

use crate::domain::{FriggError, FriggResult, PathClass, SourceClass};
use crate::path_class::classify_repository_path;
use crate::storage::PathWitnessProjection;
use serde::{Deserialize, Serialize};

use super::{
    coverage_subtree_root, hybrid_path_overlap_tokens, hybrid_source_class, is_bench_support_path,
    is_build_config_surface_path, is_ci_workflow_path, is_cli_test_support_path,
    is_entrypoint_build_workflow_path, is_entrypoint_runtime_path, is_example_support_path,
    is_frontend_runtime_noise_path, is_kotlin_android_ui_runtime_surface_path,
    is_laravel_blade_component_path, is_laravel_bootstrap_entrypoint_path,
    is_laravel_command_or_middleware_path, is_laravel_core_provider_path,
    is_laravel_form_action_blade_path, is_laravel_job_or_listener_path,
    is_laravel_layout_blade_view_path, is_laravel_livewire_component_path,
    is_laravel_livewire_view_path, is_laravel_nested_blade_component_path,
    is_laravel_non_livewire_blade_view_path, is_laravel_provider_path, is_laravel_route_path,
    is_laravel_view_component_class_path, is_package_surface_path, is_python_runtime_config_path,
    is_python_test_witness_path, is_runtime_companion_surface_path,
    is_runtime_config_artifact_path, is_scripts_ops_path, is_test_harness_path,
    is_test_support_path, is_workspace_config_surface_path,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) enum GenericWitnessSurfaceFamily {
    Runtime,
    Tests,
    PackageSurface,
    BuildConfig,
    Entrypoint,
    WorkspaceConfig,
}

pub(crate) const PATH_WITNESS_PROJECTION_HEURISTIC_VERSION: i64 = 1;
const FAMILY_RUNTIME_BIT: u64 = 1 << 0;
const FAMILY_TESTS_BIT: u64 = 1 << 1;
const FAMILY_PACKAGE_SURFACE_BIT: u64 = 1 << 2;
const FAMILY_BUILD_CONFIG_BIT: u64 = 1 << 3;
const FAMILY_ENTRYPOINT_BIT: u64 = 1 << 4;
const FAMILY_WORKSPACE_CONFIG_BIT: u64 = 1 << 5;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub(super) struct PathWitnessProjectionFlags {
    pub(super) is_entrypoint_runtime: bool,
    pub(super) is_entrypoint_build_workflow: bool,
    pub(super) is_ci_workflow: bool,
    pub(super) is_runtime_config_artifact: bool,
    pub(super) is_package_surface: bool,
    pub(super) is_build_config_surface: bool,
    pub(super) is_runtime_companion_surface: bool,
    pub(super) is_workspace_config_surface: bool,
    pub(super) is_kotlin_android_ui_runtime_surface: bool,
    pub(super) is_python_runtime_config: bool,
    pub(super) is_python_test_witness: bool,
    pub(super) is_example_support: bool,
    pub(super) is_bench_support: bool,
    pub(super) is_cli_test_support: bool,
    pub(super) is_test_harness: bool,
    pub(super) is_scripts_ops: bool,
    pub(super) is_frontend_runtime_noise: bool,
    pub(super) is_test_support: bool,
    pub(super) is_laravel_non_livewire_blade_view: bool,
    pub(super) is_laravel_livewire_view: bool,
    pub(super) is_laravel_blade_component: bool,
    pub(super) is_laravel_nested_blade_component: bool,
    pub(super) is_laravel_form_action_blade: bool,
    pub(super) is_laravel_livewire_component: bool,
    pub(super) is_laravel_view_component_class: bool,
    pub(super) is_laravel_command_or_middleware: bool,
    pub(super) is_laravel_job_or_listener: bool,
    pub(super) is_laravel_layout_blade_view: bool,
    pub(super) is_laravel_route: bool,
    pub(super) is_laravel_bootstrap_entrypoint: bool,
    pub(super) is_laravel_core_provider: bool,
    pub(super) is_laravel_provider: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct StoredPathWitnessProjection {
    pub(super) path: String,
    pub(super) path_class: PathClass,
    pub(super) source_class: SourceClass,
    pub(super) file_stem: String,
    pub(super) path_terms: Vec<String>,
    pub(super) subtree_root: Option<String>,
    pub(super) flags: PathWitnessProjectionFlags,
}

impl StoredPathWitnessProjection {
    pub(super) fn from_path(path: &str) -> Self {
        Self {
            path: path.to_owned(),
            path_class: classify_repository_path(path),
            source_class: hybrid_source_class(path),
            file_stem: file_stem_for_path(path),
            path_terms: hybrid_path_overlap_tokens(path),
            subtree_root: coverage_subtree_root(path),
            flags: build_path_witness_projection_flags(path),
        }
    }
}

pub(crate) fn generic_surface_families_for_projection(
    projection: &StoredPathWitnessProjection,
) -> Vec<GenericWitnessSurfaceFamily> {
    let mut families = Vec::new();
    if projection.flags.is_runtime_companion_surface {
        families.push(GenericWitnessSurfaceFamily::Runtime);
    }
    if projection.flags.is_test_support || projection.flags.is_test_harness {
        families.push(GenericWitnessSurfaceFamily::Tests);
    }
    if projection.flags.is_package_surface {
        families.push(GenericWitnessSurfaceFamily::PackageSurface);
    }
    if projection.flags.is_runtime_config_artifact {
        families.push(GenericWitnessSurfaceFamily::BuildConfig);
    }
    if projection.flags.is_build_config_surface || projection.flags.is_entrypoint_build_workflow {
        families.push(GenericWitnessSurfaceFamily::BuildConfig);
    }
    if projection.flags.is_entrypoint_runtime {
        families.push(GenericWitnessSurfaceFamily::Entrypoint);
    }
    if projection.flags.is_workspace_config_surface {
        families.push(GenericWitnessSurfaceFamily::WorkspaceConfig);
    }
    families.sort();
    families.dedup();
    families
}

pub(crate) fn family_bits_for_projection(projection: &StoredPathWitnessProjection) -> u64 {
    generic_surface_families_for_projection(projection)
        .into_iter()
        .fold(0u64, |bits, family| bits | family_bit(family))
}

#[allow(dead_code)]
pub(crate) fn generic_surface_families_from_bits(bits: u64) -> Vec<GenericWitnessSurfaceFamily> {
    let mut families = Vec::new();
    for family in [
        GenericWitnessSurfaceFamily::Runtime,
        GenericWitnessSurfaceFamily::Tests,
        GenericWitnessSurfaceFamily::PackageSurface,
        GenericWitnessSurfaceFamily::BuildConfig,
        GenericWitnessSurfaceFamily::Entrypoint,
        GenericWitnessSurfaceFamily::WorkspaceConfig,
    ] {
        if bits & family_bit(family) != 0 {
            families.push(family);
        }
    }
    families
}

pub(crate) fn generic_surface_family_from_name(value: &str) -> Option<GenericWitnessSurfaceFamily> {
    match value.trim() {
        "runtime" => Some(GenericWitnessSurfaceFamily::Runtime),
        "tests" => Some(GenericWitnessSurfaceFamily::Tests),
        "package_surface" => Some(GenericWitnessSurfaceFamily::PackageSurface),
        "build_config" => Some(GenericWitnessSurfaceFamily::BuildConfig),
        "entrypoint" => Some(GenericWitnessSurfaceFamily::Entrypoint),
        "workspace_config" => Some(GenericWitnessSurfaceFamily::WorkspaceConfig),
        _ => None,
    }
}

pub(super) fn build_path_witness_projection(path: &str) -> FriggResult<PathWitnessProjection> {
    let projection = StoredPathWitnessProjection::from_path(path);
    let family_bits = family_bits_for_projection(&projection);
    let flags_json = serde_json::to_string(&projection.flags).map_err(|err| {
        FriggError::Internal(format!(
            "failed to encode path witness projection flags for '{path}': {err}"
        ))
    })?;

    Ok(PathWitnessProjection {
        path: path.to_owned(),
        path_class: projection.path_class,
        source_class: projection.source_class,
        file_stem: projection.file_stem,
        path_terms: projection.path_terms,
        subtree_root: projection.subtree_root,
        family_bits,
        flags_json,
        heuristic_version: PATH_WITNESS_PROJECTION_HEURISTIC_VERSION,
    })
}

pub(crate) fn build_path_witness_projection_records_from_paths(
    paths: &[String],
) -> FriggResult<Vec<PathWitnessProjection>> {
    let mut rows = paths
        .iter()
        .map(|path| build_path_witness_projection(path))
        .collect::<FriggResult<Vec<_>>>()?;
    rows.sort_by(|left, right| left.path.cmp(&right.path));
    rows.dedup_by(|left, right| left.path == right.path);
    Ok(rows)
}

pub(super) fn decode_path_witness_projection(
    record: &PathWitnessProjection,
) -> FriggResult<StoredPathWitnessProjection> {
    let path_class = record.path_class;
    let source_class = record.source_class;
    let source_class = match source_class {
        // Legacy rows may still carry the old FRIGG-specific playbook class. Normalize those
        // projections to the generic path-based class so ranking behavior does not depend on
        // historical storage state.
        SourceClass::Playbooks => SourceClass::Project,
        other => other,
    };
    let stored_flags: PathWitnessProjectionFlags = serde_json::from_str(&record.flags_json)
        .map_err(|err| {
            FriggError::Internal(format!(
                "failed to decode stored path witness flags for '{}': {err}",
                record.path
            ))
        })?;
    let trust_stored = record.heuristic_version == PATH_WITNESS_PROJECTION_HEURISTIC_VERSION
        && !record.file_stem.is_empty();
    let path_terms = if trust_stored {
        record.path_terms.clone()
    } else {
        hybrid_path_overlap_tokens(&record.path)
    };
    let flags = if trust_stored {
        stored_flags
    } else {
        build_path_witness_projection_flags(&record.path)
    };

    Ok(StoredPathWitnessProjection {
        path: record.path.clone(),
        path_class,
        source_class,
        file_stem: if trust_stored {
            record.file_stem.clone()
        } else {
            file_stem_for_path(&record.path)
        },
        path_terms,
        subtree_root: if trust_stored {
            record.subtree_root.clone()
        } else {
            coverage_subtree_root(&record.path)
        },
        flags,
    })
}

pub(crate) fn decode_path_witness_projection_records(
    rows: &[PathWitnessProjection],
) -> FriggResult<Vec<StoredPathWitnessProjection>> {
    rows.iter().map(decode_path_witness_projection).collect()
}

fn file_stem_for_path(path: &str) -> String {
    Path::new(path)
        .file_stem()
        .and_then(|stem| stem.to_str())
        .map(|stem| stem.trim().to_ascii_lowercase())
        .unwrap_or_default()
}

fn build_path_witness_projection_flags(path: &str) -> PathWitnessProjectionFlags {
    PathWitnessProjectionFlags {
        is_entrypoint_runtime: is_entrypoint_runtime_path(path),
        is_entrypoint_build_workflow: is_entrypoint_build_workflow_path(path),
        is_ci_workflow: is_ci_workflow_path(path),
        is_runtime_config_artifact: is_runtime_config_artifact_path(path),
        is_package_surface: is_package_surface_path(path),
        is_build_config_surface: is_build_config_surface_path(path),
        is_runtime_companion_surface: is_runtime_companion_surface_path(path),
        is_workspace_config_surface: is_workspace_config_surface_path(path),
        is_kotlin_android_ui_runtime_surface: is_kotlin_android_ui_runtime_surface_path(path),
        is_python_runtime_config: is_python_runtime_config_path(path),
        is_python_test_witness: is_python_test_witness_path(path),
        is_example_support: is_example_support_path(path),
        is_bench_support: is_bench_support_path(path),
        is_cli_test_support: is_cli_test_support_path(path),
        is_test_harness: is_test_harness_path(path),
        is_scripts_ops: is_scripts_ops_path(path),
        is_frontend_runtime_noise: is_frontend_runtime_noise_path(path),
        is_test_support: is_test_support_path(path),
        is_laravel_non_livewire_blade_view: is_laravel_non_livewire_blade_view_path(path),
        is_laravel_livewire_view: is_laravel_livewire_view_path(path),
        is_laravel_blade_component: is_laravel_blade_component_path(path),
        is_laravel_nested_blade_component: is_laravel_nested_blade_component_path(path),
        is_laravel_form_action_blade: is_laravel_form_action_blade_path(path),
        is_laravel_livewire_component: is_laravel_livewire_component_path(path),
        is_laravel_view_component_class: is_laravel_view_component_class_path(path),
        is_laravel_command_or_middleware: is_laravel_command_or_middleware_path(path),
        is_laravel_job_or_listener: is_laravel_job_or_listener_path(path),
        is_laravel_layout_blade_view: is_laravel_layout_blade_view_path(path),
        is_laravel_route: is_laravel_route_path(path),
        is_laravel_bootstrap_entrypoint: is_laravel_bootstrap_entrypoint_path(path),
        is_laravel_core_provider: is_laravel_core_provider_path(path),
        is_laravel_provider: is_laravel_provider_path(path),
    }
}

fn family_bit(family: GenericWitnessSurfaceFamily) -> u64 {
    match family {
        GenericWitnessSurfaceFamily::Runtime => FAMILY_RUNTIME_BIT,
        GenericWitnessSurfaceFamily::Tests => FAMILY_TESTS_BIT,
        GenericWitnessSurfaceFamily::PackageSurface => FAMILY_PACKAGE_SURFACE_BIT,
        GenericWitnessSurfaceFamily::BuildConfig => FAMILY_BUILD_CONFIG_BIT,
        GenericWitnessSurfaceFamily::Entrypoint => FAMILY_ENTRYPOINT_BIT,
        GenericWitnessSurfaceFamily::WorkspaceConfig => FAMILY_WORKSPACE_CONFIG_BIT,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::PathWitnessProjection;

    #[test]
    fn decode_path_witness_projection_record_recomputes_live_terms_for_stale_rows() {
        let path = "autogpt_platform/backend/backend/blocks/mcp/test_helpers.py";
        let projection = StoredPathWitnessProjection::from_path(path);
        let stale_terms = vec![
            "autogpt_platform".to_owned(),
            "autogpt".to_owned(),
            "platform".to_owned(),
            "backend".to_owned(),
            "blocks".to_owned(),
            "test_helpers".to_owned(),
        ];
        let record = PathWitnessProjection {
            path: path.to_owned(),
            path_class: projection.path_class,
            source_class: projection.source_class,
            file_stem: projection.file_stem.clone(),
            path_terms: stale_terms.clone(),
            subtree_root: projection.subtree_root.clone(),
            family_bits: family_bits_for_projection(&projection),
            flags_json: serde_json::to_string(&projection.flags).expect("flags json"),
            heuristic_version: 0,
        };

        let decoded = decode_path_witness_projection(&record).expect("decode should succeed");

        assert!(
            decoded.path_terms.iter().any(|term| term == "helpers"),
            "live decoding should recover split helper token: {:?}",
            decoded.path_terms
        );
        assert_ne!(
            decoded.path_terms, stale_terms,
            "decoded path terms should not trust stale stored tokenization"
        );
    }

    #[test]
    fn decode_path_witness_projection_record_recomputes_live_flags_for_runtime_config_artifacts() {
        let path = "app/src/main/AndroidManifest.xml";
        let record = PathWitnessProjection {
            path: path.to_owned(),
            path_class: PathClass::Project,
            source_class: SourceClass::Project,
            file_stem: "androidmanifest".to_owned(),
            path_terms: Vec::new(),
            subtree_root: None,
            family_bits: 0,
            flags_json: serde_json::to_string(&PathWitnessProjectionFlags::default())
                .expect("flags json"),
            heuristic_version: 0,
        };

        let decoded = decode_path_witness_projection(&record).expect("decode should succeed");

        assert!(
            decoded.flags.is_runtime_config_artifact,
            "live decoding should recover Android and Gradle runtime config flags"
        );
    }

    #[test]
    fn path_witness_projection_derives_subtree_root_and_companion_surface_families() {
        let runtime = StoredPathWitnessProjection::from_path(
            "packages/editor-ui/src/components/canvas/NodeDetails.vue",
        );
        let tests = StoredPathWitnessProjection::from_path(
            "packages/editor-ui/tests/canvas/node_details.spec.ts",
        );
        let package_surface =
            StoredPathWitnessProjection::from_path("packages/editor-ui/package.json");
        let workspace_config =
            StoredPathWitnessProjection::from_path("packages/editor-ui/tsconfig.base.json");

        assert_eq!(
            runtime.subtree_root.as_deref(),
            Some("packages/editor-ui/src")
        );
        assert_eq!(
            tests.subtree_root.as_deref(),
            Some("packages/editor-ui/tests")
        );
        assert_eq!(
            package_surface.subtree_root.as_deref(),
            Some("packages/editor-ui")
        );
        assert_eq!(
            workspace_config.subtree_root.as_deref(),
            Some("packages/editor-ui")
        );
        assert!(
            runtime.flags.is_runtime_companion_surface,
            "runtime companion surface flag should be set for runtime siblings"
        );
        assert!(package_surface.flags.is_package_surface);
        assert!(workspace_config.flags.is_workspace_config_surface);
        assert_eq!(
            generic_surface_families_for_projection(&package_surface),
            vec![
                GenericWitnessSurfaceFamily::PackageSurface,
                GenericWitnessSurfaceFamily::BuildConfig,
            ]
        );
    }

    #[test]
    fn path_witness_projection_keeps_top_level_runtime_and_tests_in_same_workspace_root() {
        let runtime = StoredPathWitnessProjection::from_path("desktop/src/messages/layout.rs");
        let tests = StoredPathWitnessProjection::from_path("desktop/tests/layout.rs");

        assert_eq!(runtime.subtree_root.as_deref(), Some("desktop"));
        assert_eq!(tests.subtree_root.as_deref(), Some("desktop"));
    }
}