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
#![allow(clippy::panic)]

use super::runtime::{decode_params, diff_trace_artifacts, normalize_trace_response_for_tool};
use super::{
    DeepSearchHarness, DeepSearchPlaybook, DeepSearchPlaybookStep, DeepSearchTraceArtifact,
    DeepSearchTraceOutcome, DeepSearchTraceStep, allowed_step_tools,
};
use crate::domain::FriggError;
use crate::mcp::tool_surface::{ToolSurfaceProfile, manifest_for_tool_surface_profile};
use crate::mcp::types::{ReadFileParams, SearchTextParams};
use crate::settings::FriggConfig;
use serde_json::{Value, json};
use std::fs;
use std::path::PathBuf;

fn make_step(step_index: usize, step_id: &str) -> DeepSearchTraceStep {
    DeepSearchTraceStep {
        step_index,
        step_id: step_id.to_owned(),
        tool_name: "search_text".to_owned(),
        params_json: "{\"query\":\"hello\"}".to_owned(),
        outcome: DeepSearchTraceOutcome::Ok {
            response_json: "{\"matches\":[]}".to_owned(),
        },
    }
}

fn make_trace(step_count: usize, steps: Vec<DeepSearchTraceStep>) -> DeepSearchTraceArtifact {
    DeepSearchTraceArtifact {
        trace_schema: "frigg.deep_search.trace.v1".to_owned(),
        playbook_id: "playbook-suite".to_owned(),
        step_count,
        steps,
    }
}

fn fixture_trace_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/citation_payload_trace.json")
}

fn load_fixture_trace() -> DeepSearchTraceArtifact {
    DeepSearchHarness::load_trace_artifact(&fixture_trace_path())
        .expect("citation payload fixture trace must parse")
}

fn temp_fixture_path(test_name: &str) -> PathBuf {
    std::env::temp_dir().join(format!(
        "frigg-deep-search-unit-{test_name}-{}.json",
        std::process::id()
    ))
}

fn write_temp_fixture(test_name: &str, raw: &str) -> PathBuf {
    let path = temp_fixture_path(test_name);
    fs::write(&path, raw).unwrap_or_else(|err| {
        panic!(
            "failed to write temporary deep-search fixture {}: {err}",
            path.display()
        )
    });
    path
}

fn overwrite_step_response(trace: &mut DeepSearchTraceArtifact, step_id: &str, response: Value) {
    let step = trace
        .steps
        .iter_mut()
        .find(|step| step.step_id == step_id)
        .unwrap_or_else(|| panic!("expected trace step {step_id} to exist"));
    step.outcome = DeepSearchTraceOutcome::Ok {
        response_json: serde_json::to_string(&response)
            .expect("failed to serialize deep-search test response"),
    };
}

fn invalid_input_message(error: FriggError) -> String {
    match error {
        FriggError::InvalidInput(message) => message,
        other => panic!("expected invalid input error, got {other:?}"),
    }
}

fn test_harness() -> DeepSearchHarness {
    let workspace_root = std::env::current_dir()
        .expect("current working directory should exist for deep-search unit tests");
    let config = FriggConfig::from_workspace_roots(vec![workspace_root])
        .expect("current workspace should build a valid FriggConfig");
    DeepSearchHarness::new(crate::mcp::server::FriggMcpServer::new(config))
}

#[test]
fn playbook_suite_diff_reports_actual_steps_length_mismatch_before_zip() {
    let expected = make_trace(2, vec![make_step(0, "step-1"), make_step(1, "step-2")]);
    let actual = make_trace(2, vec![make_step(0, "step-1")]);

    let diff = diff_trace_artifacts(&expected, &actual);
    assert_eq!(
        diff.as_deref(),
        Some("actual trace steps length mismatch: step_count=2 steps_len=1")
    );
}

#[test]
fn playbook_suite_diff_reports_expected_steps_length_mismatch_before_zip() {
    let expected = make_trace(2, vec![make_step(0, "step-1")]);
    let actual = make_trace(2, vec![make_step(0, "step-1"), make_step(1, "step-2")]);

    let diff = diff_trace_artifacts(&expected, &actual);
    assert_eq!(
        diff.as_deref(),
        Some("expected trace steps length mismatch: step_count=2 steps_len=1")
    );
}

