symforge 11.7.1

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
//! Native `what_changed` and `diff_symbols` over one captured publication.
//!
//! Mode selection, filters, envelopes, the symbol-delta engine and every
//! repository read come from the same shared modules the MCP handlers call;
//! git objects and working-tree bytes pass the shared read gate.

use std::path::Path;

use super::embed_query::{Budget, EmbeddedQuerySnapshot, validate_path};
use super::guidance::changes::{self as shared, WhatChangedMode, WhatChangedOptions};
use super::guidance::{read_gate, search_envelope};
use crate::embed::parity::changes::{
    ChangeMode, DiffSymbolsRequest, DiffSymbolsResult, SymbolFileDelta, WhatChangedRequest,
    WhatChangedResult,
};
use crate::embed::parity::{QueryOutput, QueryRefusalKind};
use crate::live_index::LiveIndex;

/// Refuse a project selector that does not name this source's root. An embed
/// handle serves exactly one source, so any other selector is foreign.
pub(super) fn check_project(
    snapshot: &EmbeddedQuerySnapshot,
    project: Option<&str>,
) -> Result<(), QueryRefusalKind> {
    if project.is_some_and(|selected| {
        !snapshot
            .root
            .to_str()
            .is_some_and(|root| selected == root || selected == root.replace('\\', "/"))
    }) {
        return Err(QueryRefusalKind::AdmissionUnavailable);
    }
    Ok(())
}

/// The repository whose work tree IS this source root.
pub(super) fn open_repository(root: &Path) -> Option<crate::git::GitRepo> {
    crate::git::GitRepo::open_worktree_root(root)
}

/// Gated working-tree (`reference` empty) or git-object text, the same pair
/// of reads `diff_symbols_result_view` performs for MCP.
pub(super) fn gated_text(
    live: &LiveIndex,
    repo: &crate::git::GitRepo,
    reference: &str,
    path: &str,
) -> Result<Option<String>, String> {
    let mut ignore = |_| {};
    if reference.is_empty() {
        read_gate::worktree_text_with(repo, path, &mut |full_path| {
            read_gate::disk_read_with(live, path, full_path, &mut || None, &mut ignore)
        })
    } else {
        read_gate::admit_git_text_with(live, repo, reference, path, &mut ignore)
    }
}

fn validate_filters(
    path_prefix: Option<&str>,
    language: Option<&str>,
    max_tokens: Option<u64>,
) -> Result<(), QueryRefusalKind> {
    if max_tokens == Some(0) {
        return Err(QueryRefusalKind::InvalidRequest);
    }
    if let Some(prefix) = path_prefix {
        validate_path(prefix, true)?;
    }
    super::guidance::filters::parse_language_filter(language)
        .map_err(|_| QueryRefusalKind::UnsupportedOption)?;
    Ok(())
}

pub(super) fn validate_what_changed(request: &WhatChangedRequest) -> Result<(), QueryRefusalKind> {
    // MCP reads `max_tokens = 0` as "no cap", so it is not a malformed request here.
    validate_filters(
        request.path_prefix.as_deref(),
        request.language.as_deref(),
        None,
    )
}

pub(super) fn validate_diff_symbols(request: &DiffSymbolsRequest) -> Result<(), QueryRefusalKind> {
    if [&request.base, &request.target]
        .into_iter()
        .any(|reference| reference.as_deref().is_some_and(|r| r.trim().is_empty()))
    {
        return Err(QueryRefusalKind::InvalidRequest);
    }
    validate_filters(
        request.path_prefix.as_deref(),
        request.language.as_deref(),
        request.max_tokens,
    )
}

/// A token cap bounds what is returned, never what is retrievable: the full
/// projection is cached before the cap applies.
fn cache_before_token_cap(
    budget: &mut Budget,
    max_tokens: Option<u64>,
    project: impl Fn(&mut Budget) -> Result<QueryOutput, QueryRefusalKind>,
) -> Result<QueryOutput, QueryRefusalKind> {
    if max_tokens.is_some() {
        let mut full_budget = budget.projection_budget();
        budget.cache_output = Some(project(&mut full_budget)?);
        budget.cache_truncated = full_budget.truncated;
        budget.check()?;
        budget.cap_tokens(max_tokens);
    }
    project(budget)
}

fn strings(values: &[String], budget: &mut Budget) -> Vec<String> {
    let mut rows = Vec::new();
    for value in values {
        if !budget.row(value.len()) {
            break;
        }
        rows.push(value.clone());
    }
    rows
}

