basemind 0.24.0

Full AI context layer over MCP — tree-sitter code-map, document RAG (PDF/Office/HTML/email + OCR + reranker), shared agent memory, on-demand web crawl, git history + blame + per-symbol diff. 300+ languages, 10+ coding-agent harnesses, content-addressed Fjall + LanceDB.
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
//! The five `git` modes scoped to a single path — `diff`, `diff_outline`, `blame`, `blame_symbol`,
//! `symbol_history`.
//!
//! Split out of `helpers_git.rs`, which owns the dispatcher and the commit-log family, purely to
//! keep both files under the 1000-line cap. Nothing here is reachable except through
//! [`super::helpers_git::run_git`].

use rmcp::ErrorData as McpError;
use rmcp::model::CallToolResult;

use super::ServerState;
use super::helpers::{
    HashMode, LOG_WALK_MAX, blame_symbol_too_large_response, blame_too_large_response, elapsed_us,
    git_history_if_fresh, head_sha, head_snapshot_id, json_result, kind_to_str, outline_entry_for_blob,
    paginate_blame_hunks, parse_hash_mode, parse_kind, require_git_repo, symbol_fingerprint, symbol_line_range,
};
use super::helpers_git::reject_external_path;
use super::types::{
    BlameFileParams, BlameResponse, BlameSymbolParams, BlameSymbolResponse, DiffFileParams, DiffFileResponse,
    DiffOutlineParams, DiffOutlineResponse, DiffSymbolView, HunkView, SymbolHistoryEntry, SymbolHistoryParams,
    SymbolHistoryResponse,
};
use crate::git::CommitInfo;

/// Body for `git` mode `diff`: content hunks for one path between two revisions.
pub(super) fn run_diff(state: &ServerState, params: DiffFileParams) -> Result<CallToolResult, McpError> {
    let started = std::time::Instant::now();
    let repo = require_git_repo(state)?;
    let old_sha = repo
        .resolve_rev(&params.rev_old)
        .map_err(|e| McpError::invalid_params(format!("resolve_rev({}): {e}", params.rev_old), None))?;
    let new_sha = repo
        .resolve_rev(&params.rev_new)
        .map_err(|e| McpError::invalid_params(format!("resolve_rev({}): {e}", params.rev_new), None))?;
    let result = repo
        .diff_file(&old_sha, &new_sha, &params.path)
        .map_err(|e| McpError::internal_error(format!("diff: {e}"), None))?;
    let (hunks, present_old, present_new) = result.unwrap_or((Vec::new(), false, false));
    let hunks = hunks
        .into_iter()
        .map(|h| HunkView {
            kind: h.kind.as_str(),
            old_line_start: h.old_line_start,
            old_line_count: h.old_line_count,
            new_line_start: h.new_line_start,
            new_line_count: h.new_line_count,
            text: h.text,
        })
        .collect();
    json_result(&DiffFileResponse {
        path: params.path,
        rev_old: old_sha,
        rev_new: new_sha,
        present_at_old: present_old,
        present_at_new: present_new,
        hunks,
        elapsed_us: elapsed_us(started),
    })
}

