atomwrite 0.1.36

Atomic file operations CLI for LLM agents — read, write, edit, search, replace with NDJSON output
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Patch application from stdin: unified diff, SEARCH/REPLACE, full file.
//! Workload: I/O-bound (stdin read + patch parse + atomic write).
//! Parallelism: none — single-file / sequential-by-contract (coordination cost ≥ work).

use std::io::{Read, Write};
use std::time::Instant;

use anyhow::Result;

use crate::atomic::{AtomicWriteOptions, atomic_write};
use crate::checksum;
use crate::cli::{ApplyArgs, GlobalArgs, PatchFormat};
use crate::commands::resolve_backup;
use crate::error::AtomwriteError;
use crate::ndjson_types::ApplyResult;
use crate::output::NdjsonWriter;

/// Apply a patch from stdin to a target file.
///
/// # Errors
///
/// Returns `AtomwriteError::NotFound` if the target file does not exist.
/// Returns `AtomwriteError::InvalidInput` if the patch format is invalid.
/// Returns `AtomwriteError::WorkspaceJail` if the path escapes the workspace.
/// Returns an I/O error if the write fails.
#[tracing::instrument(skip_all, fields(command = "apply"))]
pub fn cmd_apply(
    args: &ApplyArgs,
    global: &GlobalArgs,
    stdin: impl Read,
    writer: &mut NdjsonWriter<impl Write>,
    defaults: &crate::config::DefaultsSection,
    fuzzy_cfg: &crate::config::FuzzySection,
) -> Result<()> {
    let start = Instant::now();
    let workspace = global.resolve_workspace()?;
    let target = crate::path_safety::validate_path(&args.file, &workspace)?;
    let resolved_backup = resolve_backup(&args.backup_opts, defaults);

    let max_stdin = global.effective_max_filesize();
    let mut patch = String::new();
    let reserve = usize::try_from(max_stdin).unwrap_or(usize::MAX);
    patch
        .try_reserve(reserve)
        .map_err(|e| AtomwriteError::InternalError {
            reason: format!("allocation failed for patch buffer of {reserve} bytes: {e}"),
        })?;
    stdin
        .take(max_stdin)
        .read_to_string(&mut patch)
        .map_err(|e| AtomwriteError::Io { source: e })?;

    let format = match args.format {
        PatchFormat::Auto => detect_format(&patch),
        other => other,
    };

    let original = if target.exists() {
        crate::file_io::read_file_string(&target, global.effective_max_filesize())?
    } else if matches!(format, PatchFormat::Full) {
        String::new()
    } else {
        return Err(AtomwriteError::NotFound { path: target }.into());
    };

    let checksum_before = checksum::hash_bytes(original.as_bytes());

    let (result_content, hunks) = match format {
        PatchFormat::Unified => apply_unified(&original, &patch)?,
        PatchFormat::SearchReplace => apply_search_replace(&original, &patch, fuzzy_cfg)?,
        PatchFormat::Full => (patch.clone(), 1),
        PatchFormat::Markdown => {
            let stripped = strip_markdown_fences(&patch);
            apply_unified(&original, &stripped)?
        }
        PatchFormat::Auto => unreachable!("Auto resolved to concrete format before dispatch"),
    };

    let checksum_after = checksum::hash_bytes(result_content.as_bytes());
    let format_name = match format {
        PatchFormat::Unified => "unified",
        PatchFormat::SearchReplace => "search-replace",
        PatchFormat::Full => "full",
        PatchFormat::Markdown => "markdown",
        PatchFormat::Auto => "auto",
    };

    if args.dry_run {
        writer.write_event(&crate::ndjson_types::ApplyPlan {
            r#type: "plan",
            operation: "apply",
            path: target.display().to_string(),
            format_detected: format_name.to_owned(),
            hunks,
        })?;
        return Ok(());
    }

    let opts = AtomicWriteOptions {
        backup: resolved_backup.backup,
        syntax_check: false,
        retention: resolved_backup.retention,
        preserve_timestamps: false,
        backup_output_dir: None,
        strategy: None,
        strict_atomic: false,
        wal_policy: crate::wal::WalPolicy::Auto,
        keep_backup: resolved_backup.keep,
        durability: crate::platform::Durability::Auto,
    };
    atomic_write(&target, result_content.as_bytes(), &opts, &workspace)?;

    writer.write_event(&ApplyResult {
        r#type: "applied",
        path: target.display().to_string(),
        format_detected: format_name.to_owned(),
        hunks_applied: hunks as u64,
        bytes_before: original.len() as u64,
        bytes_after: result_content.len() as u64,
        checksum_before,
        checksum_after,
        elapsed_ms: start.elapsed().as_millis() as u64,
    })?;

    Ok(())
}

