rag-rat 0.8.0

CLI and MCP entrypoint for indexing repositories into local source, graph, history, and memory evidence.
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
use rag_rat_core::index::AnchorHealth;
use rag_rat_core::query::orientation::Orientation;
use rag_rat_core::query::tree::{DirTree, TreeNode};

use super::*;

// ─── version line ─────────────────────────────────────────────────────────

fn status(latest: Option<&str>, update: bool) -> rag_rat_core::version_check::VersionStatus {
    rag_rat_core::version_check::VersionStatus {
        current_version: "0.5.0".to_string(),
        latest_version: latest.map(str::to_string),
        update_available: update,
        update_command: "cargo install rag-rat --force".to_string(),
        checked_at_ms: latest.map(|_| 1),
    }
}

#[test]
fn version_line_nags_when_behind() {
    let line = version_line(&status(Some("0.6.0"), true)).expect("a behind status renders");
    assert!(line.contains("update available"), "got: {line}");
    assert!(line.contains("0.5.0 → 0.6.0"), "states current → latest: {line}");
    assert!(line.contains("cargo install rag-rat --force"), "states the update command: {line}");
}

#[test]
fn version_line_is_quiet_when_current() {
    let line = version_line(&status(Some("0.5.0"), false)).expect("an up-to-date status renders");
    assert!(line.contains("0.5.0 (latest on crates.io)"), "got: {line}");
    assert!(!line.contains("update available"), "no nag when current: {line}");
}

#[test]
fn version_line_is_none_when_latest_unknown() {
    assert_eq!(version_line(&status(None, false)), None, "no cached check → no line");
}

#[test]
fn version_line_marks_a_local_build_ahead_of_crates_io() {
    // Running 0.5.0 while crates.io latest is 0.4.0 (dev/pre-release): not an update, but not
    // "the crates.io latest" either.
    let line = version_line(&status(Some("0.4.0"), false)).expect("ahead status renders");
    assert!(line.contains("ahead of crates.io latest 0.4.0"), "got: {line}");
    assert!(!line.contains("(latest on crates.io)"), "must not claim it's the latest: {line}");
}

// ─── HookInput parsing ────────────────────────────────────────────────────

#[test]
fn session_start_json_without_tool_fields_deserializes() {
    // SessionStart payloads have no tool_name / tool_input — must still parse.
    let json =
        r#"{"hook_event_name":"SessionStart","source":"startup","cwd":"/x","session_id":"s"}"#;
    let input: HookInput = serde_json::from_str(json).unwrap();
    assert_eq!(input.hook_event_name.as_deref(), Some("SessionStart"));
    assert_eq!(input.source.as_deref(), Some("startup"));
    assert_eq!(input.cwd, "/x");
    // tool_name and tool_input default (empty / null).
    assert!(input.tool_name.is_empty());
    assert!(input.tool_input.is_null());
}

#[test]
fn parses_grep_tool_input() {
    let json = r#"{"session_id":"s1","cwd":"/repo","hook_event_name":"PreToolUse",
            "tool_name":"Grep","tool_input":{"pattern":"watcher_main","path":"crates"}}"#;
    let input: HookInput = serde_json::from_str(json).unwrap();
    let search = extract_search(&input).unwrap();
    assert_eq!(search.pattern, "watcher_main");
    assert_eq!(search.search_path.as_deref(), Some("crates"));
    assert_eq!(search.source, "grep_tool");
}

#[test]
fn bash_parser_table() {
    // (command, expected pattern, expected path)
    let positives = [
        ("rg watcher_main", "watcher_main", None),
        ("rg -n 'election retry' crates/", "election retry", Some("crates/")),
        ("grep -rn foo src", "foo", Some("src")),
        ("ag --rust frobnicate", "frobnicate", None),
        ("rg -e 'fn main' --type rust", "fn main", None),
        ("cd crates && rg spawn_listener", "spawn_listener", None),
        ("FOO=1 rg spawn_listener", "spawn_listener", None),
        ("rg -A 3 -B 2 needle haystack/", "needle", Some("haystack/")),
        // grep is the pipeline HEAD (a real search piped into a pager) → still matches.
        ("rg foo | head", "foo", None),
        (r#"rg "quoted pattern" src"#, "quoted pattern", Some("src")),
    ];
    for (cmd, pattern, path) in positives {
        let got = parse_bash_search(cmd).unwrap_or_else(|| panic!("no match for {cmd}"));
        assert_eq!(got.0, pattern, "pattern for {cmd}");
        assert_eq!(got.1.as_deref(), path, "path for {cmd}");
    }
    let negatives = [
        "ls -la",
        "cargo test",
        "rg",                                        // no pattern
        "find . -name '*.rs' -exec grep foo {} \\;", // -exec: ambiguous
        "echo `rg foo`",                             // backticks: ambiguous
        "xargs grep foo",                            // xargs: ambiguous
        "groups",                                    // not grep
        // #138: grep DOWNSTREAM of a pipe is filtering another tool's output, not a code
        // search — must not augment.
        "git log | rg fix",
        "cargo test | grep result",
        "gh run view | grep -i error",
        "cargo clippy | grep -E warning",
        "cargo test |& grep result", // |& pipes stdout+stderr → still a downstream filter
        "cargo clippy |& rg warning",
    ];
    for cmd in negatives {
        assert!(parse_bash_search(cmd).is_none(), "false positive for {cmd}");
    }
}

#[test]
fn extract_search_routes_bash_commands() {
    let json = r#"{"session_id":"s1","cwd":"/repo","hook_event_name":"PreToolUse",
            "tool_name":"Bash","tool_input":{"command":"rg -n watcher_main crates/"}}"#;
    let input: HookInput = serde_json::from_str(json).unwrap();
    let search = extract_search(&input).unwrap();
    assert_eq!(search.pattern, "watcher_main");
    assert_eq!(search.source, "bash");
}

