difflore-cli 0.1.0

Your AI coding agent, taught by your team's PR reviews — a local-first, open-source MCP server that turns past review comments into rules your agent follows automatically.
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
//! MCP installer: registers difflore as an MCP server with every detected
//! AI coding tool, in one shot. Public entry points are `install_all`,
//! `uninstall_all`, `status`, `collect_status_snapshot`,
//! `detect_install_drift`, and `maybe_print_mcp_hint`.
//!
//! Detect + install + uninstall are all driven by the single `AGENTS` table in
//! `registry.rs` (one [`registry::AgentSpec`] row per surface). The drivers
//! delegate to the leaf format engines — `json_config.rs`, `goose_yaml.rs`,
//! `hooks_install.rs` — and shared path / record helpers live in `common.rs`.
//! Adding an agent means adding one `AGENTS` row, not touching a probe table,
//! a per-agent install fn, an install/uninstall dispatch list, and the
//! name/UX maps.

mod common;
mod diagnosis;
mod goose_yaml;
mod hooks_install;
mod install;
mod json_config;
mod manifest;
mod registry;
mod snapshot;
mod status_display;
mod types;
mod uninstall;

pub use install::{install_all, update_all};
pub use snapshot::{collect_status_snapshot, collect_status_snapshot_with_runtime_probe};
pub use status_display::{
    detect_install_drift, detect_install_repair_targets, maybe_print_mcp_hint, status,
};
pub use types::{
    CanonicalRecordState, CanonicalRecordStatus, InstallState, McpClientStatus, McpRuntimeProbe,
    McpStatusDiagnosis, McpStatusSnapshot, RuntimeProbeState, Status, TargetOutcome, TargetStatus,
};
pub use uninstall::uninstall_all;

#[cfg(test)]
mod test_util {
    use std::path::PathBuf;

    pub(super) fn tmp_settings_path() -> (tempfile::TempDir, PathBuf) {
        tmp_named_path("settings.json")
    }

