pathfinder-mcp 0.6.1

Pathfinder — The Headless IDE MCP Server for AI Coding Agents
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
use super::text_edit::resolve_text_edit;
use super::{FinalizeEditParams, ResolvedEdit};
use crate::server::helpers::{
    check_occ, check_sandbox_access, io_error_data, pathfinder_to_error_data,
};
use crate::server::types::EditResponse;
use pathfinder_common::error::PathfinderError;
use pathfinder_common::indent::dedent_then_reindent;
use pathfinder_common::normalize::{normalize_for_body_replace, normalize_for_full_replace};
use pathfinder_common::types::{SemanticPath, VersionHash};
use rmcp::handler::server::wrapper::Json;
use rmcp::model::ErrorData;
use std::path::{Path, PathBuf};
use tracing::instrument;

impl crate::server::PathfinderServer {
    /// Validate OCC and read file content for batch edits.
    pub(crate) async fn validate_batch_occ(
        &self,
        absolute_path: &Path,
        base_version: &str,
        filepath_str: &str,
    ) -> Result<(Vec<u8>, VersionHash), ErrorData> {
        let source = tokio::fs::read(absolute_path)
            .await
            .map_err(|e| io_error_data(format!("failed to read file: {e}")))?;
        let current_hash = VersionHash::compute(&source);

        check_occ(base_version, &current_hash, PathBuf::from(filepath_str))?;

        Ok((source, current_hash))
    }

    /// Resolve a single edit from a batch into a concrete byte range.
    pub(crate) async fn resolve_single_batch_edit(
        &self,
        edit: &crate::server::types::BatchEdit,
        edit_index: usize,
        source: &[u8],
        file_path: &Path,
    ) -> Result<ResolvedEdit, ErrorData> {
        // ── Branch A: Text-range targeting ─────────────────────────────────────
        if let Some(ref old_text) = edit.old_text {
            let Some(context_line) = edit.context_line else {
                let err = PathfinderError::InvalidTarget {
                    semantic_path: format!("edit[{edit_index}]"),
                    reason: "`context_line` is required when `old_text` is set".to_owned(),
                    edit_index: Some(edit_index),
                    valid_edit_types: None,
                };
                return Err(pathfinder_to_error_data(&err));
            };
            let replacement = edit.replacement_text.as_deref().unwrap_or("");
            let free = resolve_text_edit(
                source,
                old_text.as_str(),
                context_line,
                replacement,
                edit.normalize_whitespace,
                file_path,
            )
            .map_err(|e| pathfinder_to_error_data(&e))?;
            return Ok(ResolvedEdit {
                start_byte: free.start_byte,
                end_byte: free.end_byte,
                replacement: free.replacement,
            });
        }

        // ── Branch B: Semantic targeting ───────────────────────────────────────
        let Some(semantic_path) = SemanticPath::parse(&edit.semantic_path) else {
            let err = PathfinderError::InvalidSemanticPath {
                input: edit.semantic_path.clone(),
                issue: "Semantic path is malformed or missing '::' separator.".to_owned(),
            };
            return Err(pathfinder_to_error_data(&err));
        };

        self.resolve_semantic_batch_edit(&semantic_path, edit, edit_index, source)
            .await
    }

    /// Batch resolver for `replace_body` edits.
    pub(crate) async fn resolve_batch_replace_body(
        &self,
        semantic_path: &SemanticPath,
        new_code: &str,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        let (body_range, ..) = self
            .surgeon
            .resolve_body_range(self.workspace_root.path(), semantic_path)
            .await
            .map_err(crate::server::helpers::treesitter_error_to_error_data)?;

        let normalized = normalize_for_body_replace(new_code);
        let indented = dedent_then_reindent(&normalized, body_range.body_indent_column);

        let is_brace_block = if body_range.end_byte > body_range.start_byte {
            source.get(body_range.start_byte) == Some(&b'{')
                && source.get(body_range.end_byte.saturating_sub(1)) == Some(&b'}')
        } else {
            false
        };

        if is_brace_block {
            let inner_start = body_range.start_byte + 1;
            let inner_end = body_range.end_byte.saturating_sub(1);
            let replacement = if indented.trim().is_empty() {
                Vec::new()
            } else {
                let closing_indent = " ".repeat(body_range.indent_column);
                format!("\n{indented}\n{closing_indent}").into_bytes()
            };
            Ok(ResolvedEdit {
                start_byte: inner_start,
                end_byte: inner_end,
                replacement,
            })
        } else {
            let mut end = body_range.start_byte;
            while end > 0 && (source[end - 1] == b' ' || source[end - 1] == b'\t') {
                end -= 1;
            }
            Ok(ResolvedEdit {
                start_byte: end,
                end_byte: body_range.end_byte,
                replacement: format!("\n{indented}").into_bytes(),
            })
        }
    }

