difflore-core 0.2.0

Core library for the difflore CLI — rule store, retrieval, MCP server, hooks, cloud sync. Not intended for direct use; depend on `difflore-cli` instead.
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
use serde_json::{Value, json};

use crate::context::retrieval;
use crate::context::types::{PastVerdict, PastVerdictScope};
use crate::observability::trajectory::TrajectoryStep;

use super::super::{
    McpState, build_cost_meta, detect_git_remote_owner_repos, emit_trajectory_step, estimate_tokens,
};
use super::validate::{MCP_TEXT_ARG_CHAR_LIMIT, validate_mcp_text_arg};

pub(crate) async fn tool_get_past_verdicts(
    state: &McpState,
    args: &Value,
) -> Result<Value, (i32, String)> {
    let query = args
        .get("query")
        .and_then(|v| v.as_str())
        .ok_or((-32602, "Missing required parameter: query".to_owned()))?;
    validate_mcp_text_arg("query", query, MCP_TEXT_ARG_CHAR_LIMIT)?;
    // Optional `file` enables server-side cascade ordering.
    let target_file = args
        .get("file")
        .and_then(|v| v.as_str())
        .and_then(normalize_target_file_for_cloud);
    let top_k = args
        .get("top_k")
        .and_then(Value::as_u64)
        .map_or(10, |n| n.clamp(1, 10) as usize);

    // Scope to explicit or detected repos. On detection failure, return
    // no verdicts rather than widening to other projects.
    let explicit_repo = args
        .get("repo_full_name")
        .and_then(|v| v.as_str())
        .map(String::from);
    let repo_scopes: Vec<String> = if let Some(repo) = explicit_repo {
        vec![repo]
    } else {
        // Warm the configured-GitLab-host cache before detecting remotes so a
        // fresh MCP-server process that calls get_past_verdicts before any
        // recall can still resolve self-managed GitLab scopes. Without it the
        // detection falls empty and the verdict recall silently returns nothing.
        // Mirrors hook.rs / search_rules.rs / remember_rule.rs.
        crate::mcp_server::hook::refresh_configured_gitlab_hosts_for_remote_detection().await;
        detect_git_remote_owner_repos()
    };

    if !crate::infra::env::mcp_cloud_reads_enabled() {
        let text = "MCP cloud-backed verdict recall is local-only by default. Set `DIFFLORE_MCP_ALLOW_CLOUD_READS=1` for this MCP process if you want cloud review history to enter agent context, or run DiffLore cloud commands explicitly in the CLI.";
        let tokens_used = estimate_tokens(text);
        return Ok(json!({
            "content": [{ "type": "text", "text": text }],
            "_meta": {
                "cost": build_cost_meta(tokens_used, None),
                "cloudReadAllowed": false,
                "impact": { "verdictsRecalled": 0, "kind": "verdicts" }
            }
        }));
    }

    // Retain per-repo provenance before merging fan-out responses.
    let mut repo_by_extraction: std::collections::HashMap<String, (String, f32)> =
        std::collections::HashMap::new();
    let cloud = &state.cloud;
    let cloud_status = crate::cloud::sync::fetch_cloud_status(cloud).await;
    if !cloud_status.logged_in {
        let text = "Local recall is available now. Use `difflore cloud login` to append cloud PR review memory.";
        let tokens_used = estimate_tokens(text);
        return Ok(json!({
            "content": [{ "type": "text", "text": text }],
            "_meta": {
                "cost": build_cost_meta(tokens_used, None),
                "loggedIn": false,
                "recallScope": PastVerdictScope::Personal.as_str(),
                "teamId": Option::<String>::None,
                "impact": { "verdictsRecalled": 0, "kind": "verdicts" }
            }
        }));
    }
    let team_id = cloud_status.team_id.clone();
    let scope = if team_id.is_some() {
        PastVerdictScope::Team
    } else {
        PastVerdictScope::Personal
    };
    // Cap fan-out at 4 repos to bound MCP latency.
    let recalls = repo_scopes.into_iter().take(4).map(|repo| {
        let target_file = target_file.clone();
        let team_id = team_id.clone();
        async move {
            let group = retrieval::retrieve_past_verdicts_by_text_with_team(
                cloud,
                query,
                Some(repo.as_str()),
                scope,
                u32::try_from(top_k).unwrap_or(10),
                target_file.as_deref(),
                team_id.as_deref(),
            )
            .await;
            (repo, group)
        }
    });

    let mut groups = Vec::new();
    for (repo, group) in futures_util::future::join_all(recalls).await {
        for v in &group {
            let entry = repo_by_extraction
                .entry(v.extraction_id.clone())
                .or_insert_with(|| (repo.clone(), v.similarity));
            if v.similarity > entry.1 {
                *entry = (repo.clone(), v.similarity);
            }
        }
        groups.push(group);
    }
    let verdicts: Vec<PastVerdict> = retrieval::merge_past_verdicts(groups, top_k);

    if verdicts.is_empty() {
        let decision_scope = if team_id.is_some() {
            "team decisions"
        } else {
            "personal decisions"
        };
        let text = format!(
            "No past verdicts found for this query.\n\n> No matching cloud {decision_scope} yet. Import past GitHub reviews with `difflore import-reviews`, then run `difflore status` and `difflore recall --diff` to verify the recall path."
        );
        let tokens_used = estimate_tokens(&text);
        return Ok(json!({
            "content": [{ "type": "text", "text": text }],
            "_meta": {
                "cost": build_cost_meta(tokens_used, None),
                "recallScope": scope.as_str(),
                "teamId": team_id,
                "impact": { "verdictsRecalled": 0, "kind": "verdicts" }
            }
        }));
    }

    // Format verdicts into readable text, including repo provenance when
    // available.
    let mut text = String::from("## Past Review Verdicts\n\n");
    for (i, v) in verdicts.iter().enumerate() {
        let source_repo = repo_by_extraction
            .get(&v.extraction_id)
            .map(|(repo, _)| repo.as_str());
        let provenance = source_pr_label(v, source_repo)
            .or_else(|| source_repo.map(str::to_owned))
            .map(|source| format!(" <- from {source}"))
            .unwrap_or_default();
        text.push_str(&format!(
            "### {} [{}, similarity {:.2}]{}\n\n",
            i + 1,
            v.status,
            v.similarity,
            provenance,
        ));
        text.push_str(&format!("**Code:**\n```\n{}\n```\n\n", v.code_snippet));
        text.push_str(&format!("**Issue:** {}\n", v.issue_text));
        if let Some(reason) = v.reason.as_ref()
            && !reason.is_empty()
        {
            text.push_str(&format!("**Reason:** {reason}\n"));
        }
        if let Some(line) = source_pr_line(v, source_repo) {
            text.push_str(&format!("{line}\n"));
        }
        text.push('\n');
    }

    let n = verdicts.len();
    let top_sim = verdicts.first().map_or(0.0, |v| v.similarity);
    let source_pr_linked = verdicts
        .iter()
        .filter(|v| {
            let source_repo = repo_by_extraction
                .get(&v.extraction_id)
                .map(|(repo, _)| repo.as_str());
            source_pr_label(v, source_repo).is_some()
        })
        .count();
    let any_repo_attributed = verdicts
        .iter()
        .any(|v| repo_by_extraction.contains_key(&v.extraction_id));
    let cite_hint = if source_pr_linked > 0 {
        " If you follow one, cite it AND its source PR (e.g. \"past verdict #1 from gin-gonic/gin#4336 applies here\") so the user sees which real review shaped this change."
    } else if any_repo_attributed {
        " If you follow one, cite it AND its source repo (e.g. \"past verdict #1 from gin-gonic/gin applies here\") so the user sees which past team judgment shaped this change."
    } else {
        " If you follow one, cite it (e.g. \"past verdict #1 applies here\") so the user sees which knowledge shaped this change."
    };
    text.push_str(&format!(
        "\n> **DiffLore recalled {} past team decision{}** (top similarity {:.2}).{}",
        n,
        if n == 1 { "" } else { "s" },
        top_sim,
        cite_hint,
    ));

    // Track MCP response size; past verdicts are not injected rules.
    let tokens_used = estimate_tokens(&text);
    emit_trajectory_step(&TrajectoryStep::McpResponseSize {
        tool: "get_past_verdicts".to_owned(),
        total_tokens: tokens_used,
        rules_injected: 0,
    });

    Ok(json!({
        "content": [{
            "type": "text",
            "text": text.trim_end()
        }],
        "_meta": {
            "cost": build_cost_meta(tokens_used, None),
            "recallScope": scope.as_str(),
            "teamId": team_id,
            "impact": {
                "verdictsRecalled": n,
                "kind": "verdicts",
                "topSimilarity": top_sim,
                "sourcePrLinked": source_pr_linked,
            }
        }
    }))
}

