patchloom 0.12.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
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
//! Multi-op in-memory content edits for agent hosts (#1459 Feature 2).
//!
//! Compose sequential text operations on one buffer with all-or-nothing
//! semantics, then optionally write once via [`apply_content_edits_to_file`].
//! Results include rolled-up [`ContentEditsResult::match_count`] from replace
//! ops (and the file helper surfaces the same on [`EditResult::match_count`]).

use anyhow::Context;

use crate::ops::file::{append_content, prepend_content};

use super::{ContentEditResult, ReplaceOptions, make_diff, replace_in_content};
use crate::fallback::{EditError, EditErrorKind};

#[cfg(any(feature = "cli", feature = "files"))]
use std::path::Path;

#[cfg(any(feature = "cli", feature = "files"))]
use crate::containment::PathGuard;

#[cfg(any(feature = "cli", feature = "files"))]
use super::{ApplyMode, EditResult, build_edit_result, write_if_apply};

#[cfg(any(feature = "cli", feature = "files"))]
use crate::write::WritePolicy;

/// A single ordered edit on an in-memory buffer.
#[derive(Debug, Clone)]
pub enum ContentEdit {
    /// Text replacement (reuses [`ReplaceOptions`]).
    Replace {
        old: String,
        new: String,
        options: ReplaceOptions,
    },
    /// Insert `content` immediately before the first exact match of `anchor`.
    InsertBefore { anchor: String, content: String },
    /// Insert `content` immediately after the first exact match of `anchor`.
    InsertAfter { anchor: String, content: String },
    /// Append content to the end of the buffer.
    Append { content: String },
    /// Prepend content to the start of the buffer.
    Prepend { content: String },
}

/// Result of applying a sequence of [`ContentEdit`]s to a buffer.
#[derive(Debug, Clone)]
pub struct ContentEditsResult {
    /// Original buffer before any edits.
    pub original: String,
    /// Buffer after all edits (or unchanged when `changed` is false).
    pub modified: String,
    /// Unified diff from original to modified.
    pub diff: String,
    /// Whether any edit changed the buffer.
    pub changed: bool,
    /// Number of ops that successfully applied (equals `edits.len()` on success).
    pub ops_applied: usize,
    /// Sum of replace match counts across all [`ContentEdit::Replace`] ops.
    /// Insert/append/prepend contribute 0. Useful for agent ambiguity policies.
    pub match_count: usize,
}

/// Apply ordered edits to an in-memory buffer.
///
/// **All-or-nothing:** if any op fails, returns `Err` and does not expose a
/// partial buffer. On success, every op has been applied in order; each op
/// sees the result of the previous one.
///
/// Available with default features off as long as the replace path is
/// compiled (always; no `ast` required).
pub fn apply_content_edits(
    content: &str,
    edits: &[ContentEdit],
) -> anyhow::Result<ContentEditsResult> {
    let original = content.to_string();
    let mut current = content.to_string();
    let mut ops_applied = 0usize;
    let mut match_count = 0usize;

    for (i, edit) in edits.iter().enumerate() {
        let (next, n) = apply_one(&current, edit)
            .with_context(|| format!("content edit {} of {} failed", i + 1, edits.len()))?;
        current = next;
        match_count = match_count.saturating_add(n);
        ops_applied += 1;
    }

    let changed = current != original;
    let diff = make_diff("<buffer>", &original, &current);
    Ok(ContentEditsResult {
        original,
        modified: current,
        diff,
        changed,
        ops_applied,
        match_count,
    })
}

/// Read a file, apply multi-op content edits, and write according to `mode`.
///
/// Requires `files` or `cli` for PathGuard / write policy integration.
#[cfg(any(feature = "cli", feature = "files"))]
pub fn apply_content_edits_to_file(
    path: &Path,
    edits: &[ContentEdit],
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    if let Some(g) = guard {
        g.check_path(&path.to_string_lossy()).map_err(|e| {
            EditError::new(
                EditErrorKind::GuardRejected,
                format!("path rejected by workspace guard: {e}"),
            )
        })?;
    }
    let path_str = path.to_string_lossy().into_owned();
    let original =
        std::fs::read_to_string(path).with_context(|| format!("failed to read {path_str}"))?;
    let batch = apply_content_edits(&original, edits)?;
    let policy = WritePolicy::default();
    let applied = write_if_apply(path, &batch.modified, mode, &policy, guard)?;
    // build_edit_result uses path for headers (#1500) and stays field-complete.
    let mut result = build_edit_result(
        &path_str,
        batch.original,
        batch.modified,
        applied,
        "content.edits",
        None,
    );
    result.match_count = batch.match_count;
    Ok(result)
}

