atomwrite 0.1.38

Atomic file operations CLI for LLM agents — read, write, edit, search, replace with NDJSON output
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/// Resolve `old` → `new` with default uniqueness (no `replace_all`).
///
/// # Examples
///
/// ```
/// use atomwrite::cli::FuzzyMode;
/// use atomwrite::fuzzy::match_pair;
///
/// let content = "fn main() {\n    let x = 1;\n}\n";
/// let (updated, info) = match_pair(content, "let x = 1;", "let x = 2;", FuzzyMode::Off, None)?;
/// assert_eq!(updated, "fn main() {\n    let x = 2;\n}\n");
/// assert!(!info.fuzzy);
///
/// // A near miss fails with `FuzzyMode::Off` and the best candidate is ranked.
/// let miss = match_pair(content, "let x = 10;", "let x = 2;", FuzzyMode::Off, None);
/// assert!(miss.is_err());
/// # Ok::<(), atomwrite::error::AtomwriteError>(())
/// ```
///
/// # Errors
///
/// Returns `AtomwriteError::Cancelled` when the global shutdown flag is set by a signal or `--timeout-secs`.
/// Returns `AtomwriteError::InvalidInput` when `old` is empty, when `new` carries a backslash-escaped quote absent from `old` and the matched region, or when `old` exceeds `FUZZY_MAX_PATTERN_BYTES`.
/// Returns `AtomwriteError::MatchAmbiguous` when `old` matches more than one region, because this wrapper never sets `replace_all`.
/// Returns `AtomwriteError::MatchFailed` when no exact or fuzzy strategy matches, carrying the best candidate when one clears the floor.
/// Returns `AtomwriteError::InternalError` when the line table cannot be allocated or an exact match vanishes between count and replace.
pub fn match_pair(
    content: &str,
    old: &str,
    new: &str,
    fuzzy_mode: FuzzyMode,
    custom_threshold: Option<f64>,
) -> std::result::Result<(String, FuzzyInfo), AtomwriteError> {
    match_pair_with(
        content,
        old,
        new,
        MatchOpts {
            mode: fuzzy_mode,
            threshold: custom_threshold,
            replace_all: false,
            ..Default::default()
        },
    )
}

/// Resolve pair using full XDG [`crate::config::FuzzySection`] thresholds (R-THR wire).
///
/// # Errors
///
/// Returns `AtomwriteError::Cancelled` when the global shutdown flag is set by a signal or `--timeout-secs`.
/// Returns `AtomwriteError::InvalidInput` when `old` is empty, when `new` carries a backslash-escaped quote absent from `old` and the matched region, when `old` exceeds `FUZZY_MAX_PATTERN_BYTES`, or when `content` exceeds `section.max_content_bytes`.
/// Returns `AtomwriteError::MatchAmbiguous` when more than one region matches and `replace_all` is false.
/// Returns `AtomwriteError::MatchFailed` when no strategy matches, carrying the best candidate when one clears the floor.
/// Returns `AtomwriteError::InternalError` when the line table cannot be allocated or an exact match vanishes between count and replace.
pub fn match_pair_cfg(
    content: &str,
    old: &str,
    new: &str,
    mode: FuzzyMode,
    cli_threshold: Option<f64>,
    section: &crate::config::FuzzySection,
    replace_all: bool,
) -> std::result::Result<(String, FuzzyInfo), AtomwriteError> {
    match_pair_with(
        content,
        old,
        new,
        match_opts_from_section(mode, cli_threshold, section, replace_all),
    )
}