#[test]
fn extract_search_ignores_other_tools() {
    let json = r#"{"session_id":"s1","cwd":"/repo","hook_event_name":"PreToolUse",
            "tool_name":"Read","tool_input":{"path":"/x"}}"#;
    let input: HookInput = serde_json::from_str(json).unwrap();
    assert!(extract_search(&input).is_none());
}

// ─── format_digest ────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
fn make_orientation(
    root_title: Option<&str>,
    nodes: Vec<TreeNode>,
    truncated: u32,
    load_bearing: Vec<(&str, u64)>,
    recent: Vec<&str>,
    hot: Vec<&str>,
    memory_titles: Vec<&str>,
    head: &str,
    indexed_head: &str,
    anchor: AnchorHealth,
    parser_failures: u64,
) -> Orientation {
    Orientation {
        tree: DirTree { nodes, root_memory_title: root_title.map(str::to_string), truncated },
        load_bearing: load_bearing.into_iter().map(|(p, fi)| (p.to_string(), fi)).collect(),
        recent_commits: recent.into_iter().map(str::to_string).collect(),
        hot_files: hot.into_iter().map(str::to_string).collect(),
        // Default the total to the shown-title count (no overflow). Tests exercising the
        // `(+N more)` note override `active_memory_total` after construction.
        active_memory_total: memory_titles.len() as u32,
        active_memory_titles: memory_titles.into_iter().map(str::to_string).collect(),
        head: head.to_string(),
        indexed_head: indexed_head.to_string(),
        anchor,
        total_files: 42,
        parser_failures,
    }
}

fn healthy_anchor() -> AnchorHealth {
    AnchorHealth { current: 3, relocated: 1, stale: 0, gone: 0 }
}

fn node(depth: u8, label: &str, path: &str, file_count: u32, title: Option<&str>) -> TreeNode {
    TreeNode {
        depth,
        label: label.to_string(),
        path: path.to_string(),
        file_count,
        memory_title: title.map(str::to_string),
    }
}

#[test]
fn format_digest_contains_attribution_header() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("▶ rag-rat repo intelligence"), "missing attribution header");
    assert!(s.contains("semantic_search"), "missing tool nudge");
}

#[test]
fn format_digest_purpose_line_when_root_title_present() {
    let o = make_orientation(
        Some("My project — does amazing things"),
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("My project — does amazing things"), "missing purpose line");
}

#[test]
fn format_digest_no_purpose_line_when_root_title_absent() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    // No stray purpose-like line should appear (hard to assert absence of arbitrary content,
    // but the relevant sentinel is not there).
    assert!(!s.contains("does amazing things"));
}

#[test]
fn format_digest_layout_indents_and_annotates_tree() {
    let nodes = vec![
        node(0, "src", "src", 5, None),
        node(1, "actors", "src/actors", 8, Some("per-domain actors")),
        node(1, "data", "src/data", 3, None),
    ];
    let o = make_orientation(
        None,
        nodes,
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    // LAYOUT header renders the scoped file count (make_orientation sets total_files = 42).
    assert!(
        s.contains("LAYOUT  (42 files · ‹…› = directory memory)"),
        "LAYOUT header missing file count; got:\n{s}"
    );
    // depth-0 node: no indentation.
    assert!(s.contains("\nsrc\n"), "depth-0 node should not be indented");
    // depth-1 node with memory: indented 2 spaces + label + title.
    assert!(
        s.contains("  actors  ‹per-domain actors›"),
        "depth-1 node with title missing or malformed"
    );
    // depth-1 node without memory: indented 2 spaces.
    assert!(s.contains("  data\n"), "depth-1 node without title missing");
}

#[test]
fn format_digest_truncated_note() {
    let o = make_orientation(
        None,
        vec![node(0, "src", "src", 5, None)],
        7,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("… (+7 more)"), "missing truncated note");
}

#[test]
fn format_digest_load_bearing_fan_in() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![
            ("crates/rag-rat-core/src/index/mod.rs", 2286),
            ("crates/rag-rat-core/src/main.rs", 42),
            ("src/database.rs", 999),
        ],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("load-bearing:"), "missing load-bearing prefix");
    // crates/.../src/ prefix must be stripped.
    assert!(s.contains("index/mod.rs (fan_in 2286)"), "crates path not shortened; got:\n{s}");
    assert!(s.contains("main.rs (fan_in 42)"), "crates path not shortened for main.rs; got:\n{s}");
    // Path without the crates prefix stays unchanged.
    assert!(s.contains("src/database.rs (fan_in 999)"), "non-crates path changed; got:\n{s}");
}

