patchloom 0.25.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
//! File-level operations (create, delete, rename, append, prepend) for the library API.
//!
//! Standard file operations delegate to the tx engine via `execute_as_edit_result`.
//! All operations, including prepend, route through the tx engine.

use std::path::Path;

#[cfg(not(any(feature = "cli", feature = "files")))]
use anyhow::{Context, bail};

use crate::containment::PathGuard;
use crate::plan::Operation;

use super::{ApplyMode, EditResult};

/// Derive cwd from a file path (its parent directory).
#[cfg(any(feature = "cli", feature = "files"))]
fn cwd_from_path(path: &Path) -> &Path {
    path.parent().unwrap_or_else(|| Path::new("."))
}

/// Absolutize for engine handoff; map IO errors to OperationFailed.
#[cfg(any(feature = "cli", feature = "files"))]
fn abs_path(path: &Path) -> anyhow::Result<std::path::PathBuf> {
    super::absolute_for_engine(path).map_err(|e| {
        crate::fallback::EditError::new(
            crate::fallback::EditErrorKind::OperationFailed,
            format!("failed to resolve path {}: {e}", path.display()),
        )
        .into()
    })
}

/// Unified write path for standard file operations.
#[cfg(any(feature = "cli", feature = "files"))]
fn file_write(
    op: Operation,
    path: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    action: &'static str,
) -> anyhow::Result<EditResult> {
    let display = path.to_string_lossy();
    super::execute_as_edit_result_with_path(
        op,
        mode,
        cwd_from_path(path),
        guard,
        action,
        None,
        Some(display.as_ref()),
    )
}