#[test]
fn playbook_suite_diff_prioritizes_actual_structure_mismatch_over_step_count_mismatch() {
    let expected = make_trace(
        3,
        vec![
            make_step(0, "step-1"),
            make_step(1, "step-2"),
            make_step(2, "step-3"),
        ],
    );
    let actual = make_trace(2, vec![make_step(0, "step-1")]);

    let diff = diff_trace_artifacts(&expected, &actual);
    assert_eq!(
        diff.as_deref(),
        Some("actual trace steps length mismatch: step_count=2 steps_len=1")
    );
}

#[test]
fn playbook_suite_load_playbook_reports_parse_failure_with_path_context() {
    let path = write_temp_fixture("invalid-playbook", "{");

    let error = DeepSearchHarness::load_playbook(&path)
        .expect_err("malformed playbook fixture should fail to parse");
    let message = invalid_input_message(error);

    assert!(message.contains("failed to parse deep-search playbook"));
    assert!(message.contains(&path.display().to_string()));

    let _ = fs::remove_file(path);
}

#[test]
fn playbook_suite_load_trace_artifact_reports_parse_failure_with_path_context() {
    let path = write_temp_fixture("invalid-trace-artifact", "{");

    let error = DeepSearchHarness::load_trace_artifact(&path)
        .expect_err("malformed trace artifact fixture should fail to parse");
    let message = invalid_input_message(error);

    assert!(message.contains("failed to parse deep-search trace artifact"));
    assert!(message.contains(&path.display().to_string()));

    let _ = fs::remove_file(path);
}

#[test]
fn playbook_suite_persist_trace_artifact_round_trips_canonical_json() {
    let path = temp_fixture_path("persist-trace-artifact");
    let artifact = make_trace(2, vec![make_step(0, "step-1"), make_step(1, "step-2")]);

    DeepSearchHarness::persist_trace_artifact(&path, &artifact)
        .expect("trace artifact persistence should succeed");
    let persisted = DeepSearchHarness::load_trace_artifact(&path)
        .expect("persisted trace artifact should load");

    assert_eq!(persisted, artifact);

    let _ = fs::remove_file(path);
}

#[test]
fn playbook_suite_decode_params_wraps_missing_required_fields_as_invalid_params() {
    let error = decode_params::<ReadFileParams>(&json!({}))
        .expect_err("missing read_file path should fail param decoding");

    assert_eq!(error.code, "INVALID_PARAMS");
    assert_eq!(error.error_code.as_deref(), Some("invalid_params"));
    assert!(error.message.contains("invalid playbook step params"));
    assert!(error.message.contains("missing field `path`"));
}

#[test]
fn playbook_suite_decode_params_wraps_type_errors_as_invalid_params() {
    let error = decode_params::<SearchTextParams>(&json!({ "query": 7 }))
        .expect_err("wrong query type should fail param decoding");

    assert_eq!(error.code, "INVALID_PARAMS");
    assert_eq!(error.error_code.as_deref(), Some("invalid_params"));
    assert!(error.message.contains("invalid playbook step params"));
    assert!(error.message.contains("expected a string"));
}

#[test]
fn playbook_suite_allowed_step_tools_remain_subset_of_core_manifest() {
    let core_tools = manifest_for_tool_surface_profile(ToolSurfaceProfile::Core)
        .tool_names
        .into_iter()
        .collect::<std::collections::BTreeSet<_>>();
    for tool_name in allowed_step_tools() {
        assert!(
            core_tools.contains(*tool_name),
            "deep-search allowed step tool must remain in the stable core surface: {tool_name}"
        );
    }
}