fn source_pr_line(verdict: &PastVerdict, source_repo: Option<&str>) -> Option<String> {
    let label = source_pr_label(verdict, source_repo)?;
    let mut line = format!("**Source PR:** {label}");

    if let Some(title) = verdict.source_pr_title.as_deref().map(str::trim)
        && !title.is_empty()
    {
        line.push_str(&format!(" - {title}"));
    }

    if let Some(url) = verdict.source_pr_url.as_deref().map(str::trim)
        && !url.is_empty()
    {
        line.push_str(&format!(" ({url})"));
    }

    Some(line)
}

fn source_pr_label(verdict: &PastVerdict, source_repo: Option<&str>) -> Option<String> {
    let parsed = verdict
        .source_pr_url
        .as_deref()
        .and_then(parse_github_pr_url);

    if let Some(number) = verdict.source_pr_number {
        let repo = source_repo
            .map(str::trim)
            .filter(|r| !r.is_empty())
            .map(str::to_owned)
            .or_else(|| parsed.as_ref().map(|(repo, _)| repo.clone()));

        return Some(match repo {
            Some(repo) => format!("{repo}#{number}"),
            None => format!("PR #{number}"),
        });
    }

    if let Some((repo, Some(number))) = parsed {
        return Some(format!("{repo}#{number}"));
    }

    verdict
        .source_pr_url
        .as_deref()
        .map(str::trim)
        .filter(|url| !url.is_empty())
        .map(str::to_owned)
}