/// Full match with `replace_all` / uniqueness / guards (v0.1.30).
///
/// # Errors
///
/// Returns `AtomwriteError::Cancelled` when the global shutdown flag is set by a signal or `--timeout-secs` before or during the cascade.
/// Returns `AtomwriteError::InvalidInput` when `old` is empty, when `new` carries a backslash-escaped quote absent from `old` and the matched region, when `old` exceeds `FUZZY_MAX_PATTERN_BYTES`, or when `content` exceeds `opts.max_content_bytes`.
/// Returns `AtomwriteError::MatchAmbiguous` when more than one region matches and `replace_all` is false.
/// Returns `AtomwriteError::MatchFailed` when no strategy matches, carrying the best candidate when one clears the floor.
/// Returns `AtomwriteError::InternalError` when the line table cannot be allocated or an exact match vanishes between count and replace.
pub fn match_pair_with(
    content: &str,
    old: &str,
    new: &str,
    opts: MatchOpts,
) -> std::result::Result<(String, FuzzyInfo), AtomwriteError> {
    check_cancel()?;
    if old.is_empty() {
        return Err(AtomwriteError::InvalidInput {
            reason: "old string must not be empty".into(),
        });
    }
    guard_escape_drift(old, new, content)?;

    // Strategy 1: exact first (G-048: no FUZZY_MAX_PATTERN on exact / large --old-file)
    let exact_count = count_occurrences(content, old)?;
    if exact_count > 0 {
        if exact_count > 1 && !opts.replace_all {
            let off = find_str(content, old).unwrap_or(0);
            let (line, column) = line_col_of_offset(content, off);
            let bc = BestCandidate {
                text: Some(truncate_text(old)),
                line: Some(line),
                column: Some(column),
                similarity: Some(1.0),
                strategy: Some("exact".into()),
                diff_preview: None,
            };
            return Err(ambiguous_err(old, exact_count, Some(bc)));
        }
        let edited = if opts.replace_all {
            content.replace(old, new)
        } else {
            let Some(pos) = find_str(content, old) else {
                return Err(AtomwriteError::InternalError {
                    reason: "exact match vanished between count and replace".into(),
                });
            };
            format!("{}{}{}", &content[..pos], new, &content[pos + old.len()..])
        };
        return Ok((
            edited,
            FuzzyInfo {
                fuzzy: false,
                strategy: "exact".into(),
                strategies_tried: crate::constants::FUZZY_TRIED_EXACT,
                similarity: None,
                // The preview answers "what changed", so it compares the text
                // that left with the text that arrived. Comparing `old` with
                // itself printed the removed line twice and never showed the
                // replacement, which is the one thing the caller asked for.
                diff_preview: mini_diff(old, new),
                match_count: if opts.replace_all { exact_count } else { 1 },
                indent_adjusted: false,
            },
        ));
    }

    // G-010: Off stops after exact — no fuzzy cascade.
    if matches!(opts.mode, FuzzyMode::Off) {
        return Err(AtomwriteError::MatchFailed {
            reason: format!("old string not found (fuzzy mode off = exact-only): {old:?}"),
            best_candidate: None,
            candidates: None,
        });
    }

    // G-048: fuzzy cascade only — cap pattern size (exact already handled above).
    if old.len() > crate::constants::FUZZY_MAX_PATTERN_BYTES {
        return Err(AtomwriteError::InvalidInput {
            reason: format!(
                "{} ({} bytes > {} max); shorten the block or use a smaller unique slice for fuzzy match",
                crate::constants::FUZZY_PATTERN_CAP_REASON_MARKER,
                old.len(),
                crate::constants::FUZZY_MAX_PATTERN_BYTES
            ),
        });
    }

    // v0.1.37: above the cap the cascade would not finish inside the deadline;
    // fail loudly instead of reporting a miss. Exact matching above is uncapped.
    if content.len() as u64 > opts.max_content_bytes {
        return Err(AtomwriteError::InvalidInput {
            reason: format!(
                "content too large for the fuzzy cascade ({} bytes > {} max); raise {} in config.toml or pass --fuzzy off for exact-only matching",
                content.len(),
                opts.max_content_bytes,
                crate::constants::FUZZY_CONTENT_CAP_REASON_MARKER
            ),
        });
    }
    check_cancel()?;

    let old_lines = collect_lines(old)?;
    let content_lines = collect_lines(content)?;
    let mut best: Option<BestCandidate> = None;
    let mut cands: Vec<BestCandidate> = Vec::new();

    // GAP-2026-026: non-overlapping collect lives in collect.inc.rs (SRP).
    let collect_all =
        |finder: LineRangeFinder| -> std::result::Result<Vec<(usize, usize)>, AtomwriteError> {
            collect_all_ranges(&content_lines, &old_lines, finder)
        };

    let apply_hits = |hits: Vec<(usize, usize)>,
                      name: &str,
                      tried: u64,
                      sim: Option<f64>|
     -> std::result::Result<(String, FuzzyInfo), AtomwriteError> {
        if hits.is_empty() {
            return Err(AtomwriteError::InvalidInput {
                reason: "internal: empty hits".into(),
            });
        }
        if hits.len() > 1 && !opts.replace_all {
            let (start, end) = hits[0];
            let text = content_lines[start..end].join("\n");
            let (line, column) = line_start_position(start);
            let bc = BestCandidate {
                text: Some(truncate_text(&text)),
                line: Some(line),
                column: Some(column),
                similarity: None,
                strategy: Some("ambiguous".into()),
                diff_preview: None,
            };
            return Err(ambiguous_err(old, hits.len() as u64, Some(bc)));
        }
        if hits.len() == 1 || !opts.replace_all {
            let (start, end) = hits[0];
            let matched = content_lines[start..end].join("\n");
            // G-FZZ-001/083: post-match escape-drift against the matched region only.
            guard_escape_drift(old, new, &matched)?;
            let matched_slice = &content_lines[start..end];
            let mut adjusted_new = apply_indent_delta_block(matched_slice, new);
            let indent_adjusted = adjusted_new != new;
            adjusted_new = preserve_unicode_in_replacement(&matched, &adjusted_new);
            adjusted_new = maybe_unescape_new_string(&adjusted_new, &matched);
            let edited = apply_line_replacement(content, &content_lines, start, end, &adjusted_new);
            return Ok((
                edited,
                FuzzyInfo {
                    fuzzy: true,
                    strategy: name.into(),
                    strategies_tried: tried,
                    similarity: sim,
                    diff_preview: mini_diff(old, &matched),
                    match_count: 1,
                    indent_adjusted,
                },
            ));
        }
        // replace_all: apply from bottom
        let mut lines: Vec<String> = content_lines.iter().map(|s| (*s).to_string()).collect();
        let mut ordered = hits;
        ordered.sort_by(|a, b| b.0.cmp(&a.0));
        let count = ordered.len() as u64;
        let mut indent_adjusted = false;
        for (start, end) in ordered {
            check_cancel()?;
            let matched_first = lines.get(start).map(|s| s.as_str()).unwrap_or("");
            let adjusted = apply_indent_delta(matched_first, new);
            indent_adjusted |= adjusted != new;
            let before: Vec<&str> = lines[..start].iter().map(|s| s.as_str()).collect();
            let after: Vec<&str> = lines[end..].iter().map(|s| s.as_str()).collect();
            let mut rebuilt = Vec::new();
            rebuilt.extend(before.iter().map(|s| (*s).to_string()));
            for nl in adjusted.lines() {
                rebuilt.push(nl.to_string());
            }
            if adjusted.ends_with('\n') {
                // lines() drops trailing empty; keep simple
            }
            rebuilt.extend(after.iter().map(|s| (*s).to_string()));
            lines = rebuilt;
        }
        let mut out = lines.join("\n");
        if content.ends_with('\n') && !out.ends_with('\n') {
            out.push('\n');
        }
        Ok((
            out,
            FuzzyInfo {
                fuzzy: true,
                strategy: name.into(),
                strategies_tried: tried,
                similarity: sim,
                diff_preview: None,
                match_count: count,
                indent_adjusted,
            },
        ))
    };

    // Strategy 2–5,7
    for (name, tried, finder) in [
        (
            "line_trimmed",
            crate::constants::FUZZY_TRIED_LINE_TRIMMED,
            match_line_trimmed as LineRangeFinder,
        ),
        (
            "whitespace_normalized",
            crate::constants::FUZZY_TRIED_WHITESPACE,
            match_whitespace_normalized,
        ),
        (
            "punctuation_normalized",
            crate::constants::FUZZY_TRIED_PUNCTUATION,
            match_punctuation_normalized,
        ),
        (
            "indent_flexible",
            crate::constants::FUZZY_TRIED_INDENT,
            match_indent_flexible,
        ),
        (
            "trimmed_boundary",
            crate::constants::FUZZY_TRIED_TRIMMED_BOUNDARY,
            match_trimmed_boundary,
        ),
    ] {
        let hits = collect_all(finder)?;
        check_cancel()?;
        if !hits.is_empty() {
            // GAP-2026-021: normalized exact-equivalent strategies are not byte-identical.
            // Emit similarity: null (not a fake 1.0).
            return apply_hits(hits, name, tried, None);
        }
    }

    // Strategy 6: escape-normalized (byte offsets) — G-FZZ-120 post-guards.
    if let Some((orig_start, orig_end)) = match_escape_normalized(content, old)? {
        let matched = &content[orig_start..orig_end];
        guard_escape_drift(old, new, matched)?;
        let mut adjusted_new = apply_indent_delta(matched.lines().next().unwrap_or(""), new);
        let indent_adjusted = adjusted_new != new;
        adjusted_new = preserve_unicode_in_replacement(matched, &adjusted_new);
        adjusted_new = maybe_unescape_new_string(&adjusted_new, matched);
        let edited = format!(
            "{}{}{}",
            &content[..orig_start],
            adjusted_new,
            &content[orig_end..]
        );
        return Ok((
            edited,
            FuzzyInfo {
                fuzzy: true,
                strategy: "escape_normalized".into(),
                strategies_tried: crate::constants::FUZZY_TRIED_ESCAPE,
                similarity: None,
                diff_preview: mini_diff(old, matched),
                match_count: 1,
                indent_adjusted,
            },
        ));
    }

    // Strategy 8: block-anchor
    let min_ratio = adaptive_threshold(
        opts.threshold.unwrap_or(match opts.mode {
            FuzzyMode::Aggressive => opts.thr_aggressive,
            FuzzyMode::Auto | FuzzyMode::Off => opts.thr_auto,
        }),
        old.chars().count(),
    );
    if let Some((start, end, ratio)) = match_block_anchor(&content_lines, &old_lines, min_ratio)? {
        return apply_hits(
            vec![(start, end)],
            "block_anchor",
            crate::constants::FUZZY_TRIED_BLOCK_ANCHOR,
            Some(ratio),
        );
    }
    check_cancel()?;
    if let Some((start, end, ratio)) =
        match_block_anchor(&content_lines, &old_lines, BEST_CANDIDATE_MIN)?
    {
        let text = content_lines[start..end].join("\n");
        let (line, column) = line_start_position(start);
        consider_best(&mut best, &text, line, column, ratio, "block_anchor", old);
        push_candidate(&mut cands, &text, line, column, ratio, "block_anchor", old);
    }

    // Strategy 8b: unicode_normalized
    check_cancel()?;
    if let Some((start, end)) = match_unicode_normalized(&content_lines, &old_lines)? {
        return apply_hits(
            vec![(start, end)],
            "unicode_normalized",
            crate::constants::FUZZY_TRIED_UNICODE,
            None,
        );
    }
    check_cancel()?;

    // Strategy 9: context-aware
    if matches!(opts.mode, FuzzyMode::Aggressive | FuzzyMode::Auto) {
        let ctx_threshold = adaptive_threshold(
            opts.threshold.unwrap_or(opts.thr_context),
            old.chars().count(),
        );
        if old_lines.len() == 1 && old.len() < crate::constants::FUZZY_JW_MAX_PATTERN_BYTES {
            let jw_threshold =
                adaptive_threshold(opts.threshold.unwrap_or(opts.thr_jw), old.chars().count());
            let mut best_jw = (0usize, 0.0f64);
            let mut all_jw: Vec<(usize, f64)> = Vec::new();
            let mut jw_poll = CancelPoll::lines();
            for (i, line) in content_lines.iter().enumerate() {
                jw_poll.tick()?;
                let score = strsim::jaro_winkler(line.trim(), old.trim());
                if score >= jw_threshold {
                    all_jw.push((i, score));
                }
                if score > best_jw.1 {
                    best_jw = (i, score);
                }
            }
            // FAIL-7: only candidates that survive the dual gate are real
            // competitors; a pile of merely similar lines is a miss, not a tie.
            let dual_floor = adaptive_threshold(
                opts.threshold.unwrap_or(opts.thr_auto),
                old.chars().count(),
            );
            let needle = old.trim();
            let passes_dual_gate = |i: usize, score: f64| {
                let cand = content_lines[i].trim();
                score >= crate::constants::FUZZY_DUAL_GATE_NEAR_EXACT
                    || (strsim::normalized_damerau_levenshtein(cand, needle) >= dual_floor
                        && gestalt_ratio(cand, needle) >= dual_floor
                        && line_vote_ratio(cand, needle) >= dual_floor)
            };
            let passing_jw: Vec<(usize, f64)> = all_jw
                .iter()
                .copied()
                .filter(|&(i, score)| passes_dual_gate(i, score))
                .collect();
            if !all_jw.is_empty() {
                if passing_jw.len() > 1 && !opts.replace_all {
                    let (i0, score0) = passing_jw[0];
                    let (line, column) = line_start_position(i0);
                    let bc = BestCandidate {
                        text: Some(truncate_text(content_lines.get(i0).copied().unwrap_or(""))),
                        line: Some(line),
                        column: Some(column),
                        similarity: Some(score0),
                        strategy: Some("context_aware_jw".into()),
                        diff_preview: None,
                    };
                    return Err(ambiguous_err(old, passing_jw.len() as u64, Some(bc)));
                }
                // G-FZZ-012/121: pick argmax JW, not first hit in scan order.
                let pool = if passing_jw.is_empty() { &all_jw } else { &passing_jw };
                let (i, score) = pool
                    .iter()
                    .copied()
                    .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
                    .unwrap_or(pool[0]);
                // G-FZZ-145: dual-gate — reject high JW when Damerau/gestalt/line-vote weak.
                if !passes_dual_gate(i, score) {
                    let (line, column) = line_start_position(i);
                    consider_best(
                        &mut best,
                        content_lines.get(i).copied().unwrap_or(""),
                        line,
                        column,
                        score,
                        "context_aware_jw",
                        old,
                    );
                    // fall through to context_aware / rank rather than FP replace
                } else {
                    return apply_hits(
                        vec![(i, i + 1)],
                        "context_aware_jw",
                        crate::constants::FUZZY_TRIED_CONTEXT_AWARE,
                        Some(score),
                    );
                }
            }
            if best_jw.1 > 0.0 {
                let (line, column) = line_start_position(best_jw.0);
                consider_best(
                    &mut best,
                    content_lines.get(best_jw.0).copied().unwrap_or(""),
                    line,
                    column,
                    best_jw.1,
                    "context_aware_jw",
                    old,
                );
            }
        }
        if let Some((start, end, similarity)) = match_context_aware(
            &content_lines,
            &old_lines,
            ctx_threshold,
            opts.soft_floor_delta,
        )? {
            return apply_hits(
                vec![(start, end)],
                "context_aware",
                crate::constants::FUZZY_TRIED_CONTEXT_AWARE,
                Some(similarity),
            );
        }
        check_cancel()?;
        rank_into(&content_lines, &old_lines, old, &mut best, &mut cands)?;
    }

    // A deadline that fired during the last strategy must never read as a miss.
    check_cancel()?;
    let reason = format!("old string not found after fuzzy cascade (strategies tried): {old:?}");
    let best = best.or_else(|| cands.first().cloned()).map(Box::new);
    Err(AtomwriteError::MatchFailed {
        reason,
        best_candidate: best,
        candidates: if cands.len() > 1 { Some(cands) } else { None },
    })
}

// ─── matching strategies ─────────────────────────────────────────────────────