/// The uncapped report, rendered exactly as the MCP handler renders it.
struct WhatChangedReport {
    mode: ChangeMode,
    paths: Vec<String>,
    total_paths: u64,
    symbol_diff: Option<(shared::SymbolDiffView, String)>,
    rendered: String,
}

pub(super) fn what_changed(
    snapshot: &EmbeddedQuerySnapshot,
    request: &WhatChangedRequest,
    budget: &mut Budget,
) -> Result<QueryOutput, QueryRefusalKind> {
    check_project(snapshot, request.project.as_deref())?;
    let options = WhatChangedOptions {
        since: request.since,
        git_ref: request.git_ref.as_deref(),
        uncommitted: request.uncommitted,
        path_prefix: request.path_prefix.as_deref(),
        language: request.language.as_deref(),
        code_only: request.code_only,
        include_symbol_diff: request.include_symbol_diff,
    };
    let repo = open_repository(&snapshot.root);
    let mode = shared::determine_what_changed_mode(&options, repo.is_some())
        .map_err(|_| QueryRefusalKind::GitUnavailable)?;
    let change_mode = match &mode {
        WhatChangedMode::Timestamp(since) => ChangeMode::Timestamp { since: *since },
        WhatChangedMode::GitRef(reference) => ChangeMode::GitRef {
            reference: reference.clone(),
        },
        WhatChangedMode::Uncommitted => ChangeMode::Uncommitted,
    };
    if request.estimate == Some(true) {
        let (tokens, rendered) =
            shared::what_changed_estimate(request.include_symbol_diff.unwrap_or(false));
        let rendered = budget.text(&rendered)?;
        return Ok(QueryOutput::WhatChanged(WhatChangedResult {
            mode: change_mode,
            paths: Vec::new(),
            total_paths: 0,
            filtered_paths: 0,
            symbol_diff: None,
            rendered,
            estimated_tokens: Some(tokens),
        }));
    }
    // The MCP handler applies `max_tokens` (`enforce_token_budget`, which reads
    // 0 as no cap) to the uncommitted report only; timestamp and git-ref
    // reports are returned whole.
    let max_tokens = match mode {
        WhatChangedMode::Uncommitted => request.max_tokens.filter(|tokens| *tokens > 0),
        WhatChangedMode::Timestamp(_) | WhatChangedMode::GitRef(_) => None,
    };
    let report = what_changed_report(snapshot, &options, &mode, change_mode, repo.as_ref())?;
    budget.check()?;
    cache_before_token_cap(budget, max_tokens, |budget| {
        let rendered = budget.text(&report.rendered)?;
        let paths = strings(&report.paths, budget);
        let symbol_diff = match &report.symbol_diff {
            Some((view, rendered)) => Some(project_symbol_diff(view, rendered, None, budget)?),
            None => None,
        };
        Ok(QueryOutput::WhatChanged(WhatChangedResult {
            mode: report.mode.clone(),
            filtered_paths: report.paths.len() as u64,
            paths,
            total_paths: report.total_paths,
            symbol_diff,
            rendered,
            estimated_tokens: None,
        }))
    })
}