fn detect_format(patch: &str) -> PatchFormat {
    let head: String = {
        let mut buf = String::with_capacity(256);
        for (i, line) in patch.lines().take(15).enumerate() {
            if i > 0 {
                buf.push('\n');
            }
            buf.push_str(line);
        }
        buf
    };
    if head.contains("--- ") && head.contains("+++ ") && head.contains("@@ ") {
        PatchFormat::Unified
    } else if head.contains("<<<<<<< SEARCH") {
        PatchFormat::SearchReplace
    } else if head.contains("```diff") {
        PatchFormat::Markdown
    } else {
        PatchFormat::Full
    }
}

fn apply_unified(original: &str, patch: &str) -> Result<(String, usize)> {
    let mut lines: Vec<&str> = original.lines().collect();
    let mut hunks_applied = 0usize;
    let mut offset: i64 = 0;

    for hunk in parse_unified_hunks(patch) {
        let start_line = ((hunk.old_start as i64) + offset - 1).max(0) as usize;

        let mut new_lines = Vec::with_capacity(hunk.ops.len());
        let mut old_consumed = 0usize;

        for op in &hunk.ops {
            match op {
                HunkOp::Context(text) => {
                    new_lines.push(*text);
                    old_consumed += 1;
                }
                HunkOp::Delete(()) => {
                    old_consumed += 1;
                }
                HunkOp::Insert(text) => {
                    new_lines.push(*text);
                }
            }
        }

        let end = (start_line + old_consumed).min(lines.len());
        lines.splice(start_line..end, new_lines);
        offset += hunk.new_count as i64 - hunk.old_count as i64;
        hunks_applied += 1;
    }

    let mut result = lines.join("\n");
    if original.ends_with('\n') && !result.ends_with('\n') {
        result.push('\n');
    }

    Ok((result, hunks_applied))
}

struct Hunk<'a> {
    old_start: usize,
    old_count: usize,
    new_count: usize,
    ops: Vec<HunkOp<'a>>,
}

enum HunkOp<'a> {
    Context(&'a str),
    Delete(()),
    Insert(&'a str),
}

fn parse_unified_hunks(patch: &str) -> Vec<Hunk<'_>> {
    let mut hunks = Vec::with_capacity(4);
    let mut current: Option<Hunk<'_>> = None;
    let lines: Vec<&str> = patch.lines().collect();

    for line in &lines {
        if let Some(header) = line.strip_prefix("@@ ") {
            if let Some(h) = current.take() {
                hunks.push(h);
            }
            if let Some((old_start, old_count, new_count)) = parse_hunk_header(header) {
                current = Some(Hunk {
                    old_start,
                    old_count,
                    new_count,
                    ops: Vec::with_capacity(8),
                });
            }
        } else if let Some(ref mut h) = current {
            if let Some(text) = line.strip_prefix('+') {
                h.ops.push(HunkOp::Insert(text));
            } else if line.starts_with('-') {
                h.ops.push(HunkOp::Delete(()));
            } else if let Some(text) = line.strip_prefix(' ') {
                h.ops.push(HunkOp::Context(text));
            } else if !line.starts_with('\\')
                && !line.starts_with("---")
                && !line.starts_with("+++")
            {
                h.ops.push(HunkOp::Context(line));
            }
        }
    }

    if let Some(h) = current {
        hunks.push(h);
    }

    hunks
}

fn parse_hunk_header(header: &str) -> Option<(usize, usize, usize)> {
    let parts: Vec<&str> = header.split_whitespace().collect();
    if parts.len() < 3 {
        return None;
    }

    let old_part = parts[0].trim_start_matches('-');
    let new_part = parts[1].trim_start_matches('+');

    let (old_start, old_count) = parse_range(old_part)?;
    let (_new_start, new_count) = parse_range(new_part)?;

    Some((old_start, old_count, new_count))
}

fn parse_range(range: &str) -> Option<(usize, usize)> {
    if let Some((start, count)) = range.split_once(',') {
        Some((start.parse().ok()?, count.parse().ok()?))
    } else {
        let start: usize = range.parse().ok()?;
        Some((start, 1))
    }
}

