patchloom 0.22.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
//! Text replacement operations for the public library API.
//!
//! Delegates to the tx engine via `execute_as_edit_result`.

use std::path::Path;

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

use super::{ApplyMode, ContentEditResult, EditResult, MatchMode, ReplaceOptions};

/// Replace text in a file using literal or regex matching.
///
/// When `opts.insert_before` or `opts.insert_after` is set, the matched text
/// is preserved and the insertion is added adjacent to it.
pub fn replace_text(
    path: &Path,
    from: &str,
    to: &str,
    opts: &ReplaceOptions,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
) -> anyhow::Result<EditResult> {
    // Pre-validate before building the Operation.
    if from.is_empty() && !opts.regex {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "empty search pattern".into(),
        }));
    }
    if opts.range.is_some() && !opts.whole_line {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "range requires whole_line to be true".into(),
        }));
    }
    if opts.whole_line && opts.multiline {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "whole_line and multiline cannot be combined".into(),
        }));
    }

    let range_str = opts.range.map(|(start, end)| {
        if let Some(e) = end {
            format!("{start}:{e}")
        } else {
            format!("{start}:")
        }
    });
    // Library-only match options that the plan/tx Operation does not carry
    // (`fuzzy`) or that need honest `match_mode` (#1662): use the same
    // content path as command_position so disk and in-memory parity hold.
    // Pure `fuzzy: true` without context was previously a no-op on the
    // default files/cli path (tx only falls back when context is set).
    if opts.command_position
        || opts.fuzzy
        || opts.before_context.is_some()
        || opts.after_context.is_some()
    {
        let display = path.to_string_lossy();
        // Strict sole-path load (#1894); InvalidInputError peels via edit_error_kind.
        let original = crate::files::load_text_strict(path, &display)?;
        let content_result = replace_in_content(&original, from, to, opts)?;
        let policy = crate::write::WritePolicy::default();
        let (applied, backup_session) =
            super::write_if_apply(path, &content_result.new_content, mode, &policy, guard)?;
        let path_str = path.to_string_lossy();
        let mut result = super::build_edit_result(
            &path_str,
            content_result.original,
            content_result.new_content,
            applied,
            "replace",
            None,
        );
        result.match_count = content_result.match_count;
        result.match_mode = content_result.match_mode;
        result.match_score = content_result.match_score;
        result.matched_text = content_result.matched_text;
        result.backup_session = backup_session.clone();
        super::maybe_post_write(
            applied,
            path,
            opts.post_write.as_ref(),
            opts.post_write_cwd.as_deref(),
            backup_session.as_deref(),
        )?;
        return Ok(result);
    }

    let op = Operation::Replace {
        glob: None,
        path: Some(path.to_string_lossy().into()),
        regex: opts.regex,
        old: from.into(),
        new_text: Some(to.into()),
        nth: opts.nth,
        insert_before: opts.insert_before.clone(),
        insert_after: opts.insert_after.clone(),
        case_insensitive: opts.case_insensitive,
        multiline: opts.multiline,
        if_exists: opts.if_exists,
        whole_line: opts.whole_line,
        range: range_str,
        word_boundary: opts.word_boundary,
        before_context: opts.before_context.clone(),
        after_context: opts.after_context.clone(),
        unique: opts.unique,
        // require_change is applied below as structured EditError (not via tx bail).
        require_change: false,
        command_position: opts.command_position,
        fuzzy: false,
        min_fuzzy_score: opts.min_fuzzy_score,
        allow_absent_old: opts.allow_absent_old,
    };
    let mut result = replace_write(op, path, mode, guard, opts.fuzzy)?;
    // if_exists intentionally softens zero-match (Ok unchanged). require_change
    // must not override that: both true means if_exists wins (#1492 docs).
    if opts.require_change && !opts.if_exists && !result.changed && result.match_count == 0 {
        let similar = crate::fallback::find_similar_targets(&result.original_content, from, 3);
        let mut msg = format!("no matches for {:?}", from);
        if !similar.is_empty() {
            msg.push_str(&format!(" (did you mean: {}?)", similar.join(", ")));
        }
        return Err(
            crate::fallback::EditError::new(crate::fallback::EditErrorKind::NoMatch, msg)
                .with_similar(similar)
                .into(),
        );
    }
    // execute_as_edit_result now threads match_mode/count from replace_op meta.
    // Keep a conservative fallback for any path that still omits meta (#1662).
    if result.match_mode.is_none() && (result.match_count > 0 || result.changed) {
        result.match_mode = Some(MatchMode::Exact);
        if result.match_count == 0 && result.changed {
            result.match_count = 1;
        }
    }
    // Post-Apply format/lint hooks (#1690).
    super::maybe_post_write(
        result.applied,
        path,
        opts.post_write.as_ref(),
        opts.post_write_cwd.as_deref(),
        result.backup_session.as_deref(),
    )?;
    Ok(result)
}

