patchloom 0.30.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
//! Aider-style SEARCH/REPLACE and DiffFenced parse.
//!
//! Apply lives in [`crate::api::apply_search_replace_blocks`]. Hosts must not
//! `replacen(..., 1)` or raw `fs::write` for this format. CLI / MCP / tx
//! detect this grammar via [`looks_like_search_replace`] (#2221).

/// Shared refuse when `replace_all` is set on unified or Begin Patch.
pub(crate) const REPLACE_ALL_ONLY_FOR_SEARCH_REPLACE: &str =
    "replace_all is only valid for SEARCH/REPLACE documents";

/// True when any line trims to `<<<<<<< SEARCH`.
#[must_use]
pub fn has_search_replace_marker(input: &str) -> bool {
    input.lines().any(|l| l.trim() == "<<<<<<< SEARCH")
}

/// True when the payload is a SEARCH/REPLACE or DiffFenced document.
///
/// First non-empty line must be `<<<<<<< SEARCH` or a fence (` ``` `) so a
/// unified diff that happens to mention that marker as later content is
/// still parsed as a unified diff.
#[must_use]
pub fn looks_like_search_replace(input: &str) -> bool {
    if !has_search_replace_marker(input) {
        return false;
    }
    match input.lines().map(str::trim).find(|l| !l.is_empty()) {
        Some("<<<<<<< SEARCH") => true,
        Some(l) if l.starts_with("```") => true,
        _ => false,
    }
}

/// True when SEARCH/REPLACE markers appear with Begin Patch or unified-diff
/// file headers in a document that is otherwise a SEARCH/REPLACE payload.
#[must_use]
pub fn has_mixed_search_replace_grammar(input: &str) -> bool {
    if !looks_like_search_replace(input) {
        return false;
    }
    crate::ops::begin_patch::looks_like_begin_patch(input) || has_unified_diff_headers(input)
}

fn has_unified_diff_headers(input: &str) -> bool {
    input.lines().any(|line| {
        let t = line.trim_start();
        t.starts_with("diff --git ")
            || t.starts_with("--- a/")
            || t.starts_with("--- b/")
            || t.starts_with("+++ a/")
            || t.starts_with("+++ b/")
    })
}

/// Parse SEARCH/REPLACE, or DiffFenced (fenced unwrap) when the document
/// wraps blocks in triple backticks.
pub fn parse_search_replace_document(
    input: &str,
) -> Result<Vec<SearchReplaceBlock>, SearchReplaceParseError> {
    if has_mixed_search_replace_grammar(input) {
        return Err(SearchReplaceParseError::malformed(
            "mixed SEARCH/REPLACE and unified-diff or Begin Patch grammar is not supported",
        ));
    }
    let fenced = input.lines().any(|l| {
        let t = l.trim();
        t == "```" || t.starts_with("```")
    });
    if fenced {
        parse_diff_fenced(input)
    } else {
        parse_search_replace(input)
    }
}

/// Dest paths declared in a SEARCH/REPLACE / DiffFenced document.
pub fn search_replace_declared_paths(input: &str) -> Result<Vec<String>, SearchReplaceParseError> {
    let mut paths = Vec::new();
    for block in parse_search_replace_document(input)? {
        if !paths.iter().any(|p| p == &block.path) {
            paths.push(block.path);
        }
    }
    Ok(paths)
}

/// One SEARCH/REPLACE block (path + exact old / new).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchReplaceBlock {
    pub path: String,
    pub old: String,
    pub new: String,
}

/// Parse error for SEARCH/REPLACE / DiffFenced documents.
#[derive(Debug)]
pub struct SearchReplaceParseError {
    pub message: String,
    /// Complete blocks parsed before a truncated last block.
    pub complete: Vec<SearchReplaceBlock>,
    pub truncated: bool,
}

impl std::fmt::Display for SearchReplaceParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for SearchReplaceParseError {}

impl SearchReplaceParseError {
    fn malformed(msg: impl Into<String>) -> Self {
        Self {
            message: msg.into(),
            complete: Vec::new(),
            truncated: false,
        }
    }

    fn truncated(complete: Vec<SearchReplaceBlock>) -> Self {
        let n = complete.len();
        Self {
            message: format!(
                "truncated SEARCH/REPLACE: {n} complete block(s) before incomplete last block"
            ),
            complete,
            truncated: true,
        }
    }
}

