frigg 0.4.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
use super::*;

#[test]
fn hybrid_ranking_rust_config_queries_rescue_cargo_manifests_from_path_witness_recall()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-config-path-witness");
    prepare_workspace(
        &root,
        &[
            (
                "crates/ruff/src/commands/config.rs",
                "pub fn config_command() { let _ = \"config cargo\"; }\n",
            ),
            ("crates/ruff/Cargo.toml", "[package]\nname = \"ruff\"\n"),
            (
                "README.md",
                "# Config guide\nconfig cargo setup walkthrough\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "config cargo".to_owned(),
            limit: 5,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    let cargo_position = ranked_paths
        .iter()
        .position(|path| *path == "crates/ruff/Cargo.toml")
        .expect("Cargo.toml witness should be ranked");
    let readme_position = ranked_paths
        .iter()
        .position(|path| *path == "README.md")
        .expect("README noise should still be ranked");

    assert!(
        cargo_position < readme_position,
        "Cargo.toml should outrank README drift for `config cargo` queries: {ranked_paths:?}"
    );
    assert!(
        ranked_paths
            .iter()
            .take(3)
            .any(|path| *path == "crates/ruff/Cargo.toml"),
        "Cargo.toml should land near the top via config-artifact path recall: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_rust_workspace_config_queries_prefer_root_rust_configs_over_nested_pyprojects()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-workspace-config-vs-pyproject");
    let mut files = vec![
        (
            "Cargo.toml".to_owned(),
            "[workspace]\nmembers = [\"crates/*\"]\n".to_owned(),
        ),
        (
            "Cargo.lock".to_owned(),
            "[[package]]\nname = \"ruff\"\n".to_owned(),
        ),
        (
            ".cargo/config.toml".to_owned(),
            "[build]\ntarget-dir = \"target\"\n".to_owned(),
        ),
        (
            "rust-toolchain.toml".to_owned(),
            "[toolchain]\nchannel = \"stable\"\n".to_owned(),
        ),
        ("rustfmt.toml".to_owned(), "edition = \"2021\"\n".to_owned()),
        ("clippy.toml".to_owned(), "msrv = \"1.80\"\n".to_owned()),
    ];
    files.extend((0..8).map(|index| {
        (
            format!("crates/noise_{index:02}/pyproject.toml"),
            "[tool.pytest.ini_options]\naddopts = \"-q\"\n".to_owned(),
        )
    }));
    let file_refs = files
        .iter()
        .map(|(path, contents)| (path.as_str(), contents.as_str()))
        .collect::<Vec<_>>();
    prepare_workspace(&root, &file_refs)?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "workspace cargo toolchain config cargo lock".to_owned(),
            limit: 9,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    let first_rust_config = ranked_paths
        .iter()
        .position(|path| {
            matches!(
                *path,
                "Cargo.toml"
                    | "Cargo.lock"
                    | ".cargo/config.toml"
                    | "rust-toolchain.toml"
                    | "rustfmt.toml"
                    | "clippy.toml"
            )
        })
        .expect("a rust workspace config witness should be ranked");
    let first_pyproject = ranked_paths
        .iter()
        .position(|path| path.ends_with("pyproject.toml"))
        .expect("pyproject noise should still be ranked");

    assert!(
        first_rust_config < first_pyproject,
        "rust workspace config should outrank nested pyproject noise: {ranked_paths:?}"
    );
    assert!(
        ranked_paths.iter().take(5).any(|path| {
            matches!(
                *path,
                "Cargo.toml"
                    | "Cargo.lock"
                    | ".cargo/config.toml"
                    | "rust-toolchain.toml"
                    | "rustfmt.toml"
                    | "clippy.toml"
            )
        }),
        "a rust workspace config witness should land near the top: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_examples_queries_keep_examples_and_benches_visible_over_test_noise()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-examples-benches");
    prepare_workspace(
        &root,
        &[
            (
                "crates/ruff/tests/cli/main.rs",
                "tests examples fixtures integration benchmark\n",
            ),
            (
                "crates/ruff/tests/cli/lint.rs",
                "tests examples fixtures integration benchmark\n",
            ),
            (
                "crates/ruff_annotate_snippets/tests/examples.rs",
                "tests examples fixtures integration benchmark\n",
            ),
            (
                "crates/ruff_annotate_snippets/examples/expected_type.rs",
                "pub fn demo_example() {}\n",
            ),
            (
                "crates/ruff_benchmark/benches/formatter.rs",
                "pub fn bench_formatter() {}\n",
            ),
            (
                "crates/ruff_benchmark/benches/ty.rs",
                "pub fn bench_ty() {}\n",
            ),
            (
                "crates/ruff/src/cache.rs",
                "tests examples fixtures integration benchmark\n",
            ),
            (
                "docs/examples.md",
                "# Examples\ntests examples fixtures integration benchmark\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "tests examples fixtures integration benchmark".to_owned(),
            limit: 6,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    assert!(
        ranked_paths.iter().take(3).any(|path| matches!(
            *path,
            "crates/ruff_annotate_snippets/examples/expected_type.rs"
                | "crates/ruff_benchmark/benches/formatter.rs"
        )),
        "an examples-or-benches witness should land near the top: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
#[ignore = "workstream-c escalation target"]
fn hybrid_ranking_graphite_queries_prefer_editor_subtree_over_sibling_runtime_noise()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-graphite-editor-subtree");
    prepare_workspace(
        &root,
        &[
            (
                "editor/src/messages/panels/layers.rs",
                "pub fn canvas_panel_runtime() { let _ = \"editor panels canvas runtime\"; }\n",
            ),
            ("editor/tests/canvas_runtime.rs", "mod canvas_runtime {}\n"),
            (
                "node-graph/src/runtime.rs",
                "pub fn node_graph_runtime() { let _ = \"node graph runtime\"; }\n",
            ),
            (
                "desktop/wrapper/src/messages.rs",
                "pub fn desktop_wrapper_messages() { let _ = \"graphite editor panels canvas layout messages desktop wrapper svelte\"; }\n",
            ),
            (
                "Cargo.toml",
                "[workspace]\nmembers = [\"editor\", \"desktop\"]\n",
            ),
            ("Cargo.lock", "[[package]]\nname = \"graphite\"\n"),
            (
                "website/content/editor.md",
                "# Editor runtime\neditor panels canvas runtime\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "graphite editor panels canvas layout messages desktop wrapper svelte"
                .to_owned(),
            limit: 5,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    let editor_position = ranked_paths
        .iter()
        .position(|path| {
            matches!(
                *path,
                "editor/src/messages/panels/layers.rs" | "editor/tests/canvas_runtime.rs"
            )
        })
        .expect("an editor subtree witness should be ranked");
    let sibling_position = ranked_paths
        .iter()
        .position(|path| {
            matches!(
                *path,
                "node-graph/src/runtime.rs"
                    | "website/content/editor.md"
                    | "desktop/wrapper/src/messages.rs"
                    | "Cargo.toml"
                    | "Cargo.lock"
            )
        })
        .expect("sibling runtime noise should still be ranked");

    assert!(
        editor_position < sibling_position,
        "editor subtree witnesses should outrank sibling runtime noise: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
#[ignore = "workstream-c escalation target"]
fn hybrid_ranking_ruff_queries_keep_runtime_surfaces_above_docs_and_readme() -> FriggResult<()> {
    let root = temp_workspace_root("hybrid-ruff-runtime-vs-docs");
    prepare_workspace(
        &root,
        &[
            (
                "crates/ruff_server/src/lib.rs",
                "pub fn formatter_server() { let _ = \"formatter server wasm flow\"; }\n",
            ),
            (
                "crates/ruff_wasm/src/lib.rs",
                "pub fn formatter_wasm() { let _ = \"formatter server wasm flow\"; }\n",
            ),
            ("README.md", "# Ruff\nformatter server wasm flow overview\n"),
            (
                "docs/formatter.md",
                "# Formatter\nformatter server wasm flow guide\n",
            ),
            (
                "CONTRIBUTING.md",
                "# Contributing\nformatter server wasm flow guide\n",
            ),
            ("Cargo.lock", "[[package]]\nname = \"ruff\"\n"),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "formatter server wasm flow rust runtime".to_owned(),
            limit: 5,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    let runtime_position = ranked_paths
        .iter()
        .position(|path| {
            matches!(
                *path,
                "crates/ruff_server/src/lib.rs" | "crates/ruff_wasm/src/lib.rs"
            )
        })
        .expect("a runtime surface should be ranked");
    let docs_position = ranked_paths
        .iter()
        .position(|path| {
            matches!(
                *path,
                "README.md" | "docs/formatter.md" | "CONTRIBUTING.md" | "Cargo.lock"
            )
        })
        .expect("docs/readme/meta drift should still be ranked");

    assert!(
        runtime_position < docs_position,
        "runtime surfaces should outrank docs/readme/meta drift: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_rust_tests_queries_keep_required_tests_visible_under_examples_and_benches_crowding()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-tests-vs-examples-benches-crowding");
    prepare_workspace(
        &root,
        &[
            (
                "crates/ruff/tests/analyze_graph.rs",
                "mod analyze_graph {}\n",
            ),
            (
                "crates/ruff/tests/cli/analyze_graph.rs",
                "mod cli_analyze_graph {}\n",
            ),
            ("crates/ruff/tests/cli/format.rs", "mod cli_format {}\n"),
            ("crates/ruff/tests/cli/lint.rs", "mod cli_lint {}\n"),
            ("crates/ruff/tests/cli/main.rs", "mod cli_main {}\n"),
            ("crates/ruff/tests/config.rs", "mod config_test {}\n"),
            (
                "crates/ruff_annotate_snippets/examples/footer.rs",
                "Level::Error.title(\"mismatched types\").footer(Level::Note.title(\"expected type\"));\n",
            ),
            (
                "crates/ruff_annotate_snippets/examples/footer.svg",
                "<svg><text>expected type</text><text>footer</text></svg>\n",
            ),
            (
                "crates/ruff_python_formatter/tests/fixtures.rs",
                "fn black_compatibility() { format_range(); }\n",
            ),
            (
                "crates/ruff_benchmark/benches/linter.rs",
                "fn benchmark_linter() { criterion_group!(benches); }\n",
            ),
            (
                "crates/ruff_benchmark/benches/ty.rs",
                "fn benchmark_ty() { criterion_group!(benches); }\n",
            ),
            (
                "crates/ruff_benchmark/benches/ty_walltime.rs",
                "fn benchmark_ty_walltime() { criterion_group!(benches); }\n",
            ),
            (
                "crates/ruff_annotate_snippets/examples/expected_type.rs",
                "Level::Note.title(\"expected type\");\n",
            ),
            (
                "crates/ruff_python_parser/tests/fixtures.rs",
                "fn parse_fixture() { parse_module(\"x = 1\"); }\n",
            ),
            (
                "crates/ruff_annotate_snippets/examples/expected_type.svg",
                "<svg><text>expected type</text></svg>\n",
            ),
            (
                "crates/ruff_annotate_snippets/tests/examples.rs",
                "fn examples_snapshot() { assert_snapshot!(); }\n",
            ),
            (
                "crates/ruff_benchmark/benches/formatter.rs",
                "fn benchmark_formatter() { criterion_group!(benches); }\n",
            ),
            (
                "crates/ruff_benchmark/benches/lexer.rs",
                "fn benchmark_lexer() { criterion_group!(benches); }\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "tests fixtures integration analyze graph entrypoint".to_owned(),
            limit: 12,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();

    assert!(
        ranked_paths.iter().take(12).any(|path| {
            matches!(
                *path,
                "crates/ruff/tests/analyze_graph.rs"
                    | "crates/ruff/tests/cli/analyze_graph.rs"
                    | "crates/ruff/tests/cli/format.rs"
                    | "crates/ruff/tests/cli/lint.rs"
                    | "crates/ruff/tests/cli/main.rs"
                    | "crates/ruff/tests/config.rs"
            )
        }),
        "a required Rust test witness should remain visible under example/bench crowding: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_tests_queries_keep_cli_runtime_witnesses_visible_over_bounded_doc_noise()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-cli-runtime-test-witnesses");
    let mut files = (0..24)
        .map(|index| {
            (
                format!("docs/alpha-{index:02}.md"),
                format!(
                    "# Alpha {index}\ntests examples fixtures integration benchmark latest tool\n"
                ),
            )
        })
        .collect::<Vec<_>>();
    files.extend([
        (
            "docs/cli/latest.md".to_owned(),
            "# `mise latest`\nGets the latest available version for a plugin\n".to_owned(),
        ),
        (
            "docs/cli/tool.md".to_owned(),
            "# `mise tool`\nGets information about a tool\n".to_owned(),
        ),
        (
            "src/cli/latest.rs".to_owned(),
            "/// Gets the latest available version for a plugin\npub struct Latest;\n".to_owned(),
        ),
        (
            "src/cli/test_tool.rs".to_owned(),
            "/// Test a tool installs and executes\npub struct TestTool;\n".to_owned(),
        ),
        (
            "src/test.rs".to_owned(),
            "pub fn init_test_env() { let _ = \"tests fixtures integration\"; }\n".to_owned(),
        ),
    ]);
    let file_refs = files
        .iter()
        .map(|(path, contents)| (path.as_str(), contents.as_str()))
        .collect::<Vec<_>>();
    prepare_workspace(&root, &file_refs)?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "tests examples fixtures integration benchmark latest tool".to_owned(),
            limit: 8,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    assert!(
        ranked_paths.contains(&"src/cli/latest.rs"),
        "path witness recall should keep the CLI runtime witness visible in top-k: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_rust_mixed_tests_queries_keep_bench_witnesses_visible_under_test_crowding()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-mixed-tests-vs-benches");
    let mut files = (0..14)
        .map(|index| {
            (
                format!("crates/biome_cli/tests/cases/noise_{index:02}.rs"),
                "tests fixtures integration assist biome json css analyzer\n".to_owned(),
            )
        })
        .collect::<Vec<_>>();
    files.extend([
        (
            "crates/biome_cli/tests/cases/assist.rs".to_owned(),
            "tests fixtures integration assist biome json css analyzer\n".to_owned(),
        ),
        (
            "crates/biome_service/tests/fixtures/basic/biome.jsonc".to_owned(),
            "{ \"tests\": \"fixtures integration assist biome json css analyzer\" }\n".to_owned(),
        ),
        (
            "crates/biome_configuration/benches/biome_json.rs".to_owned(),
            "pub fn bench_biome_json() {}\n".to_owned(),
        ),
        (
            "crates/biome_css_analyze/benches/css_analyzer.rs".to_owned(),
            "pub fn bench_css_analyzer() {}\n".to_owned(),
        ),
        (
            "benchmark/biome.json".to_owned(),
            "{ \"benchmark\": true, \"biome\": \"json\" }\n".to_owned(),
        ),
    ]);
    let file_refs = files
        .iter()
        .map(|(path, contents)| (path.as_str(), contents.as_str()))
        .collect::<Vec<_>>();
    prepare_workspace(&root, &file_refs)?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
            SearchHybridQuery {
                query: "tests fixtures integration assist biome json examples benches benchmark css analyzer"
                    .to_owned(),
                limit: 12,
                weights: HybridChannelWeights::default(),
                semantic: Some(false),
            },
            SearchFilters::default(),
            &SemanticRuntimeCredentials::default(),
            &PanicSemanticQueryEmbeddingExecutor,
        )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    assert!(
        ranked_paths.iter().take(12).any(|path| {
            matches!(
                *path,
                "crates/biome_configuration/benches/biome_json.rs"
                    | "crates/biome_css_analyze/benches/css_analyzer.rs"
            )
        }),
        "a bench witness should remain visible for mixed rust tests queries: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_rust_mixed_examples_queries_keep_test_witnesses_visible_under_bench_crowding()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-mixed-benches-vs-tests");
    let mut files = (0..14)
        .map(|index| {
            (
                format!("crates/biome_package/benches/noise_{index:02}.rs"),
                "examples benches benchmark biome json css analyzer\n".to_owned(),
            )
        })
        .collect::<Vec<_>>();
    files.extend([
        (
            "crates/biome_configuration/benches/biome_json.rs".to_owned(),
            "pub fn bench_biome_json() {}\n".to_owned(),
        ),
        (
            "crates/biome_css_analyze/benches/css_analyzer.rs".to_owned(),
            "pub fn bench_css_analyzer() {}\n".to_owned(),
        ),
        (
            "crates/biome_cli/tests/cases/assist.rs".to_owned(),
            "assert_cli_snapshot();\n".to_owned(),
        ),
        (
            "crates/biome_cli/tests/cases/configuration.rs".to_owned(),
            "assert_cli_snapshot();\n".to_owned(),
        ),
    ]);
    let file_refs = files
        .iter()
        .map(|(path, contents)| (path.as_str(), contents.as_str()))
        .collect::<Vec<_>>();
    prepare_workspace(&root, &file_refs)?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "examples benches benchmark biome json css analyzer tests assist".to_owned(),
            limit: 12,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    assert!(
        ranked_paths
            .iter()
            .take(12)
            .any(|path| *path == "crates/biome_cli/tests/cases/assist.rs"),
        "a targeted test witness should remain visible for mixed rust examples queries: {ranked_paths:?}"
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_graphite_editor_subtree_companion_retrieval_preserves_editor_runtime_tests()
-> FriggResult<()> {
    let root = temp_workspace_root("hybrid-graphite-editor-subtree-companion");
    prepare_workspace(
        &root,
        &[
            (
                "editor/src/messages/panels.rs",
                "pub fn render_panels() { let _ = \"editor panels runtime\"; }\n",
            ),
            (
                "editor/tests/panels.rs",
                "#[cfg(test)] mod panels_tests {}\n",
            ),
            (
                "desktop/src/messages/layout.rs",
                "pub fn desktop_messages() { let _ = \"desktop layout runtime\"; }\n",
            ),
            (
                "desktop/tests/layout.rs",
                "#[cfg(test)] mod layout_tests {}\n",
            ),
            (
                "website/content/editor.md",
                "# Graphite editor\neditor panels runtime\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "graphite editor panels runtime messages".to_owned(),
            limit: 6,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();
    let _witness_paths = output
        .channel_results
        .iter()
        .find(|result| result.channel == crate::domain::EvidenceChannel::PathSurfaceWitness)
        .map(|result| {
            result
                .hits
                .iter()
                .map(|hit| hit.document.path.as_str())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();
    let _witness_health = output
        .channel_results
        .iter()
        .find(|result| result.channel == crate::domain::EvidenceChannel::PathSurfaceWitness)
        .map(|result| (&result.health.status, result.health.reason.as_deref()));
    let _trace = output.post_selection_trace.clone();

    let editor_runtime_position = ranked_paths
        .iter()
        .position(|path| *path == "editor/src/messages/panels.rs")
        .expect("editor runtime witness should be ranked");
    let editor_test_position = ranked_paths
        .iter()
        .position(|path| *path == "editor/tests/panels.rs")
        .expect("editor test witness should be ranked");
    let desktop_noise_position = ranked_paths
        .iter()
        .position(|path| *path == "desktop/tests/layout.rs")
        .expect("desktop noise witness should still be ranked");

    assert!(
        editor_runtime_position < desktop_noise_position
            && editor_test_position < desktop_noise_position,
        "editor subtree companions should outrank sibling desktop noise: {ranked_paths:?}",
    );

    cleanup_workspace(&root);
    Ok(())
}

#[test]
fn hybrid_ranking_rust_editor_and_wasm_companion_surfaces_stay_localized() -> FriggResult<()> {
    let root = temp_workspace_root("hybrid-rust-editor-wasm-companion");
    prepare_workspace(
        &root,
        &[
            (
                "crates/editor/src/lib.rs",
                "pub fn editor_runtime() { let _ = \"editor wasm runtime\"; }\n",
            ),
            (
                "crates/editor/tests/runtime.rs",
                "#[cfg(test)] mod runtime_tests {}\n",
            ),
            (
                "crates/desktop/src/engine.rs",
                "pub fn desktop_runtime() { let _ = \"desktop runtime\"; }\n",
            ),
            (
                "crates/desktop/tests/engine.rs",
                "#[cfg(test)] mod engine_tests {}\n",
            ),
            (
                "crates/wasm/src/lib.rs",
                "pub fn wasm_runtime() { let _ = \"wasm runtime\"; }\n",
            ),
        ],
    )?;

    let searcher = TextSearcher::new(FriggConfig::from_workspace_roots(vec![root.clone()])?);
    let output = searcher.search_hybrid_with_filters_using_executor(
        SearchHybridQuery {
            query: "editor desktop runtime wasm tests".to_owned(),
            limit: 6,
            weights: HybridChannelWeights::default(),
            semantic: Some(false),
        },
        SearchFilters::default(),
        &SemanticRuntimeCredentials::default(),
        &PanicSemanticQueryEmbeddingExecutor,
    )?;

    let ranked_paths = output
        .matches
        .iter()
        .map(|entry| entry.document.path.as_str())
        .collect::<Vec<_>>();

    let workspace_position = ranked_paths
        .iter()
        .position(|path| path.ends_with("Cargo.toml"))
        .unwrap_or(usize::MAX);
    let editor_position = ranked_paths
        .iter()
        .position(|path| *path == "crates/editor/src/lib.rs")
        .expect("editor runtime should be ranked");
    let desktop_position = ranked_paths
        .iter()
        .position(|path| *path == "crates/desktop/src/engine.rs")
        .expect("desktop noise should be ranked");

    assert!(
        editor_position < desktop_position,
        "editor companion should outrank unrelated desktop runtime: {ranked_paths:?}",
    );
    assert!(
        workspace_position == usize::MAX || workspace_position <= 6,
        "searcher should keep workspace/config surfaces reachable in top-k: {ranked_paths:?}",
    );

    cleanup_workspace(&root);
    Ok(())
}