/// Unified write path for replace operations.
#[cfg(any(feature = "cli", feature = "files"))]
fn replace_write(
    op: Operation,
    path: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    _fuzzy: bool,
) -> anyhow::Result<EditResult> {
    let cwd = path.parent().unwrap_or_else(|| Path::new("."));
    super::execute_as_edit_result(op, mode, cwd, guard, "replace", None)
}

#[cfg(not(any(feature = "cli", feature = "files")))]
fn replace_write(
    op: Operation,
    path: &Path,
    mode: ApplyMode,
    guard: Option<&PathGuard>,
    fuzzy: bool,
) -> anyhow::Result<EditResult> {
    use crate::ops;
    use anyhow::bail;

    // Fallback for no-cli/files builds: delegate to ops layer directly.
    if let Operation::Replace {
        old,
        new_text,
        regex: regex_mode,
        insert_before,
        insert_after,
        case_insensitive,
        multiline,
        if_exists,
        whole_line,
        range,
        word_boundary,
        nth,
        unique,
        before_context,
        after_context,
        min_fuzzy_score,
        allow_absent_old,
        ..
    } = op
    {
        let path_str = path.to_string_lossy();
        let original = crate::files::load_text_strict(path, &path_str)?;

        let is_regex = regex_mode;
        if old.is_empty() && !is_regex {
            return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                msg: "empty search pattern".into(),
            }));
        }

        let compiled_re = ops::replace::compile_replace_regex(
            &old,
            is_regex,
            case_insensitive,
            multiline,
            word_boundary,
        )?;

        let direct_to = if insert_before.is_none() && insert_after.is_none() {
            new_text.clone()
        } else {
            None
        };
        let replacement = ops::replace::replacement_text(
            &old,
            &direct_to,
            &insert_before,
            &insert_after,
            compiled_re.is_some(),
            is_regex,
            &original,
        );

        let parsed_range = range.as_deref().map(|r| {
            let parts: Vec<&str> = r.splitn(2, ':').collect();
            let start: usize = parts[0].parse().unwrap_or(1);
            let end: Option<usize> = parts
                .get(1)
                .and_then(|s| if s.is_empty() { None } else { s.parse().ok() });
            (start, end)
        });

        let (new_content, count) = if whole_line {
            ops::replace::replace_whole_lines(
                &original,
                &old,
                &replacement,
                compiled_re.as_ref(),
                nth,
                parsed_range,
            )
        } else {
            ops::replace::replace_content(&original, &old, &replacement, compiled_re.as_ref(), nth)
        };

        // Context disambiguation: when multiple exact matches and context is
        // provided, select the match nearest to the context instead of
        // replacing all (mirrors tx engine logic in replace_op.rs).
        // Fail closed when context does not select a unique match.
        if count > 1
            && nth.is_none()
            && !whole_line
            && !is_regex
            && (before_context.is_some() || after_context.is_some())
        {
            if let Some(target_offset) = ops::replace::context_filtered_offset(
                &original,
                &old,
                before_context.as_deref(),
                after_context.as_deref(),
            ) {
                let ctx_content = format!(
                    "{}{}{}",
                    &original[..target_offset],
                    &replacement,
                    &original[target_offset + old.len()..],
                );
                let policy = crate::write::WritePolicy::default();
                let (applied, backup_session) =
                    super::write_if_apply(path, &ctx_content, mode, &policy, guard)?;
                let mut result = super::build_edit_result(
                    &path_str,
                    original,
                    ctx_content,
                    applied,
                    "replace",
                    None,
                );
                result.match_count = 1;
                result.match_mode = Some(MatchMode::Anchored);
                result.backup_session = backup_session;
                return Ok(result);
            }
            return Err(anyhow::Error::new(crate::exit::AmbiguousError {
                msg: format!(
                    "ambiguous match: pattern {:?} matches {} times; context did not select a unique occurrence (use --nth, stronger before/after-context (replace), or --allow-non-unique (apply-fragment))",
                    crate::fallback::truncate_str(&old, 60),
                    count
                ),
            }));
        }

        let new_content = new_content.into_owned();

        // Note: context disambiguation runs BEFORE unique, so context can
        // resolve ambiguity even with unique=true. This diverges from the tx
        // engine (which bails on unique before context), but is intentional:
        // context-resolved results are unambiguous by definition.
        if unique && count > 1 {
            return Err(anyhow::Error::new(crate::exit::AmbiguousError {
                msg: format!(
                    "ambiguous match: pattern {old:?} matches {count} times; tighten the pattern/anchor, use --nth (replace), or --allow-non-unique (apply-fragment)"
                ),
            }));
        }

        // nth past last match: applied count is 0 but pattern may still match.
        if count == 0
            && let Some(n) = nth
        {
            let total = ops::replace::count_nth_candidates(
                &original,
                &old,
                compiled_re.as_ref(),
                whole_line,
                parsed_range,
            );
            if total > 0 && n > total {
                return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                    msg: format!(
                        "nth {n} is out of range: pattern matches {total} time{}",
                        if total == 1 { "" } else { "s" }
                    ),
                }));
            }
        }

        // Fuzzy/context fallback: when exact match fails and fuzzy or context
        // is enabled, try resolve_with_fallback for anchor/similarity matching
        // (mirrors tx engine fallback in replace_op.rs).
        if count == 0 && !is_regex && (fuzzy || before_context.is_some() || after_context.is_some())
        {
            use crate::fallback;
            match fallback::resolve_with_fallback_skip_exact(
                &original,
                &old,
                before_context.as_deref(),
                after_context.as_deref(),
                // Primary path already applied word_boundary exact; bare find
                // would re-accept substrings rejected by \b (#1755).
                word_boundary,
            ) {
                Ok(anchor) => {
                    let (match_mode, default_score) =
                        super::match_mode_from_strategy(anchor.strategy);
                    let score = anchor.score.or(default_score);
                    if match_mode == MatchMode::Fuzzy
                        && let Some(min) = min_fuzzy_score
                        && crate::fallback::fuzzy_fails_min_floor(score, min)
                    {
                        // Floor reject is a soft miss: honor if_exists like resolve Err.
                        if if_exists {
                            return Ok(super::build_edit_result(
                                &path_str,
                                original.clone(),
                                original,
                                false,
                                "replace",
                                None,
                            ));
                        }
                        let actual = score
                            .map(|s| format!("{s:.3}"))
                            .unwrap_or_else(|| "none".into());
                        return Err(anyhow::Error::new(crate::exit::NoMatchError {
                            msg: format!(
                                "fuzzy match score {actual} below min_fuzzy_score {min} for {old:?}"
                            ),
                        }));
                    }
                    if crate::fallback::should_refuse_fuzzy_absent_old(
                        fuzzy,
                        match_mode == MatchMode::Fuzzy,
                        allow_absent_old,
                    ) {
                        // #1758: exact old absent — do not rewrite a different live span.
                        let msg = crate::fallback::fuzzy_absent_old_refuse_message(
                            &old,
                            &anchor.matched_text,
                            score,
                        );
                        if if_exists {
                            let mut result = super::build_edit_result(
                                &path_str,
                                original.clone(),
                                original,
                                false,
                                "replace",
                                None,
                            );
                            result.match_mode = Some(match_mode);
                            result.match_score = score;
                            result.matched_text = Some(anchor.matched_text.clone());
                            return Ok(result);
                        }
                        return Err(anyhow::Error::new(crate::exit::NoMatchError { msg }));
                    }
                    let to_text = if let Some(ib) = &insert_before {
                        let ib = ops::replace::normalize_line_insert(
                            &original,
                            &anchor.matched_text,
                            ib,
                            ops::replace::InsertSide::Before,
                        );
                        format!("{}{}", ib, anchor.matched_text)
                    } else if let Some(ia) = &insert_after {
                        let ia = ops::replace::normalize_line_insert(
                            &original,
                            &anchor.matched_text,
                            ia,
                            ops::replace::InsertSide::After,
                        );
                        format!("{}{}", anchor.matched_text, ia)
                    } else {
                        new_text.as_deref().unwrap_or("").to_string()
                    };
                    let fb_content = format!(
                        "{}{}{}",
                        &original[..anchor.start_offset],
                        to_text,
                        &original[anchor.start_offset + anchor.matched_text.len()..],
                    );
                    let policy = crate::write::WritePolicy::default();
                    let (applied, backup_session) =
                        super::write_if_apply(path, &fb_content, mode, &policy, guard)?;
                    let mut result = super::build_edit_result(
                        &path_str, original, fb_content, applied, "replace", None,
                    );
                    result.match_count = 1;
                    result.match_mode = Some(match_mode);
                    result.match_score = score;
                    result.matched_text = Some(anchor.matched_text.clone());
                    result.backup_session = backup_session;
                    return Ok(result);
                }
                Err(edit_error) => {
                    if if_exists {
                        return Ok(super::build_edit_result(
                            &path_str,
                            original.clone(),
                            original,
                            false,
                            "replace",
                            None,
                        ));
                    }
                    let similar = fallback::find_similar_targets(&original, &old, 3);
                    let mut msg = format!("no matches for {old:?}");
                    if let Some(suggestion) = &edit_error.suggestion {
                        msg.push_str(&format!(" (suggestion: {suggestion})"));
                    }
                    if !similar.is_empty() {
                        msg.push_str(&format!(" (did you mean: {}?)", similar.join(", ")));
                    }
                    return Err(anyhow::Error::new(crate::exit::NoMatchError { msg }));
                }
            }
        }

        if count == 0 && if_exists {
            return Ok(super::build_edit_result(
                &path_str,
                original.clone(),
                original,
                false,
                "replace",
                None,
            ));
        }

        let policy = crate::write::WritePolicy::default();
        let (applied, backup_session) =
            super::write_if_apply(path, &new_content, mode, &policy, guard)?;
        let mut result =
            super::build_edit_result(&path_str, original, new_content, applied, "replace", None);
        result.match_count = count;
        if count > 0 {
            result.match_mode = Some(MatchMode::Exact);
        }
        result.backup_session = backup_session;
        Ok(result)
    } else {
        bail!("expected Replace operation")
    }
}

