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

use super::lexical_recall::normalize_hybrid_recall_token;

pub(super) fn hybrid_query_exact_terms(query_text: &str) -> Vec<String> {
    let mut seen = BTreeSet::new();
    let mut tokens = Vec::new();
    let mut current = String::new();

    for ch in query_text.chars() {
        if ch.is_ascii_alphanumeric() || ch == '_' {
            current.push(ch.to_ascii_lowercase());
            continue;
        }

        if let Some(token) = normalize_hybrid_recall_token(&current) {
            if seen.insert(token.clone()) {
                tokens.push(token);
            }
        }
        current.clear();
    }

    if let Some(token) = normalize_hybrid_recall_token(&current) {
        if seen.insert(token.clone()) {
            tokens.push(token);
        }
    }

    tokens
}

pub(super) fn hybrid_specific_witness_query_terms(query_text: &str) -> Vec<String> {
    const GENERIC_WITNESS_TERMS: &[&str] = &[
        "action",
        "actions",
        "app",
        "application",
        "applications",
        "blade",
        "bench",
        "benches",
        "benchmark",
        "benchmarks",
        "build",
        "cli",
        "command",
        "commands",
        "component",
        "components",
        "create",
        "creates",
        "entry",
        "entrypoint",
        "example",
        "examples",
        "flow",
        "fixture",
        "fixtures",
        "form",
        "forms",
        "integration",
        "integrations",
        "layout",
        "layouts",
        "middleware",
        "modal",
        "modals",
        "page",
        "pages",
        "part",
        "parts",
        "provider",
        "providers",
        "render",
        "resource",
        "resources",
        "route",
        "routes",
        "script",
        "scripts",
        "section",
        "sections",
        "slot",
        "slots",
        "test",
        "tests",
        "ui",
        "view",
        "views",
        "wire",
        "wiring",
    ];

    let mut seen = BTreeSet::new();
    let mut terms = Vec::new();
    let exact_terms = hybrid_query_exact_terms(query_text);
    let compound_overlap_terms = hybrid_query_overlap_terms(query_text);

    for term in exact_terms.into_iter().chain(
        compound_overlap_terms
            .into_iter()
            .filter(|term| term.contains('-')),
    ) {
        if GENERIC_WITNESS_TERMS
            .iter()
            .any(|generic| generic == &term.as_str())
        {
            continue;
        }
        if seen.insert(term.clone()) {
            terms.push(term);
        }
    }

    terms
}

pub(super) fn hybrid_query_has_kotlin_android_ui_terms(query_text: &str) -> bool {
    let exact_terms = hybrid_query_exact_terms(query_text);
    if exact_terms.iter().any(|term| {
        matches!(
            term.as_str(),
            "ui" | "compose" | "screen" | "fragment" | "layout" | "viewmodel"
        )
    }) {
        return true;
    }

    let has_view = exact_terms.iter().any(|term| term == "view");
    let has_model = exact_terms.iter().any(|term| term == "model");
    has_view && has_model
}

pub(super) fn hybrid_query_mentions_cli_command(query_text: &str) -> bool {
    let mut current = String::new();

    for ch in query_text.chars() {
        if ch.is_ascii_alphanumeric() || ch == '_' {
            current.push(ch.to_ascii_lowercase());
            continue;
        }

        if matches!(current.as_str(), "cli" | "cmd" | "command" | "commands") {
            return true;
        }
        current.clear();
    }

    matches!(current.as_str(), "cli" | "cmd" | "command" | "commands")
}

pub(super) fn path_has_exact_query_term_match(path: &str, exact_terms: &[String]) -> bool {
    let Some(stem) = Path::new(path).file_stem().and_then(|stem| stem.to_str()) else {
        return false;
    };
    let normalized_stem = stem.trim().to_ascii_lowercase();
    if normalized_stem.is_empty() {
        return false;
    }

    exact_terms.iter().any(|term| term == &normalized_stem)
}