#[cfg(not(any(feature = "cli", feature = "files")))]
fn file_write(
    op: Operation,
    path: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    action: &'static str,
) -> anyhow::Result<EditResult> {
    // Fallback for no-cli/files builds: delegate to the ops layer directly.
    match op {
        Operation::FileCreate { content, force, .. } => {
            let path_str = path.to_string_lossy();
            use crate::ops::file::{PathEntryKind, classify_path_entry, path_entry_exists};
            // Match engine: entry presence (dangling is present) + real dirs refuse.
            match classify_path_entry(path) {
                PathEntryKind::RealDirectory => {
                    return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                        msg: format!("target is not a file: {}", path.display()),
                    }));
                }
                PathEntryKind::Missing | PathEntryKind::RegularFile | PathEntryKind::Special => {}
            }
            crate::ops::file::ensure_parent_components_are_directories(path)?;
            let force = force.unwrap_or(false);
            // Match engine path: refuse existing without force in all modes
            // (Preview/Check/Apply). Preview must not soft-succeed (#MPI dual-path).
            if !force && path_entry_exists(path) {
                return Err(anyhow::Error::new(crate::exit::AlreadyExistsError {
                    msg: format!(
                        "file already exists: {} (use force to overwrite)",
                        path.display()
                    ),
                }));
            }
            // Force: soft-load prior (binary/encoding/unreadable → empty) (#1962).
            // Special nodes (dangling) → empty original; regular text strict load.
            let original = match classify_path_entry(path) {
                PathEntryKind::RegularFile => {
                    match crate::files::load_text_strict(path, &path_str) {
                        Ok(s) => s,
                        Err(e) if force && crate::exit::is_load_text_strict_fail(&e) => {
                            String::new()
                        }
                        Err(e) => return Err(e),
                    }
                }
                PathEntryKind::Missing | PathEntryKind::Special | PathEntryKind::RealDirectory => {
                    String::new()
                }
            };
            let policy = crate::write::WritePolicy::default();
            let (applied, backup_session) =
                super::write_if_apply(path, &content, mode, &policy, guard)?;
            {
                let mut __e =
                    super::build_edit_result(&path_str, original, content, applied, action, None);
                __e.backup_session = backup_session;
                Ok(__e)
            }
        }
        Operation::FileDelete { .. } => {
            let path_str = path.to_string_lossy();
            // path_entry_exists includes dangling symlinks (#2087).
            if !crate::ops::file::path_entry_exists(path) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!("file not found: {}", path.display()),
                )
                .into());
            }
            // Regular files, symlinks (unlink only), FIFO/socket/device ok;
            // real directories refuse (#2087).
            crate::ops::file::ensure_unlinkable_not_directory(path, path_str.as_ref())?;
            // Delete may remove non-UTF-8 / special nodes; soft snapshot only
            // for regular text files.
            let original = if crate::ops::file::is_regular_file_for_backup(path) {
                crate::files::load_text_strict(path, &path_str).unwrap_or_default()
            } else {
                String::new()
            };
            // Preview/Check: report would-delete without unlinking (#2087 DryRun).
            let (applied, backup_session) = if mode == ApplyMode::Apply {
                super::apply_mutation(
                    path,
                    mode,
                    guard,
                    |backup| backup.save_before_delete(path),
                    || {
                        std::fs::remove_file(path)
                            .with_context(|| format!("failed to delete {}", path.display()))
                    },
                )?
            } else {
                super::ensure_contained(guard, path)?;
                (false, None)
            };
            {
                let mut __e = super::build_edit_result(
                    &path_str,
                    original,
                    String::new(),
                    applied,
                    action,
                    None,
                );
                __e.backup_session = backup_session;
                Ok(__e)
            }
        }
        Operation::FileAppend { ref content, .. } | Operation::FilePrepend { ref content, .. } => {
            let is_append = matches!(op, Operation::FileAppend { .. });
            let content = content.clone();
            let path_str = path.to_string_lossy();
            // Match CLI/tx: entry presence (dangling symlink is present) and
            // require a regular file for content inject (#2087 dual-path).
            use crate::ops::file::{PathEntryKind, classify_path_entry, path_entry_exists};
            if !path_entry_exists(path) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!("file does not exist: {}", path.display()),
                )
                .into());
            }
            if classify_path_entry(path) != PathEntryKind::RegularFile {
                return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                    msg: format!("target is not a file: {}", path.display()),
                }));
            }
            let original = crate::files::load_text_strict(path, &path_str)?;
            let combined = if is_append {
                crate::ops::file::append_content(&original, &content)
            } else {
                crate::ops::file::prepend_content(&original, &content)
            };
            let policy = crate::write::WritePolicy::default();
            let (applied, backup_session) =
                super::write_if_apply(path, &combined, mode, &policy, guard)?;
            {
                let mut __e =
                    super::build_edit_result(&path_str, original, combined, applied, action, None);
                __e.backup_session = backup_session;
                Ok(__e)
            }
        }
        _ => bail!("unsupported file operation"),
    }
}

/// Unified cross-file write path (rename).
#[cfg(any(feature = "cli", feature = "files"))]
fn file_write_cross(
    op: Operation,
    src: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    action: &'static str,
    dest_path: Option<String>,
) -> anyhow::Result<EditResult> {
    let display = src.to_string_lossy();
    super::execute_as_edit_result_with_path(
        op,
        mode,
        cwd_from_path(src),
        guard,
        action,
        dest_path,
        Some(display.as_ref()),
    )
}

#[cfg(not(any(feature = "cli", feature = "files")))]
fn file_write_cross(
    _op: Operation,
    src: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    action: &'static str,
    dest_path: Option<String>,
) -> anyhow::Result<EditResult> {
    // Fallback: rename directly.
    if let Operation::FileRename { to, force, .. } = _op {
        let dst = Path::new(&to);
        if !force && dst.exists() {
            return Err(anyhow::Error::new(crate::exit::AlreadyExistsError {
                msg: format!(
                    "destination already exists: {} (use force to overwrite)",
                    dst.display()
                ),
            }));
        }
        // Soft text load for EditResult body only; binary / unreadable still
        // renames the inode (this no-files fallback path).
        let original = crate::files::try_read_text_file(src).unwrap_or_default();
        let (applied, backup_session) = super::apply_cross_file_mutation(
            src,
            Some(dst),
            mode,
            guard,
            |backup| {
                backup.save_before_write(src)?;
                if dst.exists() && force {
                    backup.save_before_write(dst)?;
                }
                Ok(())
            },
            || {
                std::fs::rename(src, dst).with_context(|| {
                    format!("failed to rename {} -> {}", src.display(), dst.display())
                })
            },
        )?;
        {
            let mut __e = super::build_edit_result(
                &src.to_string_lossy(),
                original.clone(),
                original,
                applied,
                action,
                dest_path,
            );
            __e.backup_session = backup_session;
            Ok(__e)
        }
    } else {
        bail!("unsupported cross-file operation")
    }
}