/// Replace text in a content string (no disk I/O).
///
/// Applies the same replacement logic as [`replace_text`] but operates on an
/// in-memory string instead of a file path. Supports all [`ReplaceOptions`]
/// features: regex, word boundary, nth, case insensitive, multiline,
/// insert_before/after, whole_line, range, if_exists, unique, fuzzy,
/// before_context, after_context, and [`ReplaceOptions::refuse_suspicious_fuzzy`].
///
/// When [`ReplaceOptions::refuse_suspicious_fuzzy`] is true and a **changed**
/// fuzzy match trips default [`super::fuzzy_span_suspicious`], returns
/// [`EditErrorKind::FuzzySpanSuspicious`] (#2005). Exact/anchored matches and
/// soft unchanged (`if_exists`) results are not refused.
pub fn replace_in_content(
    content: &str,
    from: &str,
    to: &str,
    opts: &ReplaceOptions,
) -> anyhow::Result<ContentEditResult> {
    let result = replace_in_content_inner(content, from, to, opts)?;
    maybe_refuse_suspicious_fuzzy(from, result, opts)
}

/// Apply [`ReplaceOptions::refuse_suspicious_fuzzy`] after a successful replace (#2005).
pub(crate) fn maybe_refuse_suspicious_fuzzy(
    from: &str,
    result: ContentEditResult,
    opts: &ReplaceOptions,
) -> anyhow::Result<ContentEditResult> {
    // Only refuse when a fuzzy rewrite was actually applied. Soft if_exists
    // honesty can leave match_mode=Fuzzy with changed=false (#2005 review).
    if !opts.refuse_suspicious_fuzzy
        || !result.changed
        || result.match_mode != Some(MatchMode::Fuzzy)
    {
        return Ok(result);
    }
    if super::fuzzy_span_suspicious(from, result.matched_text.as_deref(), result.match_score) {
        let matched = result.matched_text.as_deref().unwrap_or("");
        let score = result
            .match_score
            .map(|s| format!("{s:.3}"))
            .unwrap_or_else(|| "none".into());
        return Err(crate::fallback::EditError::new(
            crate::fallback::EditErrorKind::FuzzySpanSuspicious,
            format!(
                "fuzzy span suspicious: old {:?} matched {:?} (score {score}); refuse_suspicious_fuzzy / for_agent",
                crate::fallback::truncate_str(from, 60),
                crate::fallback::truncate_str(matched, 80),
            ),
        )
        .with_suggestion(
            "use a tighter old string, disable refuse_suspicious_fuzzy, or inspect matched_text with FuzzySpanPolicy",
        )
        .into());
    }
    Ok(result)
}