/// Parse `<<<<<<< SEARCH` / `=======` / `>>>>>>> REPLACE` blocks.
pub fn parse_search_replace(
    input: &str,
) -> Result<Vec<SearchReplaceBlock>, SearchReplaceParseError> {
    parse_search_replace_inner(input)
}

/// DiffFenced: unwrap fenced code blocks, then parse SEARCH/REPLACE.
pub fn parse_diff_fenced(input: &str) -> Result<Vec<SearchReplaceBlock>, SearchReplaceParseError> {
    let unwrapped = strip_fences_for_search_replace(input);
    parse_search_replace_inner(&unwrapped)
}

fn parse_search_replace_inner(
    response: &str,
) -> Result<Vec<SearchReplaceBlock>, SearchReplaceParseError> {
    let cleaned = strip_eos_tokens(response);
    let mut actions = Vec::new();
    let mut remaining: &str = &cleaned;

    while let Some(start) = remaining.find("<<<<<<< SEARCH") {
        let block = &remaining[start..];

        let (end, end_marker_len) = if let Some(pos) = block.find(">>>>>>> REPLACE") {
            (pos, ">>>>>>> REPLACE".len())
        } else if let Some(pos) = block.find(">>>>>>>") {
            let after = &block[pos + ">>>>>>>".len()..];
            let trimmed = after.trim_start();
            if trimmed.is_empty()
                || trimmed.starts_with('\n')
                || trimmed.starts_with("<<<<<<< SEARCH")
            {
                (pos, ">>>>>>>".len())
            } else if actions.is_empty() {
                return Err(SearchReplaceParseError::malformed(
                    "missing >>>>>>> REPLACE marker",
                ));
            } else {
                return Err(SearchReplaceParseError::truncated(actions));
            }
        } else if actions.is_empty() {
            return Err(SearchReplaceParseError::malformed(
                "missing >>>>>>> REPLACE marker",
            ));
        } else {
            return Err(SearchReplaceParseError::truncated(actions));
        };

        let block = &block[..end + end_marker_len];

        let separator = block
            .find("=======")
            .ok_or_else(|| SearchReplaceParseError::malformed("missing ======= separator"))?;

        let search_section = &block["<<<<<<< SEARCH".len()..separator];
        let search_section = search_section.trim_start_matches('\n');

        let (file, old_content) = if let Some(dash_pos) = search_section.find("-------") {
            let f = search_section[..dash_pos].trim();
            let c = search_section[dash_pos + "-------".len()..].trim_start_matches('\n');
            (f.to_string(), c.trim_end_matches('\n').to_string())
        } else {
            let mut lines = search_section.lines();
            let f = lines
                .next()
                .ok_or_else(|| SearchReplaceParseError::malformed("empty SEARCH section"))?
                .trim()
                .to_string();
            let c: String = lines.collect::<Vec<_>>().join("\n");
            (f, c)
        };

        let replace_section = &block[separator + "=======".len()..];
        let replace_section = replace_section
            .strip_prefix('\n')
            .unwrap_or(replace_section);
        let new_content = if let Some(stripped) = replace_section.strip_suffix("\n>>>>>>> REPLACE")
        {
            stripped.to_string()
        } else if let Some(stripped) = replace_section.strip_suffix("\n>>>>>>>") {
            stripped.to_string()
        } else if let Some(stripped) = replace_section.strip_suffix(">>>>>>> REPLACE") {
            stripped.to_string()
        } else if let Some(stripped) = replace_section.strip_suffix(">>>>>>>") {
            stripped.to_string()
        } else {
            replace_section.to_string()
        };

        actions.push(SearchReplaceBlock {
            path: file,
            old: old_content,
            new: new_content,
        });

        remaining = &remaining[start + end + end_marker_len..];
    }

    Ok(actions)
}

fn strip_eos_tokens(response: &str) -> String {
    const EOS_PATTERNS: &[&str] = &[
        "<|eos|>",
        "<|eot_id|>",
        "<|end|>",
        "<|im_end|>",
        "<|endoftext|>",
    ];
    let mut cleaned = response.to_string();
    for pat in EOS_PATTERNS {
        cleaned = cleaned.replace(pat, "");
    }
    cleaned
}