pub(super) fn hybrid_canonical_match_multiplier(path: &str, exact_terms: &[String]) -> f32 {
    const CANONICAL_SUFFIXES: &[&str] = &[
        "reference",
        "request",
        "response",
        "result",
        "results",
        "handler",
        "formatter",
    ];

    let Some(stem) = Path::new(path).file_stem().and_then(|stem| stem.to_str()) else {
        return 1.0;
    };
    let normalized_stem = stem.trim().to_ascii_lowercase();
    if normalized_stem.is_empty() || exact_terms.is_empty() {
        return 1.0;
    }
    if exact_terms
        .iter()
        .any(|term| term.eq_ignore_ascii_case(&normalized_stem))
    {
        return 1.65;
    }

    for term in exact_terms {
        if !normalized_stem.starts_with(term.as_str()) || normalized_stem == *term {
            continue;
        }
        let suffix = &normalized_stem[term.len()..];
        if CANONICAL_SUFFIXES
            .iter()
            .any(|candidate| candidate == &suffix)
        {
            return 0.78;
        }
    }

    1.0
}

pub(super) fn hybrid_path_has_exact_stem_match(path: &str, exact_terms: &[String]) -> bool {
    let Some(stem) = Path::new(path).file_stem().and_then(|stem| stem.to_str()) else {
        return false;
    };
    let normalized_stem = stem.trim().to_ascii_lowercase();
    if normalized_stem.is_empty() {
        return false;
    }

    exact_terms
        .iter()
        .any(|term| term.eq_ignore_ascii_case(&normalized_stem))
}

pub(super) fn hybrid_query_overlap_terms(query_text: &str) -> Vec<String> {
    let mut seen = BTreeSet::new();
    let mut tokens = Vec::new();
    let mut current = String::new();
    let normalized_query = query_text.trim().to_ascii_lowercase();

    for ch in query_text.chars() {
        if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' {
            current.push(ch);
            continue;
        }
        push_hybrid_query_overlap_terms(&mut current, &mut seen, &mut tokens);
    }
    push_hybrid_query_overlap_terms(&mut current, &mut seen, &mut tokens);
    push_known_compound_query_terms(&normalized_query, &mut seen, &mut tokens);

    tokens
}

pub(super) fn hybrid_overlap_count(candidate_terms: &[String], query_terms: &[String]) -> usize {
    candidate_terms
        .iter()
        .filter(|candidate_term| {
            query_terms
                .iter()
                .any(|query_term| hybrid_terms_overlap(candidate_term, query_term))
        })
        .count()
}

pub(super) fn hybrid_path_overlap_count(path: &str, query_text: &str) -> usize {
    let query_terms = hybrid_query_overlap_terms(query_text);
    hybrid_path_overlap_count_with_terms(path, &query_terms)
}

pub(super) fn hybrid_path_overlap_count_with_terms(path: &str, query_terms: &[String]) -> usize {
    let path_tokens = hybrid_path_overlap_tokens(path);
    if path_tokens.is_empty() {
        return 0;
    }

    if query_terms.is_empty() {
        return 0;
    }

    hybrid_overlap_count(&path_tokens, query_terms)
}

pub(super) fn hybrid_identifier_tokens(raw: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current = String::new();
    let mut previous_was_lowercase = false;

    for ch in raw.chars() {
        if ch == '_' || ch == '-' || ch == ' ' {
            push_hybrid_identifier_token(&mut tokens, &mut current);
            previous_was_lowercase = false;
            continue;
        }
        if !ch.is_ascii_alphanumeric() {
            push_hybrid_identifier_token(&mut tokens, &mut current);
            previous_was_lowercase = false;
            continue;
        }
        if ch.is_ascii_uppercase() && previous_was_lowercase {
            push_hybrid_identifier_token(&mut tokens, &mut current);
        }
        current.push(ch.to_ascii_lowercase());
        previous_was_lowercase = ch.is_ascii_lowercase();
    }

    push_hybrid_identifier_token(&mut tokens, &mut current);
    tokens
}