fn replace_in_content_inner(
    content: &str,
    from: &str,
    to: &str,
    opts: &ReplaceOptions,
) -> anyhow::Result<ContentEditResult> {
    use crate::ops;

    let is_regex = opts.regex;
    // Match replace_text: typed InvalidInputError so edit_error_kind peels
    // invalid_input without scraping English.
    if from.is_empty() && !is_regex {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "empty search pattern".into(),
        }));
    }
    if let Some(min) = opts.min_fuzzy_score
        && (!(0.0..=1.0).contains(&min) || min.is_nan())
    {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: format!(
                "min_fuzzy_score must be in 0.0..=1.0 (got {min}); leave None for no floor"
            ),
        }));
    }
    if opts.range.is_some() && !opts.whole_line {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "range requires whole_line to be true".into(),
        }));
    }
    if opts.whole_line && opts.multiline {
        return Err(anyhow::Error::new(crate::exit::InvalidInputError {
            msg: "whole_line and multiline cannot be combined".into(),
        }));
    }

    // Shell command-position path (#1494): only rewrite invocable command tokens.
    if opts.command_position {
        if let Some(msg) = crate::ops::shell_token::command_position_combo_error(
            crate::ops::shell_token::CommandPositionIncompat {
                regex: is_regex,
                case_insensitive: opts.case_insensitive,
                word_boundary: opts.word_boundary,
                whole_line: opts.whole_line,
                multiline: opts.multiline,
                nth: opts.nth.is_some(),
                insert_before: opts.insert_before.is_some(),
                insert_after: opts.insert_after.is_some(),
                before_context: opts.before_context.is_some(),
                after_context: opts.after_context.is_some(),
                fuzzy: opts.fuzzy,
            },
        ) {
            return Err(crate::fallback::EditError::new(
                crate::fallback::EditErrorKind::InvalidInput,
                msg,
            )
            .into());
        }
        let (new_content, count) =
            crate::ops::shell_token::replace_command_position(content, from, to);
        return finalize_content_replace(content, from, new_content, count, opts);
    }

    let compiled_re = ops::replace::compile_replace_regex(
        from,
        is_regex,
        opts.case_insensitive,
        opts.multiline,
        opts.word_boundary,
    )?;

    let direct_to = if opts.insert_before.is_none() && opts.insert_after.is_none() {
        Some(to.to_string())
    } else {
        None
    };
    let replacement = ops::replace::replacement_text(
        from,
        &direct_to,
        &opts.insert_before,
        &opts.insert_after,
        compiled_re.is_some(),
        is_regex,
        content,
    );

    let parsed_range = opts.range;

    let (new_content, count) = if opts.whole_line {
        ops::replace::replace_whole_lines(
            content,
            from,
            &replacement,
            compiled_re.as_ref(),
            opts.nth,
            parsed_range,
        )
    } else {
        ops::replace::replace_content(content, from, &replacement, compiled_re.as_ref(), opts.nth)
    };

    if count == 0
        && let Some(n) = opts.nth
    {
        let total = ops::replace::count_nth_candidates(
            content,
            from,
            compiled_re.as_ref(),
            opts.whole_line,
            parsed_range,
        );
        if total > 0 && n > total {
            return Err(anyhow::Error::new(crate::exit::InvalidInputError {
                msg: format!(
                    "nth {n} is out of range: pattern matches {total} time{}",
                    if total == 1 { "" } else { "s" }
                ),
            }));
        }
    }

    // Context disambiguation (#1315): when multiple exact matches and context
    // is provided, select the match nearest to the context instead of
    // replacing all (mirrors replace_write and tx engine logic).
    // Fail closed when context does not select a unique match.
    if count > 1
        && opts.nth.is_none()
        && !opts.whole_line
        && !is_regex
        && (opts.before_context.is_some() || opts.after_context.is_some())
    {
        if let Some(target_offset) = ops::replace::context_filtered_offset(
            content,
            from,
            opts.before_context.as_deref(),
            opts.after_context.as_deref(),
        ) {
            let ctx_content = format!(
                "{}{}{}",
                &content[..target_offset],
                replacement,
                &content[target_offset + from.len()..],
            );
            let diff = super::make_diff("<content>", content, &ctx_content);
            return Ok(ContentEditResult {
                original: content.to_string(),
                new_content: ctx_content,
                diff,
                changed: true,
                match_count: 1,
                match_mode: Some(MatchMode::Anchored),
                match_score: None,
                // Exact span at the context-picked offset (equals `from`; hosts
                // still get a non-null matched_text on Anchored like fuzzy #1736).
                matched_text: Some(from.to_string()),
            });
        }
        return Err(anyhow::Error::new(crate::exit::AmbiguousError {
            msg: format!(
                "ambiguous match: pattern {:?} matches {} times; context did not select a unique occurrence (use --nth, stronger before/after-context (replace), or --allow-non-unique (apply-fragment))",
                crate::fallback::truncate_str(from, 60),
                count
            ),
        }));
    }

    let new_content = new_content.into_owned();

    // Fuzzy/context fallback (#1286, #1315): when exact match fails and fuzzy
    // or context is enabled, try resolve_with_fallback for anchor/similarity
    // matching (matches replace_write and tx engine behavior).
    // Ambiguous (unique + count>1) is finalized below so command_position and
    // the normal path share one check in finalize_content_replace.
    if count == 0
        && !is_regex
        && (opts.fuzzy || opts.before_context.is_some() || opts.after_context.is_some())
    {
        use crate::fallback;
        match fallback::resolve_with_fallback_skip_exact(
            content,
            from,
            opts.before_context.as_deref(),
            opts.after_context.as_deref(),
            // Primary path already applied word_boundary exact; bare find
            // would re-accept substrings rejected by \b (#1755).
            opts.word_boundary,
        ) {
            Ok(anchor) => {
                let (mode, default_score) = super::match_mode_from_strategy(anchor.strategy);
                let score = anchor.score.or(default_score);
                if mode == super::MatchMode::Fuzzy
                    && let Some(min) = opts.min_fuzzy_score
                    && crate::fallback::fuzzy_fails_min_floor(score, min)
                {
                    // Floor reject is a soft miss: honor if_exists like resolve Err
                    // (#1750). Without if_exists, still hard NoMatch.
                    if opts.if_exists {
                        return Ok(ContentEditResult {
                            original: content.to_string(),
                            new_content: content.to_string(),
                            diff: String::new(),
                            changed: false,
                            match_count: 0,
                            match_mode: None,
                            match_score: None,
                            matched_text: None,
                        });
                    }
                    let actual = score
                        .map(|s| format!("{s:.3}"))
                        .unwrap_or_else(|| "none".into());
                    return Err(crate::fallback::EditError::new(
                        crate::fallback::EditErrorKind::NoMatch,
                        format!(
                            "fuzzy match score {actual} below min_fuzzy_score {min} for {:?}",
                            from
                        ),
                    )
                    .into());
                }
                if crate::fallback::should_refuse_fuzzy_absent_old(
                    opts.fuzzy,
                    mode == super::MatchMode::Fuzzy,
                    opts.allow_absent_old,
                ) {
                    // #1758: report candidate honesty without rewriting a different live span.
                    let msg = crate::fallback::fuzzy_absent_old_refuse_message(
                        from,
                        &anchor.matched_text,
                        score,
                    );
                    if opts.if_exists {
                        return Ok(ContentEditResult {
                            original: content.to_string(),
                            new_content: content.to_string(),
                            diff: String::new(),
                            changed: false,
                            match_count: 0,
                            match_mode: Some(mode),
                            match_score: score,
                            matched_text: Some(anchor.matched_text.clone()),
                        });
                    }
                    return Err(crate::fallback::EditError::new(
                        crate::fallback::EditErrorKind::NoMatch,
                        msg,
                    )
                    .into());
                }
                let to_text = if let Some(ib) = &opts.insert_before {
                    let ib = ops::replace::normalize_line_insert(
                        content,
                        &anchor.matched_text,
                        ib,
                        ops::replace::InsertSide::Before,
                    );
                    format!("{}{}", ib, anchor.matched_text)
                } else if let Some(ia) = &opts.insert_after {
                    let ia = ops::replace::normalize_line_insert(
                        content,
                        &anchor.matched_text,
                        ia,
                        ops::replace::InsertSide::After,
                    );
                    format!("{}{}", anchor.matched_text, ia)
                } else {
                    to.to_string()
                };
                let fuzzy_content = format!(
                    "{}{}{}",
                    &content[..anchor.start_offset],
                    to_text,
                    &content[anchor.start_offset + anchor.matched_text.len()..]
                );
                let diff = super::make_diff("<content>", content, &fuzzy_content);
                return Ok(ContentEditResult {
                    original: content.to_string(),
                    new_content: fuzzy_content,
                    diff,
                    changed: true,
                    match_count: 1,
                    match_mode: Some(mode),
                    match_score: score,
                    matched_text: Some(anchor.matched_text.clone()),
                });
            }
            Err(edit_error) => {
                if opts.if_exists {
                    return Ok(ContentEditResult {
                        original: content.to_string(),
                        new_content: content.to_string(),
                        diff: String::new(),
                        changed: false,
                        match_count: 0,
                        match_mode: None,
                        match_score: None,

                        matched_text: None,
                    });
                }
                let similar = fallback::find_similar_targets(content, from, 3);
                let mut msg = format!("no matches for {:?}", from);
                if let Some(suggestion) = &edit_error.suggestion {
                    msg.push_str(&format!(" (suggestion: {})", suggestion));
                }
                if !similar.is_empty() {
                    msg.push_str(&format!(" (did you mean: {}?)", similar.join(", ")));
                }
                let mut err =
                    crate::fallback::EditError::new(crate::fallback::EditErrorKind::NoMatch, msg)
                        .with_similar(similar);
                if let Some(s) = edit_error.suggestion.clone() {
                    err = err.with_suggestion(s);
                }
                return Err(err.into());
            }
        }
    }

    finalize_content_replace(content, from, new_content, count, opts)
}