/// Create a new file with the given content.
///
/// If `force` is false, fails when the file already exists
/// ([`EditErrorKind::AlreadyExists`]).
///
/// When `force` is true and the path already holds binary, invalid UTF-8, or
/// otherwise unreadable prior content, the create **overwrites** with empty
/// original for backup/diff (no host-side remove+recreate). PathGuard still
/// applies. Apply writes use the normal hardlink-preserving commit path (#1962).
pub fn file_create(
    path: &Path,
    content: &str,
    force: bool,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    #[cfg(any(feature = "cli", feature = "files"))]
    let path_owned = abs_path(path)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let path = path_owned.as_path();
    let op = Operation::FileCreate {
        path: path.to_string_lossy().into(),
        content: content.into(),
        force: Some(force),
    };
    file_write(op, path, mode, guard, "create")
}

/// Delete a file, symlink, FIFO, socket, or device node under PathGuard (#2087).
///
/// **Directories are refused** (use a host-side recursive delete if needed).
/// **Symlinks** are unlinked without following the target. Regular-file content
/// is backed up for undo; special nodes get an empty backup marker (restore
/// recreates an empty regular file, not the original node type).
///
/// DryRun / Preview / Check report would-delete without unlinking.
pub fn file_delete(
    path: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    #[cfg(any(feature = "cli", feature = "files"))]
    let path_owned = abs_path(path)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let path = path_owned.as_path();
    let op = Operation::FileDelete {
        path: path.to_string_lossy().into(),
    };
    file_write(op, path, mode, guard, "delete")
}

/// Rename (move) a file, symlink, FIFO, socket, or device node (#2091).
///
/// **Directories are refused.** Symlinks (including dangling and symlink-to-dir)
/// are moved as directory entries without following the target. Soft-loading
/// symlink text and rewriting would mutate the target via `atomic_write`;
/// special nodes use an empty path-only snapshot so write policies never
/// rewrite the link target. Regular-file content is soft-loaded for
/// preview/diff; binary / invalid UTF-8 still path-rename (#2031).
pub fn file_rename(
    src: &Path,
    dst: &Path,
    force: bool,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    #[cfg(any(feature = "cli", feature = "files"))]
    let src_owned = abs_path(src)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let dst_owned = abs_path(dst)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let src = src_owned.as_path();
    #[cfg(any(feature = "cli", feature = "files"))]
    let dst = dst_owned.as_path();
    let op = Operation::FileRename {
        from: src.to_string_lossy().into(),
        to: dst.to_string_lossy().into(),
        force,
    };
    let dest_str = Some(dst.to_string_lossy().to_string());
    file_write_cross(op, src, mode, guard, "rename", dest_str)
}

/// Append content to an existing file.
///
/// The file must exist (use file_create for new files). A trailing newline
/// is ensured between existing content and the appended content when needed.
pub fn file_append(
    path: &Path,
    content: &str,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    #[cfg(any(feature = "cli", feature = "files"))]
    let path_owned = abs_path(path)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let path = path_owned.as_path();
    let op = Operation::FileAppend {
        path: path.to_string_lossy().into(),
        content: content.into(),
    };
    file_write(op, path, mode, guard, "append")
}

/// Prepend content to an existing file.
///
/// The file must exist. Content is inserted at the beginning.
pub fn file_prepend(
    path: &Path,
    content: &str,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    #[cfg(any(feature = "cli", feature = "files"))]
    let path_owned = abs_path(path)?;
    #[cfg(any(feature = "cli", feature = "files"))]
    let path = path_owned.as_path();
    let op = Operation::FilePrepend {
        path: path.to_string_lossy().into(),
        content: content.into(),
    };
    file_write(op, path, mode, guard, "prepend")
}