Skip to main content

lean_ctx/tools/
ctx_edit.rs

1use std::path::{Path, PathBuf};
2use std::time::{SystemTime, UNIX_EPOCH};
3
4use crate::core::cache::SessionCache;
5use crate::core::tokens::count_tokens;
6
7/// Parameters for a file edit operation: path, old/new strings, and flags.
8pub struct EditParams {
9    pub path: String,
10    pub old_string: String,
11    pub new_string: String,
12    pub replace_all: bool,
13    pub create: bool,
14    /// Optional preimage guards. If provided, ctx_edit fails if the current file preimage differs.
15    pub expected_md5: Option<String>,
16    pub expected_size: Option<u64>,
17    pub expected_mtime_ms: Option<u64>,
18    /// Optional backup before writing.
19    pub backup: bool,
20    pub backup_path: Option<String>,
21    /// Emit bounded diff evidence (redacted) by default.
22    pub evidence: bool,
23    pub diff_max_lines: usize,
24    /// Reject invalid UTF-8 by default; allow lossy reads only when explicitly enabled.
25    pub allow_lossy_utf8: bool,
26}
27
28struct ReplaceArgs<'a> {
29    content: &'a str,
30    old_str: &'a str,
31    new_str: &'a str,
32    occurrences: usize,
33    replace_all: bool,
34    old_tokens: usize,
35    new_tokens: usize,
36}
37
38#[derive(Clone, Debug, PartialEq, Eq)]
39struct FileFingerprint {
40    size: u64,
41    mtime_ms: u64,
42    md5: String,
43}
44
45#[derive(Clone, Debug)]
46struct FilePreimage {
47    fp: FileFingerprint,
48    permissions: std::fs::Permissions,
49    bytes: Vec<u8>,
50    text: String,
51    uses_crlf: bool,
52}
53
54fn system_time_to_millis(t: SystemTime) -> u64 {
55    t.duration_since(UNIX_EPOCH)
56        .map_or(0, |d| d.as_millis() as u64)
57}
58
59fn read_file_bytes_limited(
60    path: &Path,
61    cap: usize,
62) -> Result<(Vec<u8>, std::fs::Metadata), String> {
63    if let Ok(meta) = std::fs::metadata(path) {
64        if meta.len() > cap as u64 {
65            return Err(format!(
66                "ERROR: file too large ({} bytes, cap {} via LCTX_MAX_READ_BYTES): {}",
67                meta.len(),
68                cap,
69                path.display()
70            ));
71        }
72    }
73
74    let mut file = std::fs::OpenOptions::new()
75        .read(true)
76        .open(path)
77        .map_err(|e| format!("ERROR: cannot open {}: {e}", path.display()))?;
78
79    use std::io::Read;
80    let mut raw: Vec<u8> = Vec::new();
81    let mut limited = (&mut file).take((cap as u64).saturating_add(1));
82    limited
83        .read_to_end(&mut raw)
84        .map_err(|e| format!("ERROR: cannot read {}: {e}", path.display()))?;
85    if raw.len() > cap {
86        return Err(format!(
87            "ERROR: file too large (cap {} via LCTX_MAX_READ_BYTES): {}",
88            cap,
89            path.display()
90        ));
91    }
92
93    let meta = file
94        .metadata()
95        .map_err(|e| format!("ERROR: cannot stat {}: {e}", path.display()))?;
96    Ok((raw, meta))
97}
98
99fn fingerprint_from_bytes(bytes: &[u8], meta: &std::fs::Metadata) -> FileFingerprint {
100    FileFingerprint {
101        size: bytes.len() as u64,
102        mtime_ms: meta.modified().map_or(0, system_time_to_millis),
103        md5: crate::core::hasher::hash_hex(bytes),
104    }
105}
106
107fn read_preimage(path: &Path, cap: usize, allow_lossy_utf8: bool) -> Result<FilePreimage, String> {
108    let (bytes, meta) = read_file_bytes_limited(path, cap)?;
109    let permissions = meta.permissions();
110    let fp = fingerprint_from_bytes(&bytes, &meta);
111
112    let text = if allow_lossy_utf8 {
113        String::from_utf8_lossy(&bytes).into_owned()
114    } else {
115        String::from_utf8(bytes.clone()).map_err(|_| {
116            format!(
117                "ERROR: file is not valid UTF-8 (binary/encoding). Refusing to edit: {}",
118                path.display()
119            )
120        })?
121    };
122    let uses_crlf = text.contains("\r\n");
123
124    Ok(FilePreimage {
125        fp,
126        permissions,
127        bytes,
128        text,
129        uses_crlf,
130    })
131}
132
133fn verify_expected_preimage(pre: &FilePreimage, params: &EditParams) -> Result<(), String> {
134    if let Some(expected) = params.expected_size {
135        if expected != pre.fp.size {
136            return Err(format!(
137                "ERROR: preimage mismatch for {}: expected_size={}, actual_size={}",
138                params.path, expected, pre.fp.size
139            ));
140        }
141    }
142    if let Some(expected) = params.expected_mtime_ms {
143        if expected != pre.fp.mtime_ms {
144            return Err(format!(
145                "ERROR: preimage mismatch for {}: expected_mtime_ms={}, actual_mtime_ms={}",
146                params.path, expected, pre.fp.mtime_ms
147            ));
148        }
149    }
150    if let Some(expected) = params.expected_md5.as_deref() {
151        if expected != pre.fp.md5 {
152            return Err(format!(
153                "ERROR: preimage mismatch for {}: expected_md5={}, actual_md5={}",
154                params.path, expected, pre.fp.md5
155            ));
156        }
157    }
158    Ok(())
159}
160
161fn ensure_preimage_still_matches(
162    path: &Path,
163    expected: &FileFingerprint,
164    cap: usize,
165) -> Result<(), String> {
166    let (bytes, meta) = read_file_bytes_limited(path, cap)?;
167    let now = fingerprint_from_bytes(&bytes, &meta);
168    if &now != expected {
169        return Err(format!(
170            "ERROR: file changed since read (TOCTOU guard). Re-read and retry: {}\nexpected: size={}, mtime_ms={}, md5={}\nactual:   size={}, mtime_ms={}, md5={}",
171            path.display(),
172            expected.size,
173            expected.mtime_ms,
174            expected.md5,
175            now.size,
176            now.mtime_ms,
177            now.md5
178        ));
179    }
180    Ok(())
181}
182
183fn default_backup_path(path: &Path) -> Option<PathBuf> {
184    let parent = path.parent()?;
185    let filename = path.file_name()?.to_string_lossy();
186    let pid = std::process::id();
187    let nanos = SystemTime::now()
188        .duration_since(UNIX_EPOCH)
189        .map_or(0, |d| d.as_nanos());
190    Some(parent.join(format!("{filename}.lean-ctx.bak.{pid}.{nanos}")))
191}
192
193fn write_atomic_bytes_with_permissions(
194    path: &Path,
195    bytes: &[u8],
196    permissions: Option<&std::fs::Permissions>,
197) -> Result<(), String> {
198    if let Some(parent) = path.parent() {
199        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
200    }
201
202    let parent = path
203        .parent()
204        .ok_or_else(|| "invalid path (no parent directory)".to_string())?;
205    let filename = path
206        .file_name()
207        .ok_or_else(|| "invalid path (no filename)".to_string())?
208        .to_string_lossy();
209
210    let pid = std::process::id();
211    let nanos = SystemTime::now()
212        .duration_since(UNIX_EPOCH)
213        .map_or(0, |d| d.as_nanos());
214    let tmp = parent.join(format!(".{filename}.lean-ctx.tmp.{pid}.{nanos}"));
215
216    {
217        use std::io::Write;
218        let mut f = std::fs::OpenOptions::new()
219            .write(true)
220            .create_new(true)
221            .open(&tmp)
222            .map_err(|e| format!("ERROR: cannot write {}: {e}", tmp.display()))?;
223        f.write_all(bytes)
224            .map_err(|e| format!("ERROR: cannot write {}: {e}", tmp.display()))?;
225        let _ = f.flush();
226        let _ = f.sync_all();
227    }
228
229    if let Some(perms) = permissions {
230        let _ = std::fs::set_permissions(&tmp, perms.clone());
231    }
232
233    #[cfg(windows)]
234    {
235        if path.exists() {
236            let _ = std::fs::remove_file(path);
237        }
238    }
239
240    std::fs::rename(&tmp, path).map_err(|e| {
241        format!(
242            "ERROR: atomic write failed: {} (tmp: {})",
243            e,
244            tmp.to_string_lossy()
245        )
246    })?;
247
248    Ok(())
249}
250
251macro_rules! static_regex {
252    ($pattern:expr) => {{
253        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
254        RE.get_or_init(|| {
255            regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
256        })
257    }};
258}
259
260fn redact_sensitive_diff(input: &str) -> String {
261    let patterns: Vec<(&str, &regex::Regex)> = vec![
262        (
263            "Bearer token",
264            static_regex!(r"(?i)(bearer\s+)[a-zA-Z0-9\-_\.]{8,}"),
265        ),
266        (
267            "Authorization header",
268            static_regex!(r"(?i)(authorization:\s*(?:basic|bearer|token)\s+)[^\s\r\n]+"),
269        ),
270        (
271            "API key param",
272            static_regex!(
273                r#"(?i)((?:api[_-]?key|apikey|access[_-]?key|secret[_-]?key|token|password|passwd|pwd|secret)\s*[=:]\s*)[^\s\r\n,;&"']+"#
274            ),
275        ),
276        ("AWS key", static_regex!(r"(AKIA[0-9A-Z]{12,})")),
277        (
278            "Private key block",
279            static_regex!(
280                r"(?s)(-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----).+?(-----END\s+(?:RSA\s+)?PRIVATE\s+KEY-----)"
281            ),
282        ),
283        (
284            "GitHub token",
285            static_regex!(r"(gh[pousr]_)[a-zA-Z0-9]{20,}"),
286        ),
287        (
288            "Generic long secret",
289            static_regex!(
290                r#"(?i)(?:key|token|secret|password|credential|auth)\s*[=:]\s*['"]?([a-zA-Z0-9+/=\-_]{32,})['"]?"#
291            ),
292        ),
293    ];
294
295    let mut out = input.to_string();
296    for (label, re) in &patterns {
297        out = re
298            .replace_all(&out, |caps: &regex::Captures| {
299                if let Some(prefix) = caps.get(1) {
300                    format!("{}[REDACTED:{}]", prefix.as_str(), label)
301                } else {
302                    format!("[REDACTED:{label}]")
303                }
304            })
305            .to_string();
306    }
307    out
308}
309
310fn build_diff_evidence(old: &str, new: &str, label: &str, max_lines: usize) -> String {
311    let diff = similar::TextDiff::from_lines(old, new)
312        .unified_diff()
313        .context_radius(3)
314        .header(label, label)
315        .to_string();
316    let diff = redact_sensitive_diff(&diff);
317
318    let mut out = String::new();
319    for (i, line) in diff.lines().enumerate() {
320        if i >= max_lines {
321            out.push_str(&format!("\n... diff truncated (max_lines={max_lines})"));
322            break;
323        }
324        out.push_str(line);
325        out.push('\n');
326    }
327    out.trim_end_matches('\n').to_string()
328}
329
330/// A cache mutation that an edit needs *after* its disk I/O completes.
331///
332/// Decoupling the cache mutation from the I/O lets the MCP layer perform the
333/// (slow) file read/replace/write while holding only a cheap per-file lock, then
334/// touch the shared cache for a sub-millisecond instant — instead of holding the
335/// global cache write-lock across all disk I/O (the root cause of issue #320).
336pub enum CacheEffect {
337    /// No cache change required (e.g. the edit failed before writing).
338    None,
339    /// The file on disk changed; drop the stale cache entry.
340    Invalidate,
341    /// Auto-escalation re-read full content that should be stored and marked
342    /// as fully delivered.
343    StoreFull(String),
344}
345
346/// Performs a string replacement edit on a file with CRLF/LF and whitespace
347/// tolerance. Thin wrapper that runs the I/O and applies the resulting cache
348/// effect to `cache` in one shot (used by tests and any in-process caller that
349/// already holds the cache exclusively).
350pub fn handle(cache: &mut SessionCache, params: &EditParams) -> String {
351    let last_mode = cache
352        .get(&params.path)
353        .map(|e| e.last_mode.clone())
354        .unwrap_or_default();
355    let (text, effect) = run_io(params, &last_mode);
356    apply_cache_effect(cache, &params.path, effect);
357    text
358}
359
360/// Applies a deferred [`CacheEffect`] to the session cache.
361pub fn apply_cache_effect(cache: &mut SessionCache, path: &str, effect: CacheEffect) {
362    match effect {
363        CacheEffect::None => {}
364        CacheEffect::Invalidate => {
365            cache.invalidate(path);
366        }
367        CacheEffect::StoreFull(content) => {
368            cache.store(path, &content);
369            cache.mark_full_delivered(path);
370        }
371    }
372}
373
374/// Performs the full edit on disk **without** touching the session cache, and
375/// reports back the [`CacheEffect`] the caller should apply afterwards.
376///
377/// `last_mode` is the cache's recorded read mode for the path (used only to
378/// decide whether to auto-escalate on a not-found match); pass `""` when unknown.
379pub fn run_io(params: &EditParams, last_mode: &str) -> (String, CacheEffect) {
380    let file_path = &params.path;
381
382    if params.create {
383        return handle_create(file_path, &params.new_string, params);
384    }
385
386    let cap = crate::core::limits::max_read_bytes();
387    let path = Path::new(file_path);
388    let pre = match read_preimage(path, cap, params.allow_lossy_utf8) {
389        Ok(p) => p,
390        Err(e) => return (e, CacheEffect::None),
391    };
392    if let Err(e) = verify_expected_preimage(&pre, params) {
393        return (e, CacheEffect::None);
394    }
395    let content = &pre.text;
396
397    if params.old_string.is_empty() {
398        return (
399            "ERROR: old_string must not be empty (use create=true to create a new file)".into(),
400            CacheEffect::None,
401        );
402    }
403
404    let uses_crlf = pre.uses_crlf;
405    let old_str = &params.old_string;
406    let new_str = &params.new_string;
407
408    let occurrences = content.matches(old_str).count();
409
410    if occurrences > 0 {
411        let args = ReplaceArgs {
412            content,
413            old_str,
414            new_str,
415            occurrences,
416            replace_all: params.replace_all,
417            old_tokens: count_tokens(&params.old_string),
418            new_tokens: count_tokens(&params.new_string),
419        };
420        return do_replace(path, &pre, params, cap, &args);
421    }
422
423    // Direct match failed -- try CRLF/LF normalization
424    if uses_crlf && !old_str.contains('\r') {
425        let old_crlf = old_str.replace('\n', "\r\n");
426        let occ = content.matches(&old_crlf).count();
427        if occ > 0 {
428            let new_crlf = new_str.replace('\n', "\r\n");
429            let args = ReplaceArgs {
430                content,
431                old_str: &old_crlf,
432                new_str: &new_crlf,
433                occurrences: occ,
434                replace_all: params.replace_all,
435                old_tokens: count_tokens(&params.old_string),
436                new_tokens: count_tokens(&params.new_string),
437            };
438            return do_replace(path, &pre, params, cap, &args);
439        }
440    } else if !uses_crlf && old_str.contains("\r\n") {
441        let old_lf = old_str.replace("\r\n", "\n");
442        let occ = content.matches(&old_lf).count();
443        if occ > 0 {
444            let new_lf = new_str.replace("\r\n", "\n");
445            let args = ReplaceArgs {
446                content,
447                old_str: &old_lf,
448                new_str: &new_lf,
449                occurrences: occ,
450                replace_all: params.replace_all,
451                old_tokens: count_tokens(&params.old_string),
452                new_tokens: count_tokens(&params.new_string),
453            };
454            return do_replace(path, &pre, params, cap, &args);
455        }
456    }
457
458    // Still not found -- try trimmed trailing whitespace per line
459    let normalized_content = trim_trailing_per_line(content);
460    let normalized_old = trim_trailing_per_line(old_str);
461    if !normalized_old.is_empty() && normalized_content.contains(&normalized_old) {
462        let line_sep = if uses_crlf { "\r\n" } else { "\n" };
463        let adapted_new = adapt_new_string_to_line_sep(new_str, line_sep);
464        let adapted_old = find_original_span(content, &normalized_old);
465        if let Some(original_match) = adapted_old {
466            let occ = content.matches(&original_match).count();
467            let args = ReplaceArgs {
468                content,
469                old_str: &original_match,
470                new_str: &adapted_new,
471                occurrences: occ,
472                replace_all: params.replace_all,
473                old_tokens: count_tokens(&params.old_string),
474                new_tokens: count_tokens(&params.new_string),
475            };
476            return do_replace(path, &pre, params, cap, &args);
477        }
478    }
479
480    let preview = if old_str.len() > 80 {
481        format!("{}...", &old_str[..old_str.floor_char_boundary(77)])
482    } else {
483        old_str.clone()
484    };
485    let hint = if uses_crlf {
486        " (file uses CRLF line endings)"
487    } else {
488        ""
489    };
490
491    let (escalation, effect) = auto_escalate_reread(last_mode, file_path);
492
493    (
494        format!(
495            "ERROR: old_string not found in {file_path}{hint}. \
496             Make sure it matches exactly (including whitespace/indentation).\n\
497             Searched for: {preview}{escalation}"
498        ),
499        effect,
500    )
501}
502
503/// Auto-escalation: when old_string is not found and the file was previously read
504/// in a compressed mode, re-read in full and return the content so the agent
505/// can immediately retry with the correct old_string. Returns the text to append
506/// plus the [`CacheEffect`] the caller should apply (store full content).
507fn auto_escalate_reread(last_mode: &str, path: &str) -> (String, CacheEffect) {
508    if last_mode.is_empty() || last_mode == "full" {
509        return (String::new(), CacheEffect::None);
510    }
511
512    let Ok(fresh_content) = std::fs::read_to_string(path) else {
513        return (String::new(), CacheEffect::None);
514    };
515
516    let line_count = fresh_content.lines().count();
517    const MAX_LINES: usize = 300;
518
519    let content_preview = if line_count <= MAX_LINES {
520        fresh_content.clone()
521    } else {
522        let lines: Vec<&str> = fresh_content.lines().collect();
523        let head = &lines[..MAX_LINES / 2];
524        let tail = &lines[line_count - MAX_LINES / 2..];
525        let omitted = line_count - MAX_LINES;
526        format!(
527            "{}\n[... {omitted} lines omitted ...]\n{}",
528            head.join("\n"),
529            tail.join("\n")
530        )
531    };
532
533    (
534        format!(
535            "\n\n[auto-escalation] Last read used mode=\"{last_mode}\". \
536             Full content ({line_count}L) below — retry edit with exact text from here:\n\n{content_preview}"
537        ),
538        CacheEffect::StoreFull(fresh_content),
539    )
540}
541
542fn do_replace(
543    path: &Path,
544    pre: &FilePreimage,
545    params: &EditParams,
546    cap: usize,
547    args: &ReplaceArgs<'_>,
548) -> (String, CacheEffect) {
549    if args.occurrences > 1 && !args.replace_all {
550        return (
551            format!(
552                "ERROR: old_string found {} times in {}. \
553                 Use replace_all=true to replace all, or provide more context to make old_string unique."
554                ,
555                args.occurrences,
556                path.display()
557            ),
558            CacheEffect::None,
559        );
560    }
561
562    let new_content = if args.replace_all {
563        args.content.replace(args.old_str, args.new_str)
564    } else {
565        args.content.replacen(args.old_str, args.new_str, 1)
566    };
567
568    if let Err(e) = ensure_preimage_still_matches(path, &pre.fp, cap) {
569        return (e, CacheEffect::None);
570    }
571
572    let backup_path = if params.backup {
573        let bp = params
574            .backup_path
575            .as_deref()
576            .map(PathBuf::from)
577            .or_else(|| default_backup_path(path));
578        let Some(bp) = bp else {
579            return (
580                format!("ERROR: cannot compute backup path for {}", path.display()),
581                CacheEffect::None,
582            );
583        };
584        if let Err(e) = write_atomic_bytes_with_permissions(&bp, &pre.bytes, Some(&pre.permissions))
585        {
586            return (
587                format!("ERROR: cannot create backup {}: {e}", bp.display()),
588                CacheEffect::None,
589            );
590        }
591        Some(bp.to_string_lossy().to_string())
592    } else {
593        None
594    };
595
596    if let Err(e) =
597        write_atomic_bytes_with_permissions(path, new_content.as_bytes(), Some(&pre.permissions))
598    {
599        return (e, CacheEffect::None);
600    }
601
602    if let Ok(mut bt) = crate::core::bounce_tracker::global().lock() {
603        bt.record_edit(&params.path);
604    }
605
606    let old_lines = args.content.lines().count();
607    let new_lines = new_content.lines().count();
608    let line_delta = new_lines as i64 - old_lines as i64;
609    let delta_str = if line_delta > 0 {
610        format!("+{line_delta}")
611    } else {
612        format!("{line_delta}")
613    };
614
615    let old_tokens = args.old_tokens;
616    let new_tokens = args.new_tokens;
617
618    let replaced_str = if args.replace_all && args.occurrences > 1 {
619        format!("{} replacements", args.occurrences)
620    } else {
621        "1 replacement".into()
622    };
623
624    let short = path.file_name().map_or_else(
625        || path.to_string_lossy().to_string(),
626        |f| f.to_string_lossy().to_string(),
627    );
628
629    let post_mtime_ms = std::fs::metadata(path)
630        .ok()
631        .and_then(|m| m.modified().ok())
632        .map_or(0, system_time_to_millis);
633    let post_fp = FileFingerprint {
634        size: new_content.len() as u64,
635        mtime_ms: post_mtime_ms,
636        md5: crate::core::hasher::hash_hex(new_content.as_bytes()),
637    };
638
639    let mut out = format!(
640        "✓ {short}: {replaced_str}, {delta_str} lines ({old_tokens}→{new_tokens} tok)\n\
641preimage: bytes={}, mtime_ms={}, md5={}\n\
642postimage: bytes={}, mtime_ms={}, md5={}",
643        pre.fp.size, pre.fp.mtime_ms, pre.fp.md5, post_fp.size, post_fp.mtime_ms, post_fp.md5
644    );
645    if let Some(bp) = backup_path {
646        out.push_str(&format!("\nbackup: {bp}"));
647    }
648    if params.evidence {
649        let diff = build_diff_evidence(args.content, &new_content, &short, params.diff_max_lines);
650        out.push_str("\n\nevidence (diff, redacted, bounded):\n```diff\n");
651        out.push_str(&diff);
652        out.push_str("\n```");
653    }
654    (out, CacheEffect::Invalidate)
655}
656
657fn handle_create(file_path: &str, content: &str, params: &EditParams) -> (String, CacheEffect) {
658    let path = Path::new(file_path);
659    let cap = crate::core::limits::max_read_bytes();
660
661    let mut preimage: Option<FilePreimage> = None;
662    if path.exists() {
663        let pre = match read_preimage(path, cap, params.allow_lossy_utf8) {
664            Ok(p) => p,
665            Err(e) => return (e, CacheEffect::None),
666        };
667        if let Err(e) = verify_expected_preimage(&pre, params) {
668            return (e, CacheEffect::None);
669        }
670        if let Err(e) = ensure_preimage_still_matches(path, &pre.fp, cap) {
671            return (e, CacheEffect::None);
672        }
673        preimage = Some(pre);
674    }
675
676    if let Some(parent) = path.parent() {
677        if !parent.exists() {
678            if let Err(e) = std::fs::create_dir_all(parent) {
679                return (
680                    format!("ERROR: cannot create directory {}: {e}", parent.display()),
681                    CacheEffect::None,
682                );
683            }
684        }
685    }
686
687    let backup_path = if params.backup {
688        if let Some(pre) = &preimage {
689            let bp = params
690                .backup_path
691                .as_deref()
692                .map(PathBuf::from)
693                .or_else(|| default_backup_path(path));
694            let Some(bp) = bp else {
695                return (
696                    format!("ERROR: cannot compute backup path for {}", path.display()),
697                    CacheEffect::None,
698                );
699            };
700            if let Err(e) =
701                write_atomic_bytes_with_permissions(&bp, &pre.bytes, Some(&pre.permissions))
702            {
703                return (
704                    format!("ERROR: cannot create backup {}: {e}", bp.display()),
705                    CacheEffect::None,
706                );
707            }
708            Some(bp.to_string_lossy().to_string())
709        } else {
710            None
711        }
712    } else {
713        None
714    };
715
716    let perms = preimage.as_ref().map(|p| &p.permissions);
717    if let Err(e) = write_atomic_bytes_with_permissions(path, content.as_bytes(), perms) {
718        return (e, CacheEffect::None);
719    }
720
721    let lines = content.lines().count();
722    let tokens = count_tokens(content);
723    let short = path.file_name().map_or_else(
724        || path.to_string_lossy().to_string(),
725        |f| f.to_string_lossy().to_string(),
726    );
727
728    let mut out = format!("✓ created {short}: {lines} lines, {tokens} tok");
729    if let Some(bp) = backup_path {
730        out.push_str(&format!("\nbackup: {bp}"));
731    }
732    (out, CacheEffect::Invalidate)
733}
734
735fn trim_trailing_per_line(s: &str) -> String {
736    s.lines().map(str::trim_end).collect::<Vec<_>>().join("\n")
737}
738
739fn adapt_new_string_to_line_sep(s: &str, sep: &str) -> String {
740    let normalized = s.replace("\r\n", "\n");
741    if sep == "\r\n" {
742        normalized.replace('\n', "\r\n")
743    } else {
744        normalized
745    }
746}
747
748/// Find the original (un-trimmed) span in `content` that matches `normalized_needle`
749/// after trailing-whitespace trimming per line.
750fn find_original_span(content: &str, normalized_needle: &str) -> Option<String> {
751    let needle_lines: Vec<&str> = normalized_needle.lines().collect();
752    if needle_lines.is_empty() {
753        return None;
754    }
755
756    let content_lines: Vec<&str> = content.lines().collect();
757
758    'outer: for start in 0..content_lines.len() {
759        if start + needle_lines.len() > content_lines.len() {
760            break;
761        }
762        for (i, nl) in needle_lines.iter().enumerate() {
763            if content_lines[start + i].trim_end() != *nl {
764                continue 'outer;
765            }
766        }
767        let sep = if content.contains("\r\n") {
768            "\r\n"
769        } else {
770            "\n"
771        };
772        return Some(content_lines[start..start + needle_lines.len()].join(sep));
773    }
774    None
775}
776
777#[cfg(test)]
778mod tests {
779    use super::*;
780    use std::io::Write;
781    use tempfile::NamedTempFile;
782
783    fn make_temp(content: &str) -> NamedTempFile {
784        let mut f = NamedTempFile::new().unwrap();
785        f.write_all(content.as_bytes()).unwrap();
786        f
787    }
788
789    fn mk_params(path: &Path, old: &str, new: &str, replace_all: bool, create: bool) -> EditParams {
790        EditParams {
791            path: path.to_string_lossy().to_string(),
792            old_string: old.to_string(),
793            new_string: new.to_string(),
794            replace_all,
795            create,
796            expected_md5: None,
797            expected_size: None,
798            expected_mtime_ms: None,
799            backup: false,
800            backup_path: None,
801            evidence: false,
802            diff_max_lines: 200,
803            allow_lossy_utf8: false,
804        }
805    }
806
807    #[test]
808    fn replace_single_occurrence() {
809        let f = make_temp("fn hello() {\n    println!(\"hello\");\n}\n");
810        let mut cache = SessionCache::new();
811        let result = handle(
812            &mut cache,
813            &mk_params(f.path(), "hello", "world", false, false),
814        );
815        assert!(result.contains("ERROR"), "should fail: 'hello' appears 2x");
816    }
817
818    #[test]
819    fn replace_all() {
820        let f = make_temp("aaa bbb aaa\n");
821        let mut cache = SessionCache::new();
822        let result = handle(&mut cache, &mk_params(f.path(), "aaa", "ccc", true, false));
823        assert!(result.contains("2 replacements"));
824        let content = std::fs::read_to_string(f.path()).unwrap();
825        assert_eq!(content, "ccc bbb ccc\n");
826    }
827
828    #[test]
829    fn not_found_error() {
830        let f = make_temp("some content\n");
831        let mut cache = SessionCache::new();
832        let result = handle(
833            &mut cache,
834            &mk_params(f.path(), "nonexistent", "x", false, false),
835        );
836        assert!(result.contains("ERROR: old_string not found"));
837    }
838
839    #[test]
840    fn create_new_file() {
841        let dir = tempfile::tempdir().unwrap();
842        let path = dir.path().join("sub/new_file.txt");
843        let mut cache = SessionCache::new();
844        let result = handle(
845            &mut cache,
846            &mk_params(&path, "", "line1\nline2\nline3\n", false, true),
847        );
848        assert!(result.contains("created new_file.txt"));
849        assert!(result.contains("3 lines"));
850        assert!(path.exists());
851    }
852
853    #[test]
854    fn unique_match_succeeds() {
855        let f = make_temp("fn main() {\n    let x = 42;\n}\n");
856        let mut cache = SessionCache::new();
857        let result = handle(
858            &mut cache,
859            &mk_params(f.path(), "let x = 42", "let x = 99", false, false),
860        );
861        assert!(result.contains("✓"));
862        assert!(result.contains("1 replacement"));
863        let content = std::fs::read_to_string(f.path()).unwrap();
864        assert!(content.contains("let x = 99"));
865    }
866
867    #[test]
868    fn crlf_file_with_lf_search() {
869        let f = make_temp("line1\r\nline2\r\nline3\r\n");
870        let mut cache = SessionCache::new();
871        let result = handle(
872            &mut cache,
873            &mk_params(f.path(), "line1\nline2", "changed1\nchanged2", false, false),
874        );
875        assert!(result.contains("✓"), "CRLF fallback should work: {result}");
876        let content = std::fs::read_to_string(f.path()).unwrap();
877        assert!(
878            content.contains("changed1\r\nchanged2"),
879            "new_string should be adapted to CRLF: {content:?}"
880        );
881        assert!(
882            content.contains("\r\nline3\r\n"),
883            "rest of file should keep CRLF: {content:?}"
884        );
885    }
886
887    #[test]
888    fn lf_file_with_crlf_search() {
889        let f = make_temp("line1\nline2\nline3\n");
890        let mut cache = SessionCache::new();
891        let result = handle(
892            &mut cache,
893            &mk_params(f.path(), "line1\r\nline2", "a\r\nb", false, false),
894        );
895        assert!(result.contains("✓"), "LF fallback should work: {result}");
896        let content = std::fs::read_to_string(f.path()).unwrap();
897        assert!(
898            content.contains("a\nb"),
899            "new_string should be adapted to LF: {content:?}"
900        );
901    }
902
903    #[test]
904    fn trailing_whitespace_tolerance() {
905        let f = make_temp("  let x = 1;  \n  let y = 2;\n");
906        let mut cache = SessionCache::new();
907        let result = handle(
908            &mut cache,
909            &mk_params(
910                f.path(),
911                "  let x = 1;\n  let y = 2;",
912                "  let x = 10;\n  let y = 20;",
913                false,
914                false,
915            ),
916        );
917        assert!(
918            result.contains("✓"),
919            "trailing whitespace tolerance should work: {result}"
920        );
921        let content = std::fs::read_to_string(f.path()).unwrap();
922        assert!(content.contains("let x = 10;"));
923        assert!(content.contains("let y = 20;"));
924    }
925
926    #[test]
927    fn crlf_with_trailing_whitespace() {
928        let f = make_temp("  const a = 1;  \r\n  const b = 2;\r\n");
929        let mut cache = SessionCache::new();
930        let result = handle(
931            &mut cache,
932            &mk_params(
933                f.path(),
934                "  const a = 1;\n  const b = 2;",
935                "  const a = 10;\n  const b = 20;",
936                false,
937                false,
938            ),
939        );
940        assert!(
941            result.contains("✓"),
942            "CRLF + trailing whitespace should work: {result}"
943        );
944        let content = std::fs::read_to_string(f.path()).unwrap();
945        assert!(content.contains("const a = 10;"));
946        assert!(content.contains("const b = 20;"));
947    }
948
949    #[test]
950    fn rejects_invalid_utf8_by_default() {
951        let mut f = NamedTempFile::new().unwrap();
952        f.write_all(&[0xff, 0xfe, 0xfd]).unwrap();
953        let mut cache = SessionCache::new();
954        let result = handle(&mut cache, &mk_params(f.path(), "a", "b", false, false));
955        assert!(
956            result.contains("not valid UTF-8"),
957            "expected utf8 rejection, got: {result}"
958        );
959    }
960
961    #[test]
962    fn allows_lossy_utf8_only_when_enabled() {
963        let mut f = NamedTempFile::new().unwrap();
964        f.write_all(&[0xff, 0xfe, 0xfd]).unwrap();
965        let mut cache = SessionCache::new();
966        let mut p = mk_params(f.path(), "a", "b", false, false);
967        p.allow_lossy_utf8 = true;
968        let result = handle(&mut cache, &p);
969        assert!(
970            !result.contains("not valid UTF-8"),
971            "lossy mode should avoid utf8 hard error, got: {result}"
972        );
973    }
974
975    #[test]
976    fn expected_md5_mismatch_fails_without_writing() {
977        let f = make_temp("aaa\n");
978        let mut cache = SessionCache::new();
979        let mut p = mk_params(f.path(), "aaa", "bbb", false, false);
980        p.expected_md5 = Some("deadbeef".to_string());
981        let result = handle(&mut cache, &p);
982        assert!(
983            result.contains("preimage mismatch"),
984            "expected preimage mismatch, got: {result}"
985        );
986        let content = std::fs::read_to_string(f.path()).unwrap();
987        assert_eq!(content, "aaa\n");
988    }
989
990    #[test]
991    fn backup_is_created_when_enabled() {
992        let f = make_temp("aaa\n");
993        let mut cache = SessionCache::new();
994        let mut p = mk_params(f.path(), "aaa", "bbb", false, false);
995        p.backup = true;
996        let out = handle(&mut cache, &p);
997        assert!(out.contains("backup:"), "expected backup path, got: {out}");
998        let bp = out
999            .lines()
1000            .find_map(|l| l.strip_prefix("backup: "))
1001            .expect("backup line");
1002        let backup_content = std::fs::read_to_string(bp).unwrap();
1003        assert_eq!(backup_content, "aaa\n");
1004        let content = std::fs::read_to_string(f.path()).unwrap();
1005        assert_eq!(content, "bbb\n");
1006    }
1007
1008    #[test]
1009    fn evidence_diff_is_emitted_when_enabled() {
1010        let f = make_temp("line1\nline2\n");
1011        let mut cache = SessionCache::new();
1012        let mut p = mk_params(f.path(), "line2", "changed2", false, false);
1013        p.evidence = true;
1014        p.diff_max_lines = 50;
1015        let out = handle(&mut cache, &p);
1016        assert!(out.contains("```diff"), "expected diff fence, got: {out}");
1017        assert!(
1018            out.contains("preimage:"),
1019            "expected preimage metadata, got: {out}"
1020        );
1021        assert!(
1022            out.contains("postimage:"),
1023            "expected postimage metadata, got: {out}"
1024        );
1025    }
1026
1027    #[test]
1028    fn detects_toctou_via_preimage_guard() {
1029        let f = make_temp("aaa\n");
1030        let cap = crate::core::limits::max_read_bytes();
1031        let pre = read_preimage(f.path(), cap, false).unwrap();
1032        std::fs::write(f.path(), "bbb\n").unwrap();
1033        let err = ensure_preimage_still_matches(f.path(), &pre.fp, cap).unwrap_err();
1034        assert!(err.contains("TOCTOU guard"), "unexpected error: {err}");
1035    }
1036
1037    /// Issue #320: run_io performs the full edit without any cache handle, so the
1038    /// MCP layer can avoid holding the global cache write-lock across disk I/O.
1039    /// A successful edit reports an Invalidate effect.
1040    #[test]
1041    fn run_io_success_reports_invalidate_effect() {
1042        let f = make_temp("fn main() {\n    let x = 42;\n}\n");
1043        let (text, effect) = run_io(
1044            &mk_params(f.path(), "let x = 42", "let x = 99", false, false),
1045            "",
1046        );
1047        assert!(text.contains("✓"), "expected success: {text}");
1048        assert!(
1049            matches!(effect, CacheEffect::Invalidate),
1050            "successful edit must invalidate the cache entry"
1051        );
1052        let content = std::fs::read_to_string(f.path()).unwrap();
1053        assert!(content.contains("let x = 99"));
1054    }
1055
1056    #[test]
1057    fn run_io_failure_reports_no_cache_effect() {
1058        let f = make_temp("some content\n");
1059        let (text, effect) = run_io(&mk_params(f.path(), "nonexistent", "x", false, false), "");
1060        assert!(text.contains("ERROR: old_string not found"));
1061        assert!(
1062            matches!(effect, CacheEffect::None),
1063            "a failed edit must not mutate the cache"
1064        );
1065    }
1066
1067    /// Issue #320: concurrent edits to *different* files must all succeed without
1068    /// serializing on any shared lock — run_io takes no cache, so there is nothing
1069    /// global to contend on.
1070    #[test]
1071    fn run_io_concurrent_edits_to_different_files_all_succeed() {
1072        use std::sync::Arc;
1073        let dir = Arc::new(tempfile::tempdir().unwrap());
1074        let n = 16;
1075        let mut paths = Vec::new();
1076        for i in 0..n {
1077            let p = dir.path().join(format!("file_{i}.txt"));
1078            std::fs::write(&p, format!("value = {i}\n")).unwrap();
1079            paths.push(p);
1080        }
1081        let barrier = Arc::new(std::sync::Barrier::new(n));
1082        let mut handles = Vec::new();
1083        for (i, p) in paths.into_iter().enumerate() {
1084            let barrier = Arc::clone(&barrier);
1085            handles.push(std::thread::spawn(move || {
1086                barrier.wait();
1087                let (text, effect) = run_io(
1088                    &mk_params(
1089                        &p,
1090                        &format!("value = {i}"),
1091                        &format!("value = {}", i + 1000),
1092                        false,
1093                        false,
1094                    ),
1095                    "",
1096                );
1097                assert!(text.contains("✓"), "edit {i} failed: {text}");
1098                assert!(matches!(effect, CacheEffect::Invalidate));
1099                (p, i)
1100            }));
1101        }
1102        for h in handles {
1103            let (p, i) = h.join().unwrap();
1104            let content = std::fs::read_to_string(&p).unwrap();
1105            assert_eq!(content, format!("value = {}\n", i + 1000));
1106        }
1107    }
1108
1109    #[test]
1110    fn run_io_escalation_reports_store_full_effect() {
1111        // A file previously read in a compressed mode ("signatures") triggers
1112        // auto-escalation when old_string is not found: the full content is
1113        // returned for re-store.
1114        let f = make_temp("line a\nline b\nline c\n");
1115        let (text, effect) = run_io(
1116            &mk_params(f.path(), "definitely-not-present", "x", false, false),
1117            "signatures",
1118        );
1119        assert!(
1120            text.contains("[auto-escalation]"),
1121            "expected escalation: {text}"
1122        );
1123        match effect {
1124            CacheEffect::StoreFull(content) => {
1125                assert!(content.contains("line a") && content.contains("line c"));
1126            }
1127            _ => panic!("escalation must report a StoreFull cache effect"),
1128        }
1129    }
1130
1131    #[test]
1132    fn apply_cache_effect_invalidate_and_store() {
1133        let f = make_temp("hello\n");
1134        let mut cache = SessionCache::new();
1135        cache.store(&f.path().to_string_lossy(), "hello\n");
1136        apply_cache_effect(
1137            &mut cache,
1138            &f.path().to_string_lossy(),
1139            CacheEffect::Invalidate,
1140        );
1141        assert!(
1142            cache.get(&f.path().to_string_lossy()).is_none(),
1143            "Invalidate must drop the entry"
1144        );
1145        apply_cache_effect(
1146            &mut cache,
1147            &f.path().to_string_lossy(),
1148            CacheEffect::StoreFull("fresh\n".to_string()),
1149        );
1150        assert!(
1151            cache.get(&f.path().to_string_lossy()).is_some(),
1152            "StoreFull must re-populate the entry"
1153        );
1154    }
1155}