    /// Batch resolver for `replace_full` edits.
    pub(crate) async fn resolve_batch_replace_full(
        &self,
        semantic_path: &SemanticPath,
        new_code: &str,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        if semantic_path.is_bare_file() {
            return Ok(ResolvedEdit {
                start_byte: 0,
                end_byte: source.len(),
                replacement: new_code.as_bytes().to_vec(),
            });
        }

        let (full_range, ..) = self
            .surgeon
            .resolve_full_range(self.workspace_root.path(), semantic_path)
            .await
            .map_err(crate::server::helpers::treesitter_error_to_error_data)?;

        let normalized = normalize_for_full_replace(new_code);
        let indented = dedent_then_reindent(&normalized, full_range.indent_column);

        Ok(ResolvedEdit {
            start_byte: full_range.start_byte,
            end_byte: full_range.end_byte,
            replacement: indented.into_bytes(),
        })
    }

    /// Batch resolver for `insert_before` edits.
    pub(crate) async fn resolve_batch_insert_before(
        &self,
        semantic_path: &SemanticPath,
        new_code: &str,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        let (insert_byte, indent_column) = if semantic_path.is_bare_file() {
            (0, 0)
        } else {
            let (symbol_range, ..) = self
                .surgeon
                .resolve_symbol_range(self.workspace_root.path(), semantic_path)
                .await
                .map_err(crate::server::helpers::treesitter_error_to_error_data)?;
            (symbol_range.start_byte, symbol_range.indent_column)
        };

        let normalized = normalize_for_full_replace(new_code);
        let indented = dedent_then_reindent(&normalized, indent_column);

        let trailing = if indented.ends_with('\n') { "" } else { "\n" };
        let after = &source[insert_byte..];
        let sep = if after.starts_with(b"\n\n") {
            ""
        } else if after.starts_with(b"\n") {
            "\n"
        } else {
            "\n\n"
        };

        Ok(ResolvedEdit {
            start_byte: insert_byte,
            end_byte: insert_byte,
            replacement: format!("{indented}{trailing}{sep}").into_bytes(),
        })
    }

    /// Batch resolver for `insert_after` edits.
    pub(crate) async fn resolve_batch_insert_after(
        &self,
        semantic_path: &SemanticPath,
        new_code: &str,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        let (insert_byte, indent_column) = if semantic_path.is_bare_file() {
            (source.len(), 0)
        } else {
            let (symbol_range, ..) = self
                .surgeon
                .resolve_symbol_range(self.workspace_root.path(), semantic_path)
                .await
                .map_err(crate::server::helpers::treesitter_error_to_error_data)?;
            (symbol_range.end_byte, symbol_range.indent_column)
        };

        let normalized = normalize_for_full_replace(new_code);
        let indented = dedent_then_reindent(&normalized, indent_column);

        let before = &source[..insert_byte];
        let before_sep = if before.ends_with(b"\n\n") {
            ""
        } else if before.ends_with(b"\n") {
            "\n"
        } else {
            "\n\n"
        };
        let after_sep = if indented.ends_with('\n') { "" } else { "\n" };

        Ok(ResolvedEdit {
            start_byte: insert_byte,
            end_byte: insert_byte,
            replacement: format!("{before_sep}{indented}{after_sep}").into_bytes(),
        })
    }

    /// Batch resolver for `delete` edits.
    pub(crate) async fn resolve_batch_delete(
        &self,
        semantic_path: &SemanticPath,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        let (full_range, ..) = self
            .surgeon
            .resolve_full_range(self.workspace_root.path(), semantic_path)
            .await
            .map_err(crate::server::helpers::treesitter_error_to_error_data)?;

        let mut b_end = full_range.start_byte;
        while b_end > 0 && source[b_end - 1].is_ascii_whitespace() {
            b_end -= 1;
        }

        let mut a_start = full_range.end_byte;
        while a_start < source.len() && source[a_start].is_ascii_whitespace() {
            a_start += 1;
        }

        let sep = if b_end == 0 || a_start == source.len() {
            b"\n" as &[u8]
        } else {
            b"\n\n"
        };

        Ok(ResolvedEdit {
            start_byte: b_end,
            end_byte: a_start,
            replacement: sep.to_vec(),
        })
    }