/// Shared zero-match / success finalization for content replace paths (#1492).
fn finalize_content_replace(
    content: &str,
    from: &str,
    new_content: String,
    count: usize,
    opts: &ReplaceOptions,
) -> anyhow::Result<ContentEditResult> {
    if opts.unique && count > 1 {
        return Err(crate::fallback::EditError::new(
            crate::fallback::EditErrorKind::AmbiguousTarget,
            format!(
                "ambiguous match: pattern {:?} matches {} times; tighten the pattern/anchor, use --nth (replace), or --allow-non-unique (apply-fragment)",
                from, count
            ),
        )
        .into());
    }

    if count == 0 && opts.if_exists {
        return Ok(ContentEditResult {
            original: content.to_string(),
            new_content: content.to_string(),
            diff: String::new(),
            changed: false,
            match_count: 0,
            match_mode: None,
            match_score: None,

            matched_text: None,
        });
    }

    if count == 0 && opts.require_change {
        let similar = crate::fallback::find_similar_targets(content, from, 3);
        let mut msg = format!("no matches for {:?}", from);
        if !similar.is_empty() {
            msg.push_str(&format!(" (did you mean: {}?)", similar.join(", ")));
        }
        return Err(
            crate::fallback::EditError::new(crate::fallback::EditErrorKind::NoMatch, msg)
                .with_similar(similar)
                .into(),
        );
    }

    let changed = content != new_content;
    let diff = if changed {
        super::make_diff("<content>", content, &new_content)
    } else {
        String::new()
    };

    Ok(ContentEditResult {
        original: content.to_string(),
        new_content,
        diff,
        changed,
        match_count: count,
        match_mode: if count > 0 {
            Some(MatchMode::Exact)
        } else {
            None
        },
        match_score: None,

        matched_text: None,
    })
}