pub(super) fn hybrid_excerpt_has_build_flow_anchor(excerpt: &str, query_terms: &[String]) -> bool {
    let normalized = excerpt.trim().to_ascii_lowercase();
    let overlap = hybrid_overlap_count(&hybrid_identifier_tokens(&normalized), query_terms);
    overlap >= 2
        && [
            "build_",
            "build(",
            "build ",
            "builder",
            "construct",
            "wire",
            "runner = build_",
            "fn build_",
            "let mut runner = build_",
        ]
        .iter()
        .any(|needle| normalized.contains(needle))
}

pub(super) fn hybrid_excerpt_has_exact_identifier_anchor(excerpt: &str, query_text: &str) -> bool {
    let normalized = excerpt.trim().to_ascii_lowercase();
    hybrid_query_exact_terms(query_text)
        .into_iter()
        .filter(|term| term.contains('_') || term.len() >= 16)
        .any(|term| normalized.contains(&term))
}

pub(super) fn hybrid_excerpt_has_test_double_anchor(excerpt: &str) -> bool {
    let normalized = excerpt.trim().to_ascii_lowercase();
    ["fake", "mock", "fixture", "#[test]", "contract"]
        .iter()
        .any(|needle| normalized.contains(needle))
}

fn push_hybrid_query_overlap_terms(
    current: &mut String,
    seen: &mut BTreeSet<String>,
    tokens: &mut Vec<String>,
) {
    if current.is_empty() {
        return;
    }
    if let Some(token) = normalize_hybrid_overlap_token(current) {
        if seen.insert(token.clone()) {
            tokens.push(token);
        }
    }
    for token in hybrid_identifier_tokens(current) {
        if seen.insert(token.clone()) {
            tokens.push(token);
        }
    }
    current.clear();
}

pub(super) fn hybrid_path_overlap_tokens(path: &str) -> Vec<String> {
    let mut seen = BTreeSet::new();
    let mut tokens = Vec::new();

    for raw_component in path.split('/') {
        let normalized_component = Path::new(raw_component)
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or(raw_component);
        if let Some(token) = normalize_hybrid_overlap_token(normalized_component) {
            if !is_low_signal_path_overlap_token(&token) && seen.insert(token.clone()) {
                tokens.push(token);
            }
        }
        for token in hybrid_identifier_tokens(normalized_component) {
            if is_low_signal_path_overlap_token(&token) {
                continue;
            }
            if seen.insert(token.clone()) {
                tokens.push(token);
            }
        }
    }

    tokens
}

fn is_low_signal_path_overlap_token(token: &str) -> bool {
    matches!(
        token,
        "src"
            | "docs"
            | "doc"
            | "tests"
            | "test"
            | "crates"
            | "contracts"
            | "playbooks"
            | "specs"
            | "target"
            | "debug"
            | "vendor"
    )
}

fn push_hybrid_identifier_token(tokens: &mut Vec<String>, current: &mut String) {
    if let Some(token) = normalize_hybrid_overlap_token(current) {
        tokens.push(token);
    }
    current.clear();
}

fn normalize_hybrid_overlap_token(token: &str) -> Option<String> {
    let normalized = token.trim().to_ascii_lowercase();
    if matches!(normalized.as_str(), "cmd" | "pkg") {
        return Some(normalized);
    }

    normalize_hybrid_recall_token(&normalized)
}

fn push_known_compound_query_terms(
    query_text: &str,
    seen: &mut BTreeSet<String>,
    tokens: &mut Vec<String>,
) {
    const COMPOUND_TERMS: &[(&str, &str)] = &[
        ("edge functions", "edge-functions"),
        ("js sdk", "js-sdk"),
        ("node cli", "node-cli"),
        ("python sdk", "python-sdk"),
        ("self hosted", "self-hosted"),
        ("task runner", "task-runner"),
        ("task runners", "task-runners"),
    ];

    for (phrase, token) in COMPOUND_TERMS {
        if query_text.contains(phrase) && seen.insert((*token).to_owned()) {
            tokens.push((*token).to_owned());
        }
    }
}