fn what_changed_report(
    snapshot: &EmbeddedQuerySnapshot,
    options: &WhatChangedOptions<'_>,
    mode: &WhatChangedMode,
    change_mode: ChangeMode,
    repo: Option<&crate::git::GitRepo>,
) -> Result<WhatChangedReport, QueryRefusalKind> {
    let live = snapshot.generation.live.as_ref();
    let filter = |paths: Vec<String>, code_only: bool| {
        shared::filter_paths_by_prefix_and_language(
            paths,
            options.path_prefix,
            options.language,
            code_only,
        )
        .map_err(|_| QueryRefusalKind::UnsupportedOption)
    };
    let envelope = |match_type: &str, parse_state: &str, total: usize, filtered: &[String]| {
        search_envelope::format_search_envelope(
            match_type,
            shared::what_changed_source_authority(mode, &snapshot.generation.freshness),
            parse_state,
            &shared::changed_paths_completeness_label(total, filtered.len()),
            &shared::what_changed_scope_summary(options, mode),
            &shared::search_paths_evidence(filtered.iter().map(String::as_str)),
        )
    };
    let message = |total_paths: usize, rendered: String| WhatChangedReport {
        mode: change_mode.clone(),
        paths: Vec::new(),
        total_paths: total_paths as u64,
        symbol_diff: None,
        rendered,
    };
    let include_symbol_diff = options.include_symbol_diff.unwrap_or(false);
    match mode {
        WhatChangedMode::Timestamp(since_ts) => {
            let view = live.capture_what_changed_timestamp_view();
            if *since_ts >= view.loaded_secs {
                return Ok(message(
                    view.paths.len(),
                    shared::what_changed_timestamp_view(&view, *since_ts),
                ));
            }
            let filtered = filter(view.paths.clone(), options.code_only.unwrap_or(false))?;
            if filtered.is_empty() {
                return Ok(message(
                    view.paths.len(),
                    if view.paths.is_empty() {
                        "Index is empty — no files tracked.".to_string()
                    } else {
                        "No indexed files matched the requested filters since the last index load."
                            .to_string()
                    },
                ));
            }
            let envelope = search_envelope::format_search_envelope(
                "exact (timestamp compare)",
                shared::what_changed_source_authority(mode, &snapshot.generation.freshness),
                super::guidance::reference_read::search_parse_state_for_paths(
                    live,
                    filtered.iter().map(String::as_str),
                ),
                &shared::changed_paths_completeness_label(view.paths.len(), filtered.len()),
                &shared::what_changed_scope_summary(options, mode),
                &shared::search_paths_evidence(filtered.iter().map(String::as_str)),
            );
            let output = shared::what_changed_paths_result(
                &filtered,
                "No indexed files matched the requested filters since the last index load.",
            );
            Ok(WhatChangedReport {
                mode: change_mode,
                total_paths: view.paths.len() as u64,
                paths: filtered,
                symbol_diff: None,
                rendered: format!("{envelope}\n\n{output}"),
            })
        }
        WhatChangedMode::Uncommitted | WhatChangedMode::GitRef(_) => {
            let repo = repo.ok_or(QueryRefusalKind::GitUnavailable)?;
            let (paths, code_only, match_type, empty, base, target) = match mode {
                WhatChangedMode::GitRef(git_ref) => (
                    repo.changed_paths_from_ref(git_ref),
                    options.code_only.unwrap_or(false),
                    "exact (git ref diff)",
                    format!("No changes detected relative to git ref '{git_ref}'."),
                    git_ref.as_str(),
                    "HEAD",
                ),
                _ => (
                    repo.uncommitted_paths(),
                    // US1 (018) FR-001/SC-001: uncommitted mode defaults to
                    // source-focused so untracked data files don't dominate.
                    options.code_only.unwrap_or(true),
                    "exact (uncommitted working tree)",
                    "No uncommitted changes detected.".to_string(),
                    "HEAD",
                    "",
                ),
            };
            let paths = paths.map_err(|_| QueryRefusalKind::GitUnavailable)?;
            let total_paths = paths.len();
            let filtered = filter(paths, code_only)?;
            if filtered.is_empty() {
                let rendered = match mode {
                    _ if total_paths == 0 => empty,
                    WhatChangedMode::GitRef(git_ref) => format!(
                        "No changes relative to git ref '{git_ref}' matched the requested filters."
                    ),
                    _ => {
                        // An empty filtered result must disclose the (default)
                        // source-focus filter and its recovery lever.
                        let default_note = if options.code_only.is_none() {
                            " Uncommitted mode is source-focused by default (code_only=true);"
                        } else {
                            ""
                        };
                        format!(
                            "No uncommitted changes matched the requested filters.{default_note} \
                             {total_paths} changed path(s) were filtered out — pass \
                             code_only=false (or widen path_prefix/language) to see them."
                        )
                    }
                };
                return Ok(message(total_paths, rendered));
            }
            let envelope = envelope(
                match_type,
                shared::what_changed_parse_state_label(mode, include_symbol_diff),
                total_paths,
                &filtered,
            );
            let mut output = shared::what_changed_paths_result(&filtered, &empty);
            let symbol_diff = include_symbol_diff.then(|| {
                let refs: Vec<&str> = filtered.iter().map(String::as_str).collect();
                let view = shared::capture_symbol_diff(base, target, &refs, |reference, path| {
                    gated_text(live, repo, reference, path)
                });
                let rendered = shared::render_symbol_diff(&view, true, false);
                output.push_str("\n\n");
                output.push_str(&rendered);
                (view, rendered)
            });
            Ok(WhatChangedReport {
                mode: change_mode,
                total_paths: total_paths as u64,
                paths: filtered,
                symbol_diff,
                rendered: format!("{envelope}\n\n{output}"),
            })
        }
    }
}