fn strip_fences_for_search_replace(input: &str) -> String {
    let mut unwrapped = String::with_capacity(input.len());
    let mut in_fence = false;

    for line in input.lines() {
        let trimmed = line.trim();
        if !in_fence && (trimmed == "```" || trimmed.starts_with("```")) {
            in_fence = true;
            continue;
        }
        if in_fence && trimmed == "```" {
            in_fence = false;
            continue;
        }
        unwrapped.push_str(line);
        unwrapped.push('\n');
    }

    unwrapped
}

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

    #[test]
    fn parse_search_replace_valid() {
        let input = "\
<<<<<<< SEARCH
src/foo.rs
-------
old line
=======
new line
>>>>>>> REPLACE
";
        let blocks = parse_search_replace(input).expect("parse");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].path, "src/foo.rs");
        assert_eq!(blocks[0].old, "old line");
        assert_eq!(blocks[0].new, "new line");
    }

    #[test]
    fn parse_search_replace_eos_token_stripped() {
        let input = "\
<<<<<<< SEARCH
a.rs
-------
old
=======
new
>>>>>>><|eos|>
";
        let blocks = parse_search_replace(input).expect("eos");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].new, "new");
    }

    #[test]
    fn parse_search_replace_eot_id_stripped() {
        let input = "\
<<<<<<< SEARCH
a.rs
-------
old
=======
new
>>>>>>><|eot_id|>
";
        let blocks = parse_search_replace(input).expect("eot");
        assert_eq!(blocks[0].path, "a.rs");
    }

    #[test]
    fn parse_search_replace_bare_close() {
        let input = "\
<<<<<<< SEARCH
a.rs
-------
old
=======
new
>>>>>>>
";
        let blocks = parse_search_replace(input).expect("bare");
        assert_eq!(blocks[0].old, "old");
        assert_eq!(blocks[0].new, "new");
    }

    #[test]
    fn parse_search_replace_truncated_after_first_block() {
        let input = "\
<<<<<<< SEARCH
a.rs
-------
old
=======
new
>>>>>>> REPLACE
<<<<<<< SEARCH
b.rs
-------
incomplete
";
        let err = parse_search_replace(input).expect_err("truncated");
        assert!(err.truncated);
        assert_eq!(err.complete.len(), 1);
        assert_eq!(err.complete[0].path, "a.rs");
    }

    #[test]
    fn parse_diff_fenced_unwraps_fence() {
        let input = "\
```
<<<<<<< SEARCH
a.rs
-------
old
=======
new
>>>>>>> REPLACE
```
";
        let blocks = parse_diff_fenced(input).expect("fenced");
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].path, "a.rs");
    }

    #[test]
    fn looks_like_search_replace_first_line_or_fence() {
        assert!(looks_like_search_replace(
            "<<<<<<< SEARCH\nfile.rs\n-------\nold\n=======\nnew\n>>>>>>> REPLACE\n"
        ));
        assert!(looks_like_search_replace(
            "```\n<<<<<<< SEARCH\nfile.rs\n-------\nold\n=======\nnew\n>>>>>>> REPLACE\n```\n"
        ));
        assert!(!looks_like_search_replace(
            "--- a/file.rs\n+++ b/file.rs\n@@ -1 +1 @@\n-old\n+new\n"
        ));
        assert!(
            !looks_like_search_replace(
                "--- a/file.rs\n+++ b/file.rs\n@@ -1,3 +1,3 @@\n context\n <<<<<<< SEARCH\n+keep\n"
            ),
            "unified diff that mentions SEARCH later is not SEARCH/REPLACE"
        );
        assert!(has_search_replace_marker("--- a/x\n<<<<<<< SEARCH\nkeep\n"));
        assert!(!has_search_replace_marker("--- a/x\n+++ b/x\n"));
    }

    #[test]
    fn mixed_search_replace_and_unified_headers_refused() {
        let input = "\
<<<<<<< SEARCH
file.rs
-------
old
=======
new
>>>>>>> REPLACE
--- a/file.rs
+++ b/file.rs
";
        assert!(has_mixed_search_replace_grammar(input));
        let err = parse_search_replace_document(input).expect_err("mixed");
        assert!(!err.truncated, "mixed grammar is malformed, not truncated");
        assert!(
            err.message.contains("mixed SEARCH/REPLACE"),
            "expected mixed-grammar refuse, got {}",
            err.message
        );
    }

    #[test]
    fn parse_search_replace_first_line_is_path_without_dashes() {
        let input = "\
<<<<<<< SEARCH
only.rs
the old text
=======
the new text
>>>>>>> REPLACE
";
        let blocks = parse_search_replace(input).expect("no dashes");
        assert_eq!(blocks[0].path, "only.rs");
        assert_eq!(blocks[0].old, "the old text");
    }
}