    pub(super) fn tmp_named_path(filename: &str) -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::TempDir::new().expect("tempdir");
        let path = dir.path().join(filename);
        (dir, path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::{
        common::{self, canonical_target_key},
        diagnosis::{
            client_name_for_surface, diagnose_status_snapshot, install_repair_targets_for_snapshot,
        },
        install::{
            failed_outcome_names, install_outcome_verb, outcome_already_installed,
            outcome_client_names, should_write_canonical_record,
        },
        registry::AGENTS,
        snapshot::collect_client_statuses_from_agents,
    };
    use std::{collections::BTreeSet, fs};

    // ── Registry single-table guard rails (Item ④) ─────────────────────────

    #[test]
    fn agents_table_orders_claude_then_claude_hooks_then_codex_first() {
        // R4: `collect_agent_statuses` relies on this row order (Claude Code →
        // Claude Code hooks → Codex first), now encoded directly in the table
        // instead of a manual reshuffle. Guard the first three names.
        let first_three: Vec<&str> = AGENTS.iter().take(3).map(|spec| spec.name).collect();
        assert_eq!(
            first_three,
            vec!["Claude Code", "Claude Code hooks", "Codex"]
        );
    }

    #[test]
    fn every_agent_surface_resolves_to_a_known_client() {
        // R1: surface `name` strings are load-bearing — every one must resolve
        // through `client_name_for_surface` to a real client (never the
        // "unknown client" sentinel), or roll-up and record-matching silently
        // break.
        for spec in AGENTS {
            assert_ne!(
                client_name_for_surface(spec.name),
                "unknown client",
                "surface {:?} did not resolve to a known client",
                spec.name
            );
            // The display client on the row must agree with the derived one.
            assert_eq!(
                client_name_for_surface(spec.name),
                spec.client,
                "surface {:?} client mismatch",
                spec.name
            );
        }
    }

    #[test]
    fn agents_table_keeps_legacy_surface_name_set() {
        // R1 mitigation: pin the exact surface-name set so a typo (e.g.
        // "Gemini CLI" vs "Gemini") can't slip in and desync the canonical
        // record / client roll-up.
        let names: BTreeSet<&str> = AGENTS.iter().map(|spec| spec.name).collect();
        let expected: BTreeSet<&str> = [
            "Claude Code",
            "Claude Code hooks",
            "Codex",
            "Cursor",
            "Cursor hooks",
            "Gemini",
            "Gemini hooks",
            "Copilot CLI",
            "Antigravity",
            "Goose",
            "Crush",
            "Roo Code",
            "Warp",
            "Windsurf hooks",
        ]
        .into_iter()
        .collect();
        assert_eq!(names, expected);
    }

    #[test]
    fn client_matrix_collapses_raw_surfaces_to_eleven_clients() {
        let clients = collect_client_statuses_from_agents(&[
            TargetStatus {
                name: "Claude Code",
                detected: true,
                state: InstallState::Installed,
                detail: None,
            },
            TargetStatus {
                name: "Claude Code hooks",
                detected: true,
                state: InstallState::Installed,
                detail: None,
            },
            TargetStatus {
                name: "Cursor",
                detected: true,
                state: InstallState::Installed,
                detail: None,
            },
            TargetStatus {
                name: "Cursor hooks",
                detected: true,
                state: InstallState::NotInstalled,
                detail: None,
            },
        ]);
        assert_eq!(clients.len(), 11);
        let claude = clients
            .iter()
            .find(|client| client.name == "Claude Code")
            .expect("claude client");
        assert_eq!(claude.state, InstallState::Installed);
        let cursor = clients
            .iter()
            .find(|client| client.name == "Cursor")
            .expect("cursor client");
        assert_eq!(cursor.state, InstallState::Conflict);
    }

    #[test]
    fn client_detail_ignores_undetected_optional_surfaces() {
        let clients = collect_client_statuses_from_agents(&[
            TargetStatus {
                name: "Cursor",
                detected: true,
                state: InstallState::Installed,
                detail: Some("~/.cursor/mcp.json".to_owned()),
            },
            TargetStatus {
                name: "Cursor hooks",
                detected: false,
                state: InstallState::NotInstalled,
                detail: Some("./.cursor/hooks.json not found".to_owned()),
            },
        ]);
        let cursor = clients
            .iter()
            .find(|client| client.name == "Cursor")
            .expect("cursor client");

        assert_eq!(cursor.state, InstallState::Installed);
        assert_eq!(
            cursor.detail.as_deref(),
            Some("1/1 detected surface(s) installed")
        );
    }

    #[test]
    fn canonical_target_key_normalizes_display_and_cli_names() {
        assert_eq!(canonical_target_key("Claude Code"), "claude");
        assert_eq!(canonical_target_key("Claude Code hooks"), "claude hooks");
        assert_eq!(canonical_target_key("claude"), "claude");
        assert_eq!(canonical_target_key("Codex"), "codex");
        assert_eq!(canonical_target_key("codex"), "codex");
        assert_eq!(canonical_target_key("Gemini hooks"), "gemini hooks");
    }

    #[test]
    fn dry_run_outcome_verbs_describe_plan_not_execution() {
        assert_eq!(
            install_outcome_verb(&Status::Installed, true, false),
            "would install"
        );
        assert_eq!(
            install_outcome_verb(&Status::Updated, true, false),
            "would update"
        );
        assert_eq!(
            install_outcome_verb(&Status::Installed, true, true),
            "already installed"
        );
        assert_eq!(
            install_outcome_verb(&Status::Updated, true, true),
            "already installed"
        );
        assert_eq!(
            install_outcome_verb(
                &Status::Skipped("DiffLore plugin already installed".to_owned()),
                true,
                true
            ),
            "already installed"
        );
        assert_eq!(
            install_outcome_verb(&Status::Installed, false, false),
            "installed"
        );
        assert_eq!(
            install_outcome_verb(&Status::Updated, false, false),
            "updated"
        );
    }

    #[test]
    fn dry_run_already_installed_uses_canonical_surface_names() {
        let installed_surfaces = BTreeSet::from([canonical_target_key("Claude Code hooks")]);
        let outcome = TargetOutcome {
            name: "Claude Code hooks",
            status: Status::Updated,
            detail: "~/.claude/settings.json".to_owned(),
        };

        assert!(outcome_already_installed(&outcome, &installed_surfaces));
    }

    #[test]
    fn outcome_client_names_collapses_hook_surfaces_to_restart_clients() {
        let outcomes = vec![
            TargetOutcome {
                name: "Cursor",
                status: Status::Installed,
                detail: "~/.cursor/mcp.json".to_owned(),
            },
            TargetOutcome {
                name: "Cursor hooks",
                status: Status::Updated,
                detail: "./.cursor/hooks.json".to_owned(),
            },
            TargetOutcome {
                name: "Gemini hooks",
                status: Status::Skipped("not found".to_owned()),
                detail: String::new(),
            },
        ];

        assert_eq!(outcome_client_names(&outcomes), vec!["Cursor".to_owned()]);
    }

    #[test]
    fn canonical_record_is_skipped_on_partial_install_failure() {
        let installed = vec!["Claude Code"];
        let failed = vec!["Cursor"];
        assert!(!should_write_canonical_record(false, &installed, &failed));
        assert!(should_write_canonical_record(false, &installed, &[]));
        assert!(!should_write_canonical_record(true, &installed, &[]));
        assert!(!should_write_canonical_record(false, &[], &[]));
    }

    #[test]
    fn json_probe_requires_command_and_mcp_server_arg() {
        let (_tmp, path) = test_util::tmp_named_path("mcp.json");
        fs::write(
            &path,
            r#"{ "mcpServers": { "difflore": { "command": "/tmp/fake/difflore", "args": [] } } }"#,
        )
        .expect("write config");

        let status =
            common::probe_json_install("Cursor", &path, "mcpServers", "/tmp/fake/difflore");
        assert_eq!(status.state, InstallState::Conflict);
        assert!(
            status
                .detail
                .as_deref()
                .is_some_and(|detail| detail.contains("args=[]"))
        );

        fs::write(
            &path,
            r#"{ "mcpServers": { "difflore": { "command": "/tmp/fake/difflore", "args": ["mcp-server"] } } }"#,
        )
        .expect("write config");
        let status =
            common::probe_json_install("Cursor", &path, "mcpServers", "/tmp/fake/difflore");
        assert_eq!(status.state, InstallState::Installed);
    }

    #[test]
    fn failed_outcome_names_only_counts_real_errors() {
        let outcomes = vec![
            TargetOutcome {
                name: "Claude Code",
                status: Status::Installed,
                detail: String::new(),
            },
            TargetOutcome {
                name: "Cursor",
                status: Status::Skipped("not detected".to_owned()),
                detail: String::new(),
            },
            TargetOutcome {
                name: "Gemini",
                status: Status::Error("write failed".to_owned()),
                detail: String::new(),
            },
        ];

        assert_eq!(failed_outcome_names(&outcomes), vec!["Gemini"]);
    }

    #[test]
    fn runtime_probe_output_accepts_initialize_and_tools_list() {
        let stdout = concat!(
            r#"{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05"}}"#,
            "\n",
            r#"{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"search_rules"},{"name":"get_rules"}]}}"#,
            "\n",
            r#"{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"{\"results\":[{\"id\":\"rule-1\",\"title\":\"Probe rule title\"}]}"}],"_meta":{"impact":{"kind":"rules_index","rulesInjected":1,"rulesIndexed":12}}}}"#,
            "\n"
        );
        let probe = common::evaluate_runtime_probe_output(stdout, "", true);
        assert_eq!(probe.state, RuntimeProbeState::Ok);
        assert!(probe.initialized);
        assert!(probe.tools_listed);
        assert!(probe.tool_call_completed);
        assert_eq!(probe.tool_call_name.as_deref(), Some("search_rules"));
        assert_eq!(probe.tool_call_rules_injected, Some(1));
        assert_eq!(probe.tool_call_rules_indexed, Some(12));
        assert_eq!(
            probe.tool_call_top_result.as_deref(),
            Some("Probe rule title")
        );
        assert_eq!(probe.tool_count, Some(2));
        assert_eq!(
            probe.tool_names,
            vec!["search_rules".to_owned(), "get_rules".to_owned()]
        );
        // The human-facing detail line summarizes a clean handshake without
        // leaking internal probe wiring (the search_rules tool call itself is
        // asserted via the structured `tool_call_name` field above).
        assert!(
            probe.detail.contains("MCP handshake and tool listing OK"),
            "{}",
            probe.detail
        );
    }

    #[test]
    fn runtime_probe_input_scopes_search_to_changed_file() {
        let input = common::build_runtime_probe_input(Some("crates/app/src/lib.rs".to_owned()));
        let messages = input
            .lines()
            .map(|line| serde_json::from_str::<serde_json::Value>(line).expect("valid json"))
            .collect::<Vec<_>>();

        assert_eq!(messages.len(), 3);
        assert_eq!(messages[2]["method"], "tools/call");
        assert_eq!(messages[2]["params"]["name"], "search_rules");
        assert_eq!(
            messages[2]["params"]["arguments"]["file"],
            "crates/app/src/lib.rs"
        );
        assert!(
            messages[2]["params"]["arguments"]["intent"]
                .as_str()
                .expect("intent")
                .contains("crates/app/src/lib.rs")
        );
        assert_eq!(
            messages[2]["params"]["arguments"]["session_id"],
            "difflore-mcp-status"
        );
    }

    #[test]
    fn runtime_probe_input_omits_file_when_no_diff_exists() {
        let input = common::build_runtime_probe_input(None);
        let messages = input
            .lines()
            .map(|line| serde_json::from_str::<serde_json::Value>(line).expect("valid json"))
            .collect::<Vec<_>>();

        assert_eq!(messages.len(), 3);
        assert!(messages[2]["params"]["arguments"].get("file").is_none());
        assert_eq!(
            messages[2]["params"]["arguments"]["intent"],
            "verify DiffLore MCP can recall review memory"
        );
    }

    #[test]
    fn runtime_probe_output_reports_missing_tool_list() {
        let stdout = r#"{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05"}}"#;
        let probe = common::evaluate_runtime_probe_output(stdout, "boom", false);
        assert_eq!(probe.state, RuntimeProbeState::Failed);
        assert!(probe.initialized);
        assert!(!probe.tools_listed);
        assert!(probe.detail.contains("stderr: boom"));
    }

    #[test]
    fn runtime_probe_output_requires_search_rules_tool_call() {
        let stdout = concat!(
            r#"{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05"}}"#,
            "\n",
            r#"{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"search_rules"},{"name":"get_rules"}]}}"#,
            "\n"
        );
        let probe = common::evaluate_runtime_probe_output(stdout, "", true);
        assert_eq!(probe.state, RuntimeProbeState::Failed);
        assert!(probe.initialized);
        assert!(probe.tools_listed);
        assert!(!probe.tool_call_completed);
        assert!(probe.detail.contains("did not complete search_rules"));
    }

    fn diagnosis_fixture(
        runtime_state: RuntimeProbeState,
        record_state: CanonicalRecordState,
    ) -> McpStatusSnapshot {
        let (recorded_targets, actual_targets) =
            if matches!(record_state, CanonicalRecordState::Stale) {
                (
                    vec!["Claude Code".to_owned()],
                    vec!["Claude Code".to_owned(), "Claude Code hooks".to_owned()],
                )
            } else {
                (
                    vec!["Claude Code".to_owned()],
                    vec!["Claude Code".to_owned()],
                )
            };
        McpStatusSnapshot {
            binary: "difflore".to_owned(),
            canonical_record: CanonicalRecordStatus {
                path: Some("mcp.json".to_owned()),
                state: record_state,
                detail: None,
                recorded_targets,
                actual_targets,
            },
            runtime_probe: Some(McpRuntimeProbe {
                state: runtime_state,
                detail: "probe detail".to_owned(),
                initialized: matches!(runtime_state, RuntimeProbeState::Ok),
                tools_listed: matches!(runtime_state, RuntimeProbeState::Ok),
                tool_call_completed: matches!(runtime_state, RuntimeProbeState::Ok),
                tool_call_name: matches!(runtime_state, RuntimeProbeState::Ok)
                    .then(|| "search_rules".to_owned()),
                tool_call_rules_injected: None,
                tool_call_rules_indexed: None,
                tool_call_top_result: None,
                tool_count: Some(7),
                tool_names: Vec::new(),
            }),
            diagnosis: None,
            clients: vec![McpClientStatus {
                name: "Claude Code",
                detected: true,
                state: InstallState::Installed,
                detail: None,
                surfaces: Vec::new(),
            }],
            agents: Vec::new(),
        }
    }

    #[test]
    fn diagnosis_distinguishes_healthy_runtime_from_install_record_drift() {
        let snapshot = diagnosis_fixture(RuntimeProbeState::Ok, CanonicalRecordState::Stale);
        let diagnosis = diagnose_status_snapshot(&snapshot);
        assert!(diagnosis.summary.contains("server is healthy"));
        assert!(diagnosis.summary.contains("client-wiring drift"));
        assert!(diagnosis.next_step.contains("difflore agents install"));
        assert_eq!(diagnosis.affected_clients, vec!["Claude Code".to_owned()]);
        assert!(
            diagnosis
                .actions
                .iter()
                .any(|action| action.contains("Restart/reload affected client(s): Claude Code"))
        );
        assert!(
            diagnosis
                .actions
                .iter()
                .any(|action| action.contains("Claude Code: restart Claude Code"))
        );
    }

    #[test]
    fn diagnosis_for_clean_runtime_lists_installed_client_reload_steps() {
        let snapshot = diagnosis_fixture(RuntimeProbeState::Ok, CanonicalRecordState::Present);
        let diagnosis = diagnose_status_snapshot(&snapshot);
        assert!(diagnosis.next_step.contains("Transport closed"));
        assert!(
            diagnosis
                .actions
                .iter()
                .any(|action| action.contains("Claude Code: restart Claude Code"))
        );
        assert!(
            diagnosis
                .actions
                .iter()
                .any(|action| action.contains("completes a search_rules"))
        );
    }

    #[test]
    fn install_repair_targets_include_canonical_hook_drift() {
        let snapshot = diagnosis_fixture(RuntimeProbeState::Ok, CanonicalRecordState::Stale);
        assert_eq!(
            install_repair_targets_for_snapshot(&snapshot),
            vec!["Claude Code".to_owned()]
        );
    }

    #[test]
    fn install_repair_targets_are_empty_for_clean_installed_client() {
        let snapshot = diagnosis_fixture(RuntimeProbeState::Ok, CanonicalRecordState::Present);
        assert!(install_repair_targets_for_snapshot(&snapshot).is_empty());
    }

    #[test]
    fn diagnosis_flags_runtime_failure_as_memory_server_problem() {
        let snapshot = diagnosis_fixture(RuntimeProbeState::Failed, CanonicalRecordState::Present);
        let diagnosis = diagnose_status_snapshot(&snapshot);
        assert!(diagnosis.summary.contains("failed the stdio self-check"));
        assert!(diagnosis.next_step.contains("stderr/details"));
        assert!(
            diagnosis
                .actions
                .iter()
                .any(|action| action.contains("Rebuild or upgrade"))
        );
    }
}