/// Body for `git` mode `diff_outline`: which symbols the served view has that `rev` does not, and
/// vice versa — "what did this branch add" without reading source.
pub(super) async fn run_diff_outline(
    state: &ServerState,
    params: DiffOutlineParams,
) -> Result<CallToolResult, McpError> {
    let started = std::time::Instant::now();
    let repo = require_git_repo(state)?;
    let rev_spec = params.rev.as_deref().unwrap_or("HEAD");
    let rev_sha = repo
        .resolve_rev(rev_spec)
        .map_err(|e| McpError::invalid_params(format!("resolve_rev({rev_spec}): {e}"), None))?;

    state.await_cache_ready().await;
    let cache = state.shared.cache.load_full();
    let here = cache.by_path.get(&params.path).map(|l1| {
        l1.symbols
            .iter()
            .map(|s| (s.name.clone(), kind_to_str(s.kind)))
            .collect::<Vec<(String, &'static str)>>()
    });

    let rev_blob = repo
        .read_blob_at_rev(&rev_sha, &params.path)
        .map_err(|e| McpError::internal_error(format!("read blob {rev_sha}:{}: {e}", params.path), None))?;

    let there: Option<Vec<(String, &'static str)>> = match rev_blob {
        Some(bytes) => {
            let lang = crate::lang::detect(std::path::Path::new(&params.path))
                .ok_or_else(|| McpError::invalid_params(format!("unsupported language for {}", params.path), None))?;
            let l1 = crate::extract::l1::extract_l1(lang, &bytes)
                .map_err(|e| McpError::internal_error(format!("extract {rev_sha}:{}: {e}", params.path), None))?;
            Some(l1.symbols.into_iter().map(|s| (s.name, kind_to_str(s.kind))).collect())
        }
        None => None,
    };

    let (added, removed, common, note) = match (here, there) {
        (Some(h), Some(t)) => {
            let hs: ahash::AHashSet<(String, &'static str)> = h.iter().cloned().collect();
            let ts: ahash::AHashSet<(String, &'static str)> = t.iter().cloned().collect();
            let added = h
                .iter()
                .filter(|p| !ts.contains(*p))
                .cloned()
                .map(|(n, k)| DiffSymbolView {
                    name: n,
                    kind: k.to_string(),
                })
                .collect();
            let removed = t
                .iter()
                .filter(|p| !hs.contains(*p))
                .cloned()
                .map(|(n, k)| DiffSymbolView {
                    name: n,
                    kind: k.to_string(),
                })
                .collect();
            let common = h
                .iter()
                .filter(|p| ts.contains(*p))
                .cloned()
                .map(|(n, k)| DiffSymbolView {
                    name: n,
                    kind: k.to_string(),
                })
                .collect();
            (added, removed, common, None)
        }
        (Some(h), None) => (
            h.into_iter()
                .map(|(n, k)| DiffSymbolView {
                    name: n,
                    kind: k.to_string(),
                })
                .collect(),
            Vec::new(),
            Vec::new(),
            Some(format!("path absent at {rev_spec}; entire file treated as added")),
        ),
        (None, Some(t)) => (
            Vec::new(),
            t.into_iter()
                .map(|(n, k)| DiffSymbolView {
                    name: n,
                    kind: k.to_string(),
                })
                .collect(),
            Vec::new(),
            Some("path not indexed in the current view; entire file treated as removed".to_string()),
        ),
        (None, None) => {
            return Err(McpError::invalid_params(
                format!("path not present in current view or at {rev_spec}: {}", params.path),
                None,
            ));
        }
    };

    json_result(&DiffOutlineResponse {
        path: params.path,
        rev: rev_sha,
        added,
        removed,
        common,
        note,
        elapsed_us: elapsed_us(started),
    })
}

/// Body for `git` mode `blame`: per-line blame at `rev`, optionally clamped to a 1-based inclusive
/// line range.
pub(super) fn run_blame(state: &ServerState, params: BlameFileParams) -> Result<CallToolResult, McpError> {
    let started = std::time::Instant::now();
    let repo = require_git_repo(state)?;
    reject_external_path(&params.path)?;
    let suspect_sha = match params.rev.as_deref() {
        Some(r) => repo
            .resolve_rev(r)
            .map_err(|e| McpError::invalid_params(format!("resolve_rev({r}): {e}"), None))?,
        None => head_sha(repo)?,
    };
    let range = match (params.line_start, params.line_end) {
        (Some(lo), Some(hi)) => Some((lo, hi)),
        (None, None) => None,
        _ => {
            return Err(McpError::invalid_params(
                "line_start and line_end must be provided together",
                None,
            ));
        }
    };
    let resume_after: u32 = match params.cursor.as_ref() {
        Some(c) => c.decode_in_memory()?.0.min(u32::MAX as u64) as u32,
        None => 0,
    };
    let result = match state.shared.git_cache.blame(repo, &suspect_sha, &params.path, range) {
        Ok(r) => r,
        Err(e) => {
            if let Some(too_large) = blame_too_large_response(&params.path, &suspect_sha, &e, started) {
                return json_result(&too_large);
            }
            return Err(McpError::internal_error(format!("blame: {e}"), None));
        }
    };
    let (hunks, next_cursor) = paginate_blame_hunks(result.hunks.iter(), resume_after, params.limit);
    let truncated_reason: Option<&'static str> = match result.truncated_reason.as_deref() {
        Some("shallow_clone") => Some("shallow_clone"),
        Some(_) => Some("truncated"),
        None if repo.is_shallow() => Some("shallow_clone"),
        None => None,
    };
    json_result(&BlameResponse {
        path: result.path.clone(),
        suspect_sha: result.suspect_sha.clone(),
        hunks,
        truncated: truncated_reason.is_some(),
        truncated_reason,
        next_cursor,
        elapsed_us: elapsed_us(started),
    })
}

/// Body for `git` mode `blame_symbol`: blame clamped to one symbol's line span, resolved through
/// the cached L1 outline of the served view.
pub(super) async fn run_blame_symbol(
    state: &ServerState,
    params: BlameSymbolParams,
) -> Result<CallToolResult, McpError> {
    let started = std::time::Instant::now();
    let repo = require_git_repo(state)?;
    reject_external_path(&params.path)?;
    let kind = params.kind.as_deref().map(parse_kind).transpose()?;
    state.await_cache_ready().await;
    let cache = state.shared.cache.load_full();
    let l1 = cache
        .by_path
        .get(&params.path)
        .ok_or_else(|| McpError::invalid_params(format!("file not indexed in current view: {}", params.path), None))?;
    let sym = l1
        .symbols
        .iter()
        .find(|s| s.name == params.name && kind.is_none_or(|k| s.kind == k))
        .ok_or_else(|| {
            McpError::invalid_params(
                format!(
                    "symbol `{}`{} not found in {}",
                    params.name,
                    kind.map(|k| format!(" (kind={})", kind_to_str(k))).unwrap_or_default(),
                    params.path
                ),
                None,
            )
        })?;
    let (line_start, line_end) = symbol_line_range(repo, &params.path, sym);
    let suspect_sha = match params.rev.as_deref() {
        Some(r) => repo
            .resolve_rev(r)
            .map_err(|e| McpError::invalid_params(format!("resolve_rev({r}): {e}"), None))?,
        None => head_sha(repo)?,
    };
    let resume_after: u32 = match params.cursor.as_ref() {
        Some(c) => c.decode_in_memory()?.0.min(u32::MAX as u64) as u32,
        None => 0,
    };
    let result = match state
        .shared
        .git_cache
        .blame(repo, &suspect_sha, &params.path, Some((line_start, line_end)))
    {
        Ok(r) => r,
        Err(e) => {
            if let Some(too_large) =
                blame_symbol_too_large_response(&params.path, &suspect_sha, sym, line_start, line_end, &e, started)
            {
                return json_result(&too_large);
            }
            return Err(McpError::internal_error(format!("blame: {e}"), None));
        }
    };
    let (hunks, next_cursor) = paginate_blame_hunks(result.hunks.iter(), resume_after, params.limit);
    let truncated_reason: Option<&'static str> = match result.truncated_reason.as_deref() {
        Some("shallow_clone") => Some("shallow_clone"),
        Some(_) => Some("truncated"),
        None if repo.is_shallow() => Some("shallow_clone"),
        None => None,
    };
    json_result(&BlameSymbolResponse {
        path: result.path.clone(),
        suspect_sha: result.suspect_sha.clone(),
        name: sym.name.clone(),
        kind: kind_to_str(sym.kind).to_string(),
        line_start,
        line_end,
        hunks,
        truncated: truncated_reason.is_some(),
        truncated_reason,
        next_cursor,
        elapsed_us: elapsed_us(started),
    })
}

/// Body for `git` mode `symbol_history`: tree-sitter × git — the commits where a named symbol's
/// body fingerprint actually changed (or where it was introduced / removed).
pub(super) fn run_symbol_history(state: &ServerState, params: SymbolHistoryParams) -> Result<CallToolResult, McpError> {
    let started = std::time::Instant::now();
    let repo = require_git_repo(state)?;
    let kind = params.kind.as_deref().map(parse_kind).transpose()?;
    let limit = params.limit.unwrap_or(20).min(100) as usize;
    let lang = crate::lang::detect(std::path::Path::new(&params.path))
        .ok_or_else(|| McpError::invalid_params(format!("unsupported language: {}", params.path), None))?;
    let hash_mode = match params.hash_mode.as_deref() {
        Some(s) => parse_hash_mode(s)?,
        None => HashMode::Normalized,
    };

    let head = head_sha(repo)?;
    let snapshot = head_snapshot_id(&head);

    let skip = match params.cursor.as_ref() {
        Some(c) => {
            let (offset, snapshot_id) = c.decode_in_memory()?;
            if snapshot_id != snapshot {
                return json_result(&SymbolHistoryResponse {
                    path: params.path,
                    name: params.name,
                    kind: kind.map(|k| kind_to_str(k).to_string()),
                    commits_inspected: 0,
                    history: Vec::new(),
                    hash_mode: hash_mode.as_str(),
                    truncated: false,
                    truncated_reason: None,
                    next_cursor: None,
                    cursor_invalidated: true,
                    elapsed_us: elapsed_us(started),
                });
            }
            offset as usize
        }
        None => 0,
    };

    let walk_depth = (skip.saturating_add(limit).saturating_add(1).saturating_mul(4)).min(LOG_WALK_MAX) as u32;
    let commits: Vec<CommitInfo> = match git_history_if_fresh(state, &head) {
        Some(index) => index.commits_touching(&params.path, 0, walk_depth as usize),
        None => state
            .shared
            .git_cache
            .log(repo, &head, Some(&params.path), walk_depth, false)
            .map_err(|e| McpError::internal_error(format!("log: {e}"), None))?
            .as_ref()
            .clone(),
    };

    let chronological: Vec<CommitInfo> = commits.iter().cloned().rev().collect();

    let mut history = Vec::new();
    let mut prev_fp: Option<Vec<u8>> = None;
    let mut prev_existed = false;
    let mut inspected: u32 = 0;
    for c in chronological {
        inspected += 1;
        let blob = repo
            .read_blob_at_rev_with_oid(&c.sha, &params.path)
            .map_err(|e| McpError::internal_error(format!("blob: {e}"), None))?;
        let fingerprint = match blob {
            Some((bytes, oid)) => outline_entry_for_blob(&state.shared.outline_cache, oid, lang, bytes)
                .and_then(|entry| symbol_fingerprint(&entry, &params.name, kind, lang, hash_mode)),
            None => None,
        };
        let change = match (prev_existed, fingerprint.as_ref()) {
            (false, Some(_)) => Some("introduced"),
            (true, None) => Some("removed"),
            (true, Some(curr)) => {
                if prev_fp.as_deref() != Some(curr.as_slice()) {
                    Some("modified")
                } else {
                    None
                }
            }
            (false, None) => None,
        };
        if let Some(kind_str) = change {
            history.push(SymbolHistoryEntry {
                sha: c.sha.clone(),
                short_sha: c.short_sha.clone(),
                summary: c.summary.clone(),
                author: c.author.clone(),
                author_time_unix: c.author_time_unix,
                change: kind_str,
            });
        }
        prev_existed = fingerprint.is_some();
        prev_fp = fingerprint;
    }
    history.reverse();

    let total_history = history.len();
    let page: Vec<SymbolHistoryEntry> = history.into_iter().skip(skip).take(limit).collect();
    let has_more = total_history > skip + page.len();
    let next_cursor = has_more.then(|| super::cursor::Cursor::encode_in_memory((skip + page.len()) as u64, snapshot));

    let truncated = repo.is_shallow();
    json_result(&SymbolHistoryResponse {
        path: params.path,
        name: params.name,
        kind: kind.map(|k| kind_to_str(k).to_string()),
        commits_inspected: inspected,
        history: page,
        hash_mode: hash_mode.as_str(),
        truncated,
        truncated_reason: truncated.then_some("shallow_clone"),
        next_cursor,
        cursor_invalidated: false,
        elapsed_us: elapsed_us(started),
    })
}