/// Project a shared symbol delta into its bounded public form.
fn project_symbol_diff(
    view: &shared::SymbolDiffView,
    rendered: &str,
    commits: Option<(Option<String>, Option<String>, u64)>,
    budget: &mut Budget,
) -> Result<DiffSymbolsResult, QueryRefusalKind> {
    let rendered = budget.text(rendered)?;
    let mut files = Vec::new();
    for file in &view.files {
        let size = file.path.len()
            + file.withheld.as_ref().map_or(0, String::len)
            + [&file.added, &file.removed, &file.modified]
                .into_iter()
                .flatten()
                .map(String::len)
                .sum::<usize>();
        if !budget.row(size) {
            break;
        }
        files.push(SymbolFileDelta {
            path: file.path.clone(),
            added: file.added.clone(),
            removed: file.removed.clone(),
            modified: file.modified.clone(),
            withheld: file.withheld.clone(),
        });
    }
    let (base_commit, target_commit, total) =
        commits.unwrap_or((None, None, view.changed_files as u64));
    Ok(DiffSymbolsResult {
        base: view.base.clone(),
        target: view.target.clone(),
        base_commit,
        target_commit,
        total_changed_files: total,
        filtered_changed_files: view.changed_files as u64,
        files,
        added_count: view.added_count as u64,
        removed_count: view.removed_count as u64,
        modified_count: view.modified_count as u64,
        withheld_count: view.withheld_count as u64,
        rendered,
        estimated_tokens: None,
    })
}

pub(super) fn diff_symbols(
    snapshot: &EmbeddedQuerySnapshot,
    request: &DiffSymbolsRequest,
    budget: &mut Budget,
) -> Result<QueryOutput, QueryRefusalKind> {
    check_project(snapshot, request.project.as_deref())?;
    let base = request.base.as_deref().unwrap_or("main");
    let target = request.target.as_deref().unwrap_or("HEAD");
    let compact = request.compact.unwrap_or(false);
    let summary_only = request.summary_only.unwrap_or(false);
    if request.estimate == Some(true) {
        let (tokens, rendered) = shared::diff_symbols_estimate(compact, summary_only);
        let rendered = budget.text(&rendered)?;
        return Ok(QueryOutput::DiffSymbols(DiffSymbolsResult {
            base: base.to_owned(),
            target: target.to_owned(),
            base_commit: None,
            target_commit: None,
            total_changed_files: 0,
            filtered_changed_files: 0,
            files: Vec::new(),
            added_count: 0,
            removed_count: 0,
            modified_count: 0,
            withheld_count: 0,
            rendered,
            estimated_tokens: Some(tokens),
        }));
    }
    let live = snapshot.generation.live.as_ref();
    let repo = open_repository(&snapshot.root).ok_or(QueryRefusalKind::GitUnavailable)?;
    let changed = repo
        .changed_paths_between_refs(base, target)
        .map_err(|_| QueryRefusalKind::GitUnavailable)?;
    let code_only = request.code_only.unwrap_or(false);
    let filtered = shared::filter_diff_symbol_paths(
        &changed,
        request.path_prefix.as_deref(),
        request.language.as_deref(),
        code_only,
    )
    .map_err(|_| QueryRefusalKind::UnsupportedOption)?;
    let commits = Some((
        repo.resolve_ref_commit(base).map(|oid| oid.to_string()),
        repo.resolve_ref_commit(target).map(|oid| oid.to_string()),
        changed.len() as u64,
    ));
    let (view, rendered) = if filtered.is_empty() {
        (
            shared::capture_symbol_diff(base, target, &[], |_, _| Ok(None)),
            format!("No file changes found between {base} and {target}."),
        )
    } else {
        let view = shared::capture_symbol_diff(base, target, &filtered, |reference, path| {
            gated_text(live, &repo, reference, path)
        });
        let envelope = shared::diff_symbols_envelope(
            base,
            target,
            changed.len(),
            &filtered,
            compact,
            summary_only,
            request.path_prefix.as_deref(),
            request.language.as_deref(),
            code_only,
        );
        let body = shared::render_symbol_diff(&view, compact, summary_only);
        (view, format!("{envelope}\n\n{body}"))
    };
    budget.check()?;
    cache_before_token_cap(budget, request.max_tokens, |budget| {
        project_symbol_diff(&view, &rendered, commits.clone(), budget).map(QueryOutput::DiffSymbols)
    })
}