#[test]
fn format_digest_memories_overflow_uses_true_total() {
    // The query truncates to 3 titles, but the repo has 9 active non-dir memories.
    // The overflow note must report the honest remainder (9 - 3 = 6), not 5 - 3 = 2.
    let titles: Vec<&str> = vec!["alpha", "beta", "gamma"];
    let mut o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        titles,
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    o.active_memory_total = 9;
    let s = format_digest(&o, true, true);
    // All 3 shown titles must appear.
    assert!(s.contains("alpha"), "first memory title missing");
    assert!(s.contains("beta"), "second memory title missing");
    assert!(s.contains("gamma"), "third memory title missing");
    // Overflow note reflects the true total (9 - 3 = 6 more).
    assert!(s.contains("(+6 more)"), "overflow note must use true total; got:\n{s}");
}

#[test]
fn format_digest_memories_no_overflow_when_three_or_fewer() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec!["alpha", "beta", "gamma"],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("alpha · beta · gamma"), "three titles should be shown");
    assert!(!s.contains("more)"), "no overflow note when ≤3 titles");
}

#[test]
fn short_path_strips_crates_prefix() {
    assert_eq!(
        short_path("crates/rag-rat-core/src/index/mod.rs"),
        "index/mod.rs",
        "three-segment crates prefix should be stripped"
    );
    assert_eq!(
        short_path("crates/rag-rat-mcp/src/server.rs"),
        "server.rs",
        "single-file under src should be stripped"
    );
}

#[test]
fn short_path_leaves_non_crates_paths_unchanged() {
    assert_eq!(short_path("src/database.rs"), "src/database.rs");
    assert_eq!(short_path("crates/only-two"), "crates/only-two");
    assert_eq!(
        short_path("crates/foo/not-src/bar.rs"),
        "crates/foo/not-src/bar.rs",
        "second segment is 'not-src', must not strip"
    );
    assert_eq!(short_path(""), "");
}

// ─── Health line table test ───────────────────────────────────────────────

struct HealthCase {
    live: bool,
    enabled: bool,
    head: &'static str,
    indexed: &'static str,
    expected: &'static str,
}

#[test]
fn format_digest_health_watcher_combinations() {
    let cases = [
        HealthCase {
            live: true,
            enabled: true,
            head: "aaa",
            indexed: "aaa",
            expected: "index fresh (watcher live)",
        },
        HealthCase {
            live: true,
            enabled: true,
            head: "aaa",
            indexed: "bbb",
            expected: "index syncing (watcher live)",
        },
        HealthCase {
            live: false,
            enabled: true,
            head: "aaa",
            indexed: "bbb",
            expected: "index stale — start the rag-rat MCP server",
        },
        HealthCase {
            live: false,
            enabled: false,
            head: "aaa",
            indexed: "bbb",
            expected: "watcher off; index stale — run 'rag-rat index'",
        },
        HealthCase {
            live: false,
            enabled: true,
            head: "aaa",
            indexed: "aaa",
            expected: "index fresh",
        },
    ];
    for case in &cases {
        let o = make_orientation(
            None,
            vec![],
            0,
            vec![],
            vec![],
            vec![],
            vec![],
            case.head,
            case.indexed,
            healthy_anchor(),
            0,
        );
        let s = format_digest(&o, case.live, case.enabled);
        assert!(
            s.contains(case.expected),
            "health line mismatch for live={} enabled={} head={}: expected {:?}, got:\n{}",
            case.live,
            case.enabled,
            case.head,
            case.expected,
            s
        );
    }
}

#[test]
fn format_digest_gone_adds_doctor_nudge() {
    let anchor = AnchorHealth { current: 2, relocated: 0, stale: 1, gone: 3 };
    let o =
        make_orientation(None, vec![], 0, vec![], vec![], vec![], vec![], "abc", "abc", anchor, 0);
    let s = format_digest(&o, true, true);
    assert!(s.contains("3 gone → run 'rag-rat memory doctor'"), "missing gone nudge");
}

#[test]
fn format_digest_no_doctor_nudge_when_gone_is_zero() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        0,
    );
    let s = format_digest(&o, true, true);
    assert!(!s.contains("memory doctor"), "unexpected doctor nudge when gone=0");
}

#[test]
fn format_digest_parser_failures_shown_when_nonzero() {
    let o = make_orientation(
        None,
        vec![],
        0,
        vec![],
        vec![],
        vec![],
        vec![],
        "abc",
        "abc",
        healthy_anchor(),
        5,
    );
    let s = format_digest(&o, true, true);
    assert!(s.contains("parser failures: 5"), "missing parser failures note");
}