    /// Dispatch a semantic batch edit to the per-type resolver.
    pub(crate) async fn resolve_semantic_batch_edit(
        &self,
        semantic_path: &SemanticPath,
        edit: &crate::server::types::BatchEdit,
        edit_index: usize,
        source: &[u8],
    ) -> Result<ResolvedEdit, ErrorData> {
        let new_code = edit.new_code.as_deref().unwrap_or_default();
        match edit.edit_type.as_str() {
            "replace_body" => {
                self.resolve_batch_replace_body(semantic_path, new_code, source)
                    .await
            }
            "replace_full" => {
                self.resolve_batch_replace_full(semantic_path, new_code, source)
                    .await
            }
            "insert_before" => {
                self.resolve_batch_insert_before(semantic_path, new_code, source)
                    .await
            }
            "insert_after" => {
                self.resolve_batch_insert_after(semantic_path, new_code, source)
                    .await
            }
            "delete" => self.resolve_batch_delete(semantic_path, source).await,
            _unknown => {
                let err = PathfinderError::InvalidTarget {
                    semantic_path: edit.semantic_path.clone(),
                    reason: format!(
                        "edit_type is required for semantic targeting. Got: '{}' (empty).",
                        edit.edit_type
                    ),
                    edit_index: Some(edit_index),
                    valid_edit_types: Some(vec![
                        "replace_body".to_string(),
                        "replace_full".to_string(),
                        "insert_before".to_string(),
                        "insert_after".to_string(),
                        "delete".to_string(),
                    ]),
                };
                Err(pathfinder_to_error_data(&err))
            }
        }
    }

    /// Apply resolved edits to source content, sorted backwards to prevent offset shifts.
    pub(crate) fn apply_sorted_edits(
        source: &[u8],
        mut resolved_edits: Vec<(usize, String, ResolvedEdit)>,
    ) -> Result<Vec<u8>, ErrorData> {
        // Sort edits backwards to prevent shifted byte offsets
        resolved_edits.sort_by_key(|(_, _, e)| std::cmp::Reverse(e.start_byte));

        // Ensure no overlapping edits
        for i in 1..resolved_edits.len() {
            let (prev_idx, _, prev) = &resolved_edits[i - 1]; // This is later in the file
            let (curr_idx, curr_path, curr) = &resolved_edits[i]; // This is earlier in the file
            if curr.end_byte > prev.start_byte {
                let err = PathfinderError::InvalidTarget {
                    semantic_path: curr_path.clone(),
                    reason: format!(
                        "overlapping edits in replace_batch: edit {curr_idx} overlaps with edit {prev_idx}"
                    ),
                    edit_index: Some(*curr_idx),
                    valid_edit_types: None,
                };
                return Err(pathfinder_to_error_data(&err));
            }
        }

        let mut new_bytes = source.to_vec();
        for (_, _, edit) in resolved_edits {
            new_bytes.splice(edit.start_byte..edit.end_byte, edit.replacement);
        }

        Ok(new_bytes)
    }

    /// Core logic for the `replace_batch` tool (PRD Epic 5).
    ///
    /// Executes multiple edits on the same file atomically. Edits are resolved,
    /// sorted backwards by byte offset, and spliced together. This avoids OCC
    /// mismatches from chains of edits.
    #[instrument(skip(self, params), fields(filepath = %params.filepath))]
    pub(crate) async fn replace_batch_impl(
        &self,
        params: crate::server::types::ReplaceBatchParams,
    ) -> Result<Json<EditResponse>, ErrorData> {
        let start = std::time::Instant::now();
        tracing::info!(
            tool = "replace_batch",
            filepath = %params.filepath,
            "replace_batch: start"
        );

        let file_path = Path::new(&params.filepath);
        check_sandbox_access(&self.sandbox, file_path, "replace_batch", &params.filepath)?;

        let absolute_path = self.workspace_root.resolve(file_path);
        let (source, current_hash) = self
            .validate_batch_occ(&absolute_path, &params.base_version, &params.filepath)
            .await?;

        let mut resolved_edits = Vec::new();
        for (edit_index, edit) in params.edits.iter().enumerate() {
            let resolved = self
                .resolve_single_batch_edit(edit, edit_index, &source, file_path)
                .await?;
            let path_or_text = if !edit.semantic_path.is_empty() {
                edit.semantic_path.clone()
            } else if let Some(old_text) = &edit.old_text {
                format!("text match: '{old_text}'")
            } else {
                "unknown".to_string()
            };
            resolved_edits.push((edit_index, path_or_text, resolved));
        }

        let new_bytes = Self::apply_sorted_edits(&source, resolved_edits)?;
        let resolve_ms = start.elapsed().as_millis();

        // C1: Log when SemanticPath::parse fails and falls back to bare file
        let semantic_path = if let Some(p) = SemanticPath::parse(&params.filepath) {
            p
        } else {
            tracing::warn!(
                filepath = %params.filepath,
                "replace_batch: SemanticPath::parse failed, treating as bare file"
            );
            SemanticPath {
                file_path: file_path.to_path_buf(),
                symbol_chain: None,
            }
        };

        self.finalize_edit(FinalizeEditParams {
            tool_name: "replace_batch",
            semantic_path: &semantic_path,
            raw_semantic_path_str: &params.filepath,
            source: &source,
            original_hash: &current_hash,
            new_content: new_bytes,
            ignore_validation_failures: params.ignore_validation_failures,
            start_time: start,
            resolve_ms,
            warning: None,
        })
        .await
    }
}