/// Returns (new content, replace match_count for this op).
fn apply_one(content: &str, edit: &ContentEdit) -> anyhow::Result<(String, usize)> {
    match edit {
        ContentEdit::Replace { old, new, options } => {
            let result: ContentEditResult = replace_in_content(content, old, new, options)?;
            Ok((result.new_content, result.match_count))
        }
        ContentEdit::InsertBefore {
            anchor,
            content: insert,
        } => {
            if anchor.is_empty() {
                return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                    msg: "insert_before anchor must not be empty".into(),
                }));
            }
            // First-match only (same as String::find). Callers that need
            // unique anchors should use Replace with unique:true or a longer
            // anchor span.
            match content.find(anchor.as_str()) {
                Some(idx) => {
                    let mut out = String::with_capacity(content.len() + insert.len());
                    out.push_str(&content[..idx]);
                    out.push_str(insert);
                    out.push_str(&content[idx..]);
                    Ok((out, 0))
                }
                None => Err(EditError::new(
                    EditErrorKind::NoMatch,
                    format!("insert_before anchor not found: {anchor:?}"),
                )
                .into()),
            }
        }
        ContentEdit::InsertAfter {
            anchor,
            content: insert,
        } => {
            if anchor.is_empty() {
                return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                    msg: "insert_after anchor must not be empty".into(),
                }));
            }
            // First-match only (see InsertBefore).
            match content.find(anchor.as_str()) {
                Some(idx) => {
                    let end = idx + anchor.len();
                    let mut out = String::with_capacity(content.len() + insert.len());
                    out.push_str(&content[..end]);
                    out.push_str(insert);
                    out.push_str(&content[end..]);
                    Ok((out, 0))
                }
                None => Err(EditError::new(
                    EditErrorKind::NoMatch,
                    format!("insert_after anchor not found: {anchor:?}"),
                )
                .into()),
            }
        }
        ContentEdit::Append { content: inject } => Ok((append_content(content, inject), 0)),
        ContentEdit::Prepend { content: inject } => Ok((prepend_content(content, inject), 0)),
    }
}

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

    #[test]
    fn multi_op_replace_then_append() {
        let edits = [
            ContentEdit::Replace {
                old: "hello".into(),
                new: "hi".into(),
                options: ReplaceOptions::default(),
            },
            ContentEdit::Append {
                content: "\nworld".into(),
            },
        ];
        let r = apply_content_edits("hello\n", &edits).unwrap();
        assert!(r.changed);
        assert_eq!(r.ops_applied, 2);
        assert_eq!(r.match_count, 1, "replace match_count should roll up");
        assert_eq!(r.modified, "hi\n\nworld");
        // Pure buffer helper keeps the `<buffer>` path label (#1500).
        assert!(
            r.diff.contains("--- a/<buffer>") && r.diff.contains("+++ b/<buffer>"),
            "buffer helper must label headers as <buffer>, got:\n{}",
            r.diff
        );
        assert!(
            !r.diff.contains("a//") && !r.diff.contains("b//"),
            "buffer headers must not double-slash: {}",
            r.diff
        );
    }

    #[test]
    fn multi_op_all_or_nothing_on_failure() {
        let edits = [
            ContentEdit::Replace {
                old: "a".into(),
                new: "A".into(),
                options: ReplaceOptions::default(),
            },
            ContentEdit::InsertBefore {
                anchor: "missing".into(),
                content: "x".into(),
            },
        ];
        let err = apply_content_edits("a b\n", &edits).unwrap_err();
        assert!(
            err.to_string().contains("content edit 2"),
            "error should identify failing op: {err}"
        );
    }

    #[test]
    fn multi_op_unique_failure() {
        let edits = [ContentEdit::Replace {
            old: "x".into(),
            new: "y".into(),
            options: ReplaceOptions {
                unique: true,
                ..Default::default()
            },
        }];
        let err = apply_content_edits("x x\n", &edits).unwrap_err();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("ambiguous") || msg.contains("matches"),
            "expected unique/ambiguous failure, got: {msg}"
        );
    }

    #[test]
    fn multi_op_insert_before_after() {
        let edits = [
            ContentEdit::InsertBefore {
                anchor: "B".into(),
                content: "A".into(),
            },
            ContentEdit::InsertAfter {
                anchor: "B".into(),
                content: "C".into(),
            },
        ];
        let r = apply_content_edits("B", &edits).unwrap();
        assert_eq!(r.modified, "ABC");
    }

    #[test]
    fn empty_edits_is_noop() {
        let r = apply_content_edits("same\n", &[]).unwrap();
        assert!(!r.changed);
        assert_eq!(r.ops_applied, 0);
        assert_eq!(r.match_count, 0);
        assert_eq!(r.modified, "same\n");
    }

    #[test]
    fn match_count_sums_multiple_replaces() {
        let edits = [
            ContentEdit::Replace {
                old: "a".into(),
                new: "A".into(),
                options: ReplaceOptions::default(),
            },
            ContentEdit::Replace {
                old: "b".into(),
                new: "B".into(),
                options: ReplaceOptions::default(),
            },
        ];
        let r = apply_content_edits("a a b\n", &edits).unwrap();
        assert_eq!(r.match_count, 3, "two a's + one b: {r:?}");
        assert_eq!(r.modified, "A A B\n");
    }

    #[test]
    fn multi_op_prepend() {
        let edits = [ContentEdit::Prepend {
            content: "pre\n".into(),
        }];
        let r = apply_content_edits("body\n", &edits).unwrap();
        assert_eq!(r.modified, "pre\nbody\n");
        assert_eq!(r.ops_applied, 1);
    }

    #[test]
    fn empty_anchor_is_rejected() {
        let before = apply_content_edits(
            "body\n",
            &[ContentEdit::InsertBefore {
                anchor: String::new(),
                content: "x".into(),
            }],
        )
        .unwrap_err();
        let before_msg = format!("{before:#}");
        assert!(
            before_msg.contains("empty"),
            "insert_before empty anchor: {before_msg}"
        );
        assert_eq!(
            crate::fallback::edit_error_kind(&before),
            Some(EditErrorKind::InvalidInput)
        );
        let after = apply_content_edits(
            "body\n",
            &[ContentEdit::InsertAfter {
                anchor: String::new(),
                content: "x".into(),
            }],
        )
        .unwrap_err();
        let after_msg = format!("{after:#}");
        assert!(
            after_msg.contains("empty"),
            "insert_after empty anchor: {after_msg}"
        );
        assert_eq!(
            crate::fallback::edit_error_kind(&after),
            Some(EditErrorKind::InvalidInput)
        );
    }

    #[test]
    fn require_change_true_missing_is_no_match() {
        let edits = [ContentEdit::Replace {
            old: "missing".into(),
            new: "x".into(),
            options: ReplaceOptions {
                require_change: true,
                ..Default::default()
            },
        }];
        let err = apply_content_edits("hello\n", &edits).unwrap_err();
        assert_eq!(
            crate::fallback::edit_error_kind(&err),
            Some(EditErrorKind::NoMatch)
        );
    }

    #[test]
    fn require_change_false_missing_is_ok_unchanged() {
        let edits = [ContentEdit::Replace {
            old: "missing".into(),
            new: "x".into(),
            options: ReplaceOptions::default(),
        }];
        let r = apply_content_edits("hello\n", &edits).unwrap();
        assert!(!r.changed);
        assert_eq!(r.modified, "hello\n");
    }

    #[test]
    fn require_change_unique_multi_is_ambiguous() {
        let edits = [ContentEdit::Replace {
            old: "x".into(),
            new: "y".into(),
            options: ReplaceOptions {
                unique: true,
                require_change: true,
                ..Default::default()
            },
        }];
        let err = apply_content_edits("x x\n", &edits).unwrap_err();
        assert_eq!(
            crate::fallback::edit_error_kind(&err),
            Some(EditErrorKind::AmbiguousTarget)
        );
    }

    #[test]
    fn require_change_batch_fails_whole_batch() {
        let edits = [
            ContentEdit::Replace {
                old: "a".into(),
                new: "A".into(),
                options: ReplaceOptions {
                    require_change: true,
                    ..Default::default()
                },
            },
            ContentEdit::Replace {
                old: "missing".into(),
                new: "z".into(),
                options: ReplaceOptions {
                    require_change: true,
                    ..Default::default()
                },
            },
        ];
        let err = apply_content_edits("a b\n", &edits).unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("content edit 2"), "got: {msg}");
        assert_eq!(
            crate::fallback::edit_error_kind(&err),
            Some(EditErrorKind::NoMatch)
        );
    }
    #[test]
    fn command_position_in_batch_match_count() {
        let edits = [ContentEdit::Replace {
            old: "pip".into(),
            new: "uv".into(),
            options: ReplaceOptions {
                command_position: true,
                require_change: true,
                ..Default::default()
            },
        }];
        let r = apply_content_edits("timeout 5 pip install\nuv pip install\n", &edits).unwrap();
        assert_eq!(r.match_count, 1);
        assert_eq!(r.modified, "timeout 5 uv install\nuv pip install\n");
    }

    #[test]
    fn insert_before_uses_first_anchor_only() {
        let edits = [ContentEdit::InsertBefore {
            anchor: "x".into(),
            content: "IN:".into(),
        }];
        let r = apply_content_edits("a x b x c\n", &edits).unwrap();
        assert_eq!(r.modified, "a IN:x b x c\n");
    }
}