#[tokio::test]
async fn playbook_suite_validates_all_step_tools_before_executing_any_step() {
    let harness = test_harness();
    let playbook = DeepSearchPlaybook {
        playbook_id: "preflight-tool-validation".to_owned(),
        steps: vec![
            DeepSearchPlaybookStep {
                step_id: "tool-001".to_owned(),
                tool_name: "search_text".to_owned(),
                params: json!({
                    "query": "",
                    "repository_id": "repo-001",
                }),
            },
            DeepSearchPlaybookStep {
                step_id: "tool-002".to_owned(),
                tool_name: "write_file".to_owned(),
                params: json!({ "path": "src/lib.rs" }),
            },
        ],
    };

    let error = harness
        .run_playbook(&playbook)
        .await
        .expect_err("unsupported step tools should be rejected before executing earlier steps");
    let message = invalid_input_message(error);

    assert!(
        message.contains("tool-002"),
        "preflight validation should point at the unsupported step, got: {message}"
    );
    assert!(
        message.contains("allowed tools:"),
        "preflight validation should surface the stable-core allowlist, got: {message}"
    );
    assert!(
        !message.contains("failed with invalid_params"),
        "unsupported-tool validation should happen before earlier step execution, got: {message}"
    );
}

#[test]
fn playbook_suite_compose_citation_payload_rejects_invalid_response_json() {
    let mut trace = load_fixture_trace();
    let step = trace
        .steps
        .iter_mut()
        .find(|step| step.step_id == "tool-002")
        .expect("expected search_text fixture step");
    step.outcome = DeepSearchTraceOutcome::Ok {
        response_json: "{".to_owned(),
    };

    let error = DeepSearchHarness::compose_citation_payload(&trace, "answer")
        .expect_err("invalid step response_json should fail citation composition");
    let message = invalid_input_message(error);

    assert!(message.contains("failed to parse response_json for deep-search step tool-002"));
}

#[test]
fn playbook_suite_compose_citation_payload_requires_matches_array_for_match_tools() {
    let mut trace = load_fixture_trace();
    overwrite_step_response(&mut trace, "tool-002", json!({}));

    let error = DeepSearchHarness::compose_citation_payload(&trace, "answer")
        .expect_err("missing matches[] should fail citation composition");
    let message = invalid_input_message(error);

    assert_eq!(
        message,
        "tool search_text step tool-002 response is missing matches[] for citation composition"
    );
}

#[test]
fn playbook_suite_compose_citation_payload_requires_non_empty_string_fields() {
    let mut trace = load_fixture_trace();
    overwrite_step_response(
        &mut trace,
        "tool-003",
        json!({
            "bytes": 18,
            "content": "line 1\nline 2\n",
            "path": "src/lib.rs",
            "repository_id": "   "
        }),
    );

    let error = DeepSearchHarness::compose_citation_payload(&trace, "answer")
        .expect_err("blank repository_id should fail citation composition");
    let message = invalid_input_message(error);

    assert_eq!(
        message,
        "tool read_file step tool-003 is missing required string field 'repository_id' for citation composition"
    );
}

#[test]
fn playbook_suite_compose_citation_payload_requires_numeric_fields() {
    let mut trace = load_fixture_trace();
    overwrite_step_response(
        &mut trace,
        "tool-005",
        json!({
            "matches": [{
                "line": 3,
                "path": "src/lib.rs",
                "repository_id": "repo-001",
                "symbol": "greeting"
            }],
            "note": "Structured details are available under metadata."
        }),
    );

    let error = DeepSearchHarness::compose_citation_payload(&trace, "answer")
        .expect_err("missing numeric match field should fail citation composition");
    let message = invalid_input_message(error);

    assert_eq!(
        message,
        "tool find_references step tool-005 match 0 is missing required numeric field 'column' for citation composition"
    );
}

#[test]
fn playbook_suite_normalizes_list_repositories_to_stable_identity_fields() {
    let normalized = normalize_trace_response_for_tool(
        "list_repositories",
        json!({
            "repositories": [{
                "repository_id": "repo-001",
                "display_name": "fixture",
                "root_path": "/tmp/fixture",
                "storage": {
                    "exists": true,
                    "initialized": true
                },
                "health": {
                    "lexical": {
                        "state": "missing",
                        "reason": "missing_manifest_snapshot"
                    }
                }
            }]
        }),
    );

    assert_eq!(
        normalized,
        json!({
            "repositories": [{
                "repository_id": "repo-001",
                "display_name": "fixture",
                "root_path": "/tmp/fixture"
            }]
        })
    );
}