fn parse_github_pr_url(url: &str) -> Option<(String, Option<i64>)> {
    let rest = url.split_once("github.com/")?.1;
    let mut parts = rest.trim_matches('/').split('/');
    let owner = parts.next()?.trim();
    let repo = parts.next()?.trim();
    if owner.is_empty() || repo.is_empty() {
        return None;
    }

    let kind = parts.next();
    let number = parts.next().and_then(|n| n.parse::<i64>().ok());
    let pr_number = if matches!(kind, Some("pull" | "pulls")) {
        number
    } else {
        None
    };

    Some((format!("{owner}/{repo}"), pr_number))
}

fn normalize_target_file_for_cloud(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() || trimmed == "unknown" {
        return None;
    }
    let normalized = trimmed.replace('\\', "/");
    let cwd = std::env::current_dir().ok();
    if let Some(cwd) = cwd {
        let path = std::path::Path::new(trimmed);
        if path.is_absolute()
            && let Ok(relative) = path.strip_prefix(cwd)
        {
            return path_components_for_cloud(relative, usize::MAX);
        }
    }
    let path = std::path::Path::new(&normalized);
    let max_components = if path.is_absolute() || has_windows_drive_prefix(&normalized) {
        4
    } else {
        usize::MAX
    };
    path_components_for_cloud(path, max_components)
}

fn has_windows_drive_prefix(path: &str) -> bool {
    let bytes = path.as_bytes();
    bytes.first().is_some_and(u8::is_ascii_alphabetic) && bytes.get(1) == Some(&b':')
}

fn path_components_for_cloud(path: &std::path::Path, max_components: usize) -> Option<String> {
    let mut parts: Vec<String> = path
        .components()
        .filter_map(|component| match component {
            std::path::Component::Normal(value) => value.to_str().map(ToOwned::to_owned),
            _ => None,
        })
        .filter(|part| part != "." && part != ".." && !part.trim().is_empty())
        .collect();
    if parts.is_empty() {
        return None;
    }
    if parts.len() > max_components {
        parts = parts.split_off(parts.len() - max_components);
    }
    let joined = parts.join("/");
    Some(joined.chars().take(512).collect())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn verdict() -> PastVerdict {
        PastVerdict {
            extraction_id: "e1".to_owned(),
            code_snippet: "snippet".to_owned(),
            issue_text: "issue".to_owned(),
            status: "accepted".to_owned(),
            reason: Some("reason".to_owned()),
            similarity: 0.91,
            created_at: "2026-04-10T00:00:00Z".to_owned(),
            signature: None,
            source_pr_number: Some(4336),
            source_pr_title: Some("fix recovery panic handling".to_owned()),
            source_pr_url: Some("https://github.com/gin-gonic/gin/pull/4336".to_owned()),
        }
    }

    #[test]
    fn source_pr_label_prefers_recall_repo_with_number() {
        assert_eq!(
            source_pr_label(&verdict(), Some("upstream/fork")),
            Some("upstream/fork#4336".to_owned())
        );
    }

    #[test]
    fn source_pr_line_includes_exact_pr_title_and_url() {
        assert_eq!(
            source_pr_line(&verdict(), Some("gin-gonic/gin")),
            Some("**Source PR:** gin-gonic/gin#4336 - fix recovery panic handling (https://github.com/gin-gonic/gin/pull/4336)".to_owned())
        );
    }

    #[test]
    fn source_pr_label_can_parse_github_pr_url_without_number_field() {
        let mut verdict = verdict();
        verdict.source_pr_number = None;

        assert_eq!(
            source_pr_label(&verdict, None),
            Some("gin-gonic/gin#4336".to_owned())
        );
    }

    #[test]
    fn normalize_target_file_strips_absolute_prefix() {
        let path = if cfg!(windows) {
            r"C:\Users\alice\workspace\crates\difflore-core\src\lib.rs"
        } else {
            "/Users/alice/workspace/crates/difflore-core/src/lib.rs"
        };

        let normalized = normalize_target_file_for_cloud(path).expect("normalized path");

        assert_eq!(normalized, "crates/difflore-core/src/lib.rs");
        assert!(!normalized.contains("alice"));
        assert!(!normalized.contains("workspace"));
    }

    #[test]
    fn normalize_target_file_keeps_relative_path() {
        assert_eq!(
            normalize_target_file_for_cloud("crates/difflore-core/src/lib.rs"),
            Some("crates/difflore-core/src/lib.rs".to_owned())
        );
    }
}