fn apply_search_replace(
    original: &str,
    patch: &str,
    fuzzy_cfg: &crate::config::FuzzySection,
) -> Result<(String, usize)> {
    let mut result = original.to_owned();
    let mut blocks = 0usize;

    let mut i = 0;
    let lines: Vec<&str> = patch.lines().collect();

    while i < lines.len() {
        if lines[i].starts_with("<<<<<<< SEARCH") {
            let search_start = i + 1;
            let mut sep = search_start;
            while sep < lines.len() && lines[sep] != "=======" {
                sep += 1;
            }
            if sep >= lines.len() {
                break;
            }
            let replace_start = sep + 1;
            let mut replace_end = replace_start;
            while replace_end < lines.len() && !lines[replace_end].starts_with(">>>>>>> REPLACE") {
                replace_end += 1;
            }

            let search_text = lines[search_start..sep].join("\n");
            let replace_text = lines[replace_start..replace_end].join("\n");

            // Exact first; on miss use fuzzy cascade (G-FZZ-059/073).
            if let Some(pos) = result.find(&search_text) {
                result.replace_range(pos..pos + search_text.len(), &replace_text);
                blocks += 1;
            } else if let Ok((edited, _info)) = crate::fuzzy::match_pair_cfg(
                &result,
                &search_text,
                &replace_text,
                crate::cli_args::FuzzyMode::Auto,
                None,
                fuzzy_cfg,
                false,
            ) {
                result = edited;
                blocks += 1;
            }

            i = if replace_end < lines.len() {
                replace_end + 1
            } else {
                lines.len()
            };
        } else {
            i += 1;
        }
    }

    if blocks == 0 {
        return Err(AtomwriteError::InvalidInput {
            reason: "no SEARCH/REPLACE blocks found in patch".into(),
        }
        .into());
    }

    Ok((result, blocks))
}

fn strip_markdown_fences(patch: &str) -> String {
    let mut in_block = false;
    let mut result = Vec::with_capacity(patch.lines().count());

    for line in patch.lines() {
        if line.starts_with("```diff") {
            in_block = true;
            continue;
        }
        if in_block && line.starts_with("```") {
            in_block = false;
            continue;
        }
        if in_block {
            result.push(line);
        }
    }

    result.join("\n")
}

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

    #[test]
    fn detect_format_unified() {
        let patch = "--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,3 @@\n";
        assert!(matches!(detect_format(patch), PatchFormat::Unified));
    }

    #[test]
    fn detect_format_search_replace() {
        let patch = "<<<<<<< SEARCH\nold text\n=======\nnew text\n>>>>>>> REPLACE\n";
        assert!(matches!(detect_format(patch), PatchFormat::SearchReplace));
    }

    #[test]
    fn detect_format_markdown() {
        let patch = "```diff\n--- a/file.txt\n+++ b/file.txt\n```\n";
        assert!(matches!(detect_format(patch), PatchFormat::Markdown));
    }

    #[test]
    fn detect_format_full() {
        let patch = "this is just plain text content\nno markers at all\n";
        assert!(matches!(detect_format(patch), PatchFormat::Full));
    }

    #[test]
    fn strip_markdown_fences_extracts_content() {
        let input = "some preamble\n```diff\n--- a\n+++ b\n@@ -1 +1 @@\n-old\n+new\n```\nafter";
        let result = strip_markdown_fences(input);
        assert!(result.contains("--- a"));
        assert!(!result.contains("```"));
    }

    #[test]
    fn apply_search_replace_basic() {
        let original = "hello world\nfoo bar\n";
        let patch = "<<<<<<< SEARCH\nhello world\n=======\nhello universe\n>>>>>>> REPLACE\n";
        let (result, blocks) =
            apply_search_replace(original, patch, &crate::config::FuzzySection::default()).unwrap();
        assert_eq!(blocks, 1);
        assert!(result.contains("hello universe"));
    }

    #[test]
    fn apply_search_replace_no_blocks_fails() {
        let original = "hello world\n";
        let patch = "no search replace blocks here\n";
        let result = apply_search_replace(original, patch, &crate::config::FuzzySection::default());
        assert!(result.is_err());
    }

    #[test]
    fn apply_unified_simple() {
        let original = "line1\nline2\nline3\n";
        let patch = "--- a/file\n+++ b/file\n@@ -1,3 +1,3 @@\n line1\n-line2\n+modified\n line3\n";
        let (result, hunks) = apply_unified(original, patch).unwrap();
        assert_eq!(hunks, 1);
        assert!(result.contains("modified"));
        assert!(!result.contains("line2"));
    }
}