#[test]
fn playbook_suite_normalizes_deep_search_tool_responses_to_stable_replay_fields() {
    let read_file = normalize_trace_response_for_tool(
        "read_file",
        json!({
            "repository_id": "repo-001",
            "path": "src/lib.rs",
            "content": "pub fn greeting() -> &'static str { \"hello\" }\n",
            "bytes": 47,
            "note": "Structured details are available under metadata.",
            "metadata": {
                "freshness": "runtime"
            }
        }),
    );
    assert_eq!(
        read_file,
        json!({
            "repository_id": "repo-001",
            "path": "src/lib.rs",
            "content": "pub fn greeting() -> &'static str { \"hello\" }\n"
        })
    );

    let search_text = normalize_trace_response_for_tool(
        "search_text",
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 1,
                "column": 8,
                "excerpt": "pub fn greeting() -> &'static str { \"hello\" }",
                "score": 0.99,
                "anchor": {
                    "start_line": 1,
                    "start_column": 1,
                    "end_line": 1,
                    "end_column": 10
                }
            }],
            "note": "Structured details are available under metadata.",
            "metadata": {
                "resource_usage": {
                    "elapsed_ms": 7
                }
            }
        }),
    );
    assert_eq!(
        search_text,
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 1,
                "column": 8,
                "excerpt": "pub fn greeting() -> &'static str { \"hello\" }"
            }]
        })
    );

    let search_symbol = normalize_trace_response_for_tool(
        "search_symbol",
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 1,
                "symbol": "greeting",
                "kind": "function"
            }],
            "metadata": {
                "freshness": "runtime"
            }
        }),
    );
    assert_eq!(
        search_symbol,
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 1,
                "symbol": "greeting"
            }]
        })
    );

    let find_references = normalize_trace_response_for_tool(
        "find_references",
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 2,
                "column": 25,
                "symbol": "greeting",
                "kind": "reference"
            }],
            "note": "Structured details are available under metadata."
        }),
    );
    assert_eq!(
        find_references,
        json!({
            "matches": [{
                "repository_id": "repo-001",
                "path": "src/lib.rs",
                "line": 2,
                "column": 25,
                "symbol": "greeting"
            }]
        })
    );
}

#[tokio::test]
async fn playbook_suite_run_step_rejects_unsupported_tool_with_invalid_params() {
    let harness = test_harness();
    let outcome = harness
        .run_step(&DeepSearchPlaybookStep {
            step_id: "tool-999".to_owned(),
            tool_name: "write_file".to_owned(),
            params: json!({ "path": "src/lib.rs" }),
        })
        .await;

    assert_eq!(
        outcome,
        DeepSearchTraceOutcome::Err {
            code: "INVALID_PARAMS".to_owned(),
            message: format!(
                "invalid input: unsupported tool in playbook step 'tool-999': write_file (allowed tools: {})",
                allowed_step_tools().join(", ")
            ),
            error_code: Some("invalid_params".to_owned()),
        }
    );
}

#[tokio::test]
async fn playbook_suite_run_step_wraps_decode_failures_as_invalid_params() {
    let harness = test_harness();
    let outcome = harness
        .run_step(&DeepSearchPlaybookStep {
            step_id: "tool-002".to_owned(),
            tool_name: "read_file".to_owned(),
            params: json!({}),
        })
        .await;

    match outcome {
        DeepSearchTraceOutcome::Err {
            code,
            message,
            error_code,
        } => {
            assert_eq!(code, "INVALID_PARAMS");
            assert_eq!(error_code.as_deref(), Some("invalid_params"));
            assert!(message.contains("invalid playbook step params"));
            assert!(message.contains("missing field `path`"));
        }
        DeepSearchTraceOutcome::Ok { .. } => {
            unreachable!("invalid read_file params should not succeed")
        }
    }
}