fn hybrid_terms_overlap(left: &str, right: &str) -> bool {
    if left == right {
        return true;
    }

    const TERM_ALIASES: &[(&str, &[&str])] = &[
        ("cmd", &["command", "commands"]),
        ("command", &["cmd"]),
        ("commands", &["cmd"]),
        ("pkg", &["package", "packages"]),
        ("package", &["pkg"]),
        ("packages", &["pkg"]),
    ];

    let aliases_overlap = |candidate: &str, query: &str| {
        TERM_ALIASES
            .iter()
            .find(|(term, _)| *term == candidate)
            .is_some_and(|(_, aliases)| aliases.contains(&query))
    };
    if aliases_overlap(left, right) || aliases_overlap(right, left) {
        return true;
    }

    let normalize_plural = |term: &str| -> Option<String> {
        let normalized = term.trim().to_ascii_lowercase();
        if normalized.len() > 4 && normalized.ends_with("ies") {
            return Some(format!("{}y", &normalized[..normalized.len() - 3]));
        }
        if normalized.len() > 3 && normalized.ends_with('s') && !normalized.ends_with("ss") {
            return Some(normalized[..normalized.len() - 1].to_owned());
        }
        None
    };

    normalize_plural(left).as_deref() == Some(right)
        || normalize_plural(right).as_deref() == Some(left)
}

#[cfg(test)]
mod tests {
    use super::{
        hybrid_path_overlap_count, hybrid_query_mentions_cli_command, hybrid_query_overlap_terms,
        hybrid_specific_witness_query_terms,
    };

    #[test]
    fn hybrid_path_overlap_counts_cmd_as_command_abbreviation() {
        assert_eq!(
            hybrid_path_overlap_count(
                "cmd/frpc/main.go",
                "entry point bootstrap server api main cli command",
            ),
            2
        );
    }

    #[test]
    fn hybrid_path_overlap_counts_pkg_as_package_abbreviation() {
        assert_eq!(
            hybrid_path_overlap_count(
                "pkg/config/source/aggregator.go",
                "tests packages internal library integration",
            ),
            1
        );
    }

    #[test]
    fn hybrid_query_mentions_cli_command_uses_token_matches_not_substrings() {
        assert!(hybrid_query_mentions_cli_command(
            "ruff analyze cli entrypoint"
        ));
        assert!(hybrid_query_mentions_cli_command(
            "entry point bootstrap cli command"
        ));
        assert!(hybrid_query_mentions_cli_command(
            "entry point bootstrap commands runner"
        ));
        assert!(!hybrid_query_mentions_cli_command(
            "entry point bootstrap client runtime"
        ));
    }

    #[test]
    fn hybrid_specific_witness_terms_treat_cli_context_as_generic() {
        assert_eq!(
            hybrid_specific_witness_query_terms("ruff analyze cli command entrypoint"),
            vec!["ruff".to_owned(), "analyze".to_owned()]
        );
    }

    #[test]
    fn hybrid_specific_witness_terms_preserve_known_compound_path_anchors() {
        let terms = hybrid_specific_witness_query_terms(
            "firecrawl js sdk client task runner self hosted edge functions",
        );

        assert!(terms.contains(&"js-sdk".to_owned()));
        assert!(terms.contains(&"task-runner".to_owned()));
        assert!(terms.contains(&"self-hosted".to_owned()));
        assert!(terms.contains(&"edge-functions".to_owned()));
    }

    #[test]
    fn hybrid_query_overlap_terms_preserve_known_compound_language_and_runner_terms() {
        let terms = hybrid_query_overlap_terms(
            "firecrawl js sdk client task runner self hosted edge functions",
        );

        assert!(terms.contains(&"js-sdk".to_owned()));
        assert!(terms.contains(&"task-runner".to_owned()));
        assert!(terms.contains(&"self-hosted".to_owned()));
        assert!(terms.contains(&"edge-functions".to_owned()));
    }
}