cordy 0.1.7

A cross-platform TUI coding agent in Rust — hot-swap any model/provider mid-conversation, MCP, skills, sub-agents, background jobs, and an OpenCode-inspired interface.
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
//! The `apply_patch` engine: turn a patch into a set of file writes.
//!
//! Work happens in two steps. [`plan`] parses the patch, reads every file it touches, locates each
//! chunk (tolerantly — see [`seek`]) and produces the exact new contents; [`Plan::apply`] then
//! writes them. Splitting it this way means a patch is either fully valid before anything is
//! touched, or it fails with nothing half-written — and the preview shown to the user for approval
//! is the same content that will land.

pub mod parser;
pub mod seek;

use std::path::{Path, PathBuf};

use similar::TextDiff;

pub use parser::{Hunk, ParseError, UpdateFileChunk, parse_patch};

/// Why a patch could not be turned into file writes.
#[derive(Debug, Clone, PartialEq)]
pub enum PatchError {
    Parse(ParseError),
    /// The patch is fine but doesn't fit the files on disk.
    Mismatch(String),
    Io(String),
    /// A path escaped the working directory.
    Path(String),
}

impl std::fmt::Display for PatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PatchError::Parse(e) => write!(f, "{e}"),
            PatchError::Mismatch(m) | PatchError::Io(m) | PatchError::Path(m) => write!(f, "{m}"),
        }
    }
}

impl From<ParseError> for PatchError {
    fn from(e: ParseError) -> Self {
        PatchError::Parse(e)
    }
}

/// One resolved file operation, with its final contents already computed.
#[derive(Debug, Clone, PartialEq)]
pub enum FileChange {
    Add {
        path: PathBuf,
        contents: String,
    },
    Delete {
        path: PathBuf,
        original: String,
    },
    Update {
        path: PathBuf,
        /// Set when the file is also renamed; `path` is where the content is read from.
        move_to: Option<PathBuf>,
        original: String,
        contents: String,
    },
}

impl FileChange {
    /// The path the change writes to.
    pub fn target(&self) -> &Path {
        match self {
            FileChange::Add { path, .. } | FileChange::Delete { path, .. } => path,
            FileChange::Update {
                move_to: Some(dest),
                ..
            } => dest,
            FileChange::Update { path, .. } => path,
        }
    }

    /// Every path the change reads or writes — what to snapshot before applying.
    pub fn touched(&self) -> Vec<PathBuf> {
        match self {
            FileChange::Add { path, .. } | FileChange::Delete { path, .. } => vec![path.clone()],
            FileChange::Update { path, move_to, .. } => match move_to {
                Some(dest) => vec![path.clone(), dest.clone()],
                None => vec![path.clone()],
            },
        }
    }

    fn label(&self) -> String {
        match self {
            FileChange::Add { path, .. } => format!("A {}", path.display()),
            FileChange::Delete { path, .. } => format!("D {}", path.display()),
            FileChange::Update {
                path,
                move_to: Some(dest),
                ..
            } => format!("M {}{}", path.display(), dest.display()),
            FileChange::Update { path, .. } => format!("M {}", path.display()),
        }
    }

    /// Lines added and removed, for the one-line result summary.
    fn line_delta(&self) -> (usize, usize) {
        let (before, after) = match self {
            FileChange::Add { contents, .. } => ("", contents.as_str()),
            FileChange::Delete { original, .. } => (original.as_str(), ""),
            FileChange::Update {
                original, contents, ..
            } => (original.as_str(), contents.as_str()),
        };
        let diff = TextDiff::from_lines(before, after);
        let mut added = 0;
        let mut removed = 0;
        for change in diff.iter_all_changes() {
            match change.tag() {
                similar::ChangeTag::Insert => added += 1,
                similar::ChangeTag::Delete => removed += 1,
                similar::ChangeTag::Equal => {}
            }
        }
        (added, removed)
    }
}

/// A validated patch: every change resolved against the current files, nothing written yet.
#[derive(Debug, Clone, PartialEq)]
pub struct Plan {
    pub changes: Vec<FileChange>,
}

impl Plan {
    pub fn is_empty(&self) -> bool {
        self.changes.is_empty()
    }

    /// A unified diff of the whole patch, for the approval prompt.
    pub fn diff(&self) -> String {
        let mut out = String::new();
        for change in &self.changes {
            out.push_str(&change.label());
            out.push('\n');
            let (before, after) = match change {
                FileChange::Add { contents, .. } => ("", contents.as_str()),
                FileChange::Delete { original, .. } => (original.as_str(), ""),
                FileChange::Update {
                    original, contents, ..
                } => (original.as_str(), contents.as_str()),
            };
            let diff = TextDiff::from_lines(before, after);
            out.push_str(
                &diff
                    .unified_diff()
                    .context_radius(3)
                    .header("before", "after")
                    .to_string(),
            );
            if !out.ends_with('\n') {
                out.push('\n');
            }
        }
        out
    }

    /// Write every change. Callers snapshot [`FileChange::touched`] first so this stays undoable.
    pub fn apply(&self) -> Result<String, PatchError> {
        let mut lines = Vec::new();
        for change in &self.changes {
            match change {
                FileChange::Add { path, contents } => {
                    if let Some(parent) = path.parent() {
                        std::fs::create_dir_all(parent).map_err(|e| io_err(path, e))?;
                    }
                    std::fs::write(path, contents).map_err(|e| io_err(path, e))?;
                }
                FileChange::Delete { path, .. } => {
                    std::fs::remove_file(path).map_err(|e| io_err(path, e))?;
                }
                FileChange::Update {
                    path,
                    move_to,
                    contents,
                    ..
                } => {
                    let dest = move_to.as_deref().unwrap_or(path);
                    if let Some(parent) = dest.parent() {
                        std::fs::create_dir_all(parent).map_err(|e| io_err(dest, e))?;
                    }
                    std::fs::write(dest, contents).map_err(|e| io_err(dest, e))?;
                    if move_to.is_some() {
                        std::fs::remove_file(path).map_err(|e| io_err(path, e))?;
                    }
                }
            }
            let (added, removed) = change.line_delta();
            lines.push(format!("{} (+{added} -{removed})", change.label()));
        }
        Ok(lines.join("\n"))
    }
}

fn io_err(path: &Path, e: std::io::Error) -> PatchError {
    PatchError::Io(format!("{}: {e}", path.display()))
}

/// Parse `patch` and resolve it against the files under `cwd`.
///
/// Paths are resolved relative to `cwd` and must stay inside it: a patch is a bulk edit, and one
/// stray `../` would put it outside everything the permission prompt described.
pub fn plan(patch: &str, cwd: &Path) -> Result<Plan, PatchError> {
    let hunks = parse_patch(patch)?;
    let mut changes = Vec::with_capacity(hunks.len());
    for hunk in hunks {
        let change = match hunk {
            Hunk::AddFile { path, contents } => {
                let path = resolve(cwd, &path)?;
                if path.exists() {
                    return Err(PatchError::Mismatch(format!(
                        "{} already exists — use '*** Update File:' to change it",
                        path.display()
                    )));
                }
                FileChange::Add { path, contents }
            }
            Hunk::DeleteFile { path } => {
                let path = resolve(cwd, &path)?;
                let original = read(&path)?;
                FileChange::Delete { path, original }
            }
            Hunk::UpdateFile {
                path,
                move_path,
                chunks,
            } => {
                let path = resolve(cwd, &path)?;
                let move_to = move_path.map(|p| resolve(cwd, &p)).transpose()?;
                let original = read(&path)?;
                let contents = apply_chunks(&original, &chunks, &path)?;
                FileChange::Update {
                    path,
                    move_to,
                    original,
                    contents,
                }
            }
        };
        changes.push(change);
    }
    Ok(Plan { changes })
}

fn read(path: &Path) -> Result<String, PatchError> {
    std::fs::read_to_string(path).map_err(|e| {
        PatchError::Io(format!(
            "failed to read {} to patch it: {e}",
            path.display()
        ))
    })
}

/// Resolve a patch path against `cwd`, refusing anything that leaves it.
fn resolve(cwd: &Path, path: &Path) -> Result<PathBuf, PatchError> {
    if path.is_absolute() {
        return Err(PatchError::Path(format!(
            "{}: patch paths must be relative to the working directory",
            path.display()
        )));
    }
    let mut resolved = cwd.to_path_buf();
    for part in path.components() {
        match part {
            std::path::Component::Normal(p) => resolved.push(p),
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                if !resolved.pop() || !resolved.starts_with(cwd) {
                    return Err(PatchError::Path(format!(
                        "{}: patch paths must stay inside the working directory",
                        path.display()
                    )));
                }
            }
            _ => {
                return Err(PatchError::Path(format!(
                    "{}: unsupported path component",
                    path.display()
                )));
            }
        }
    }
    Ok(resolved)
}

/// Apply an update hunk's chunks to a file's contents.
fn apply_chunks(
    original: &str,
    chunks: &[UpdateFileChunk],
    path: &Path,
) -> Result<String, PatchError> {
    let mut lines: Vec<String> = original.split('\n').map(String::from).collect();
    // `split` leaves a trailing empty element for the final newline; drop it so line indexes match
    // what a diff tool would report, and re-add it at the end.
    if lines.last().is_some_and(String::is_empty) {
        lines.pop();
    }

    let replacements = compute_replacements(&lines, chunks, path)?;
    let mut new_lines = apply_replacements(lines, &replacements);
    if !new_lines.last().is_some_and(String::is_empty) {
        new_lines.push(String::new());
    }
    Ok(new_lines.join("\n"))
}

/// Locate each chunk, producing `(start, old_len, new_lines)` edits in file order.
fn compute_replacements(
    original_lines: &[String],
    chunks: &[UpdateFileChunk],
    path: &Path,
) -> Result<Vec<(usize, usize, Vec<String>)>, PatchError> {
    let mut replacements = Vec::new();
    let mut line_index = 0usize;

    for chunk in chunks {
        // `@@ context` narrows the search window before matching the chunk itself.
        if let Some(ctx) = &chunk.change_context {
            match seek::seek_sequence(
                original_lines,
                std::slice::from_ref(ctx),
                line_index,
                /*eof*/ false,
            ) {
                Some(idx) => line_index = idx + 1,
                None => {
                    return Err(PatchError::Mismatch(format!(
                        "failed to find context '{ctx}' in {}",
                        path.display()
                    )));
                }
            }
        }

        if chunk.old_lines.is_empty() {
            // A pure insertion goes at the end of the file (before a trailing blank line).
            let at = if original_lines.last().is_some_and(String::is_empty) {
                original_lines.len() - 1
            } else {
                original_lines.len()
            };
            replacements.push((at, 0, chunk.new_lines.clone()));
            continue;
        }

        let mut pattern: &[String] = &chunk.old_lines;
        let mut new_slice: &[String] = &chunk.new_lines;
        let mut found =
            seek::seek_sequence(original_lines, pattern, line_index, chunk.is_end_of_file);

        // A chunk that reaches the end of the file often carries a trailing empty line standing in
        // for the final newline, which isn't in `original_lines`. Retry without it.
        if found.is_none() && pattern.last().is_some_and(String::is_empty) {
            pattern = &pattern[..pattern.len() - 1];
            if new_slice.last().is_some_and(String::is_empty) {
                new_slice = &new_slice[..new_slice.len() - 1];
            }
            found = seek::seek_sequence(original_lines, pattern, line_index, chunk.is_end_of_file);
        }

        match found {
            Some(start) => {
                replacements.push((start, pattern.len(), new_slice.to_vec()));
                line_index = start + pattern.len();
            }
            None => {
                return Err(PatchError::Mismatch(format!(
                    "failed to find these lines in {}:\n{}",
                    path.display(),
                    chunk.old_lines.join("\n")
                )));
            }
        }
    }

    replacements.sort_by_key(|(index, _, _)| *index);
    Ok(replacements)
}

/// Splice the edits in, back to front so earlier indexes stay valid.
fn apply_replacements(
    mut lines: Vec<String>,
    replacements: &[(usize, usize, Vec<String>)],
) -> Vec<String> {
    for (start, old_len, new_segment) in replacements.iter().rev() {
        let start = *start;
        for _ in 0..*old_len {
            if start < lines.len() {
                lines.remove(start);
            }
        }
        for (offset, line) in new_segment.iter().enumerate() {
            lines.insert(start + offset, line.clone());
        }
    }
    lines
}

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

    fn write(dir: &Path, name: &str, contents: &str) {
        let path = dir.join(name);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(path, contents).unwrap();
    }

    #[test]
    fn adds_updates_and_deletes_in_one_patch() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path();
        write(cwd, "keep.txt", "one\ntwo\nthree\n");
        write(cwd, "gone.txt", "bye\n");

        let patch = "*** Begin Patch\n\
             *** Add File: new/hello.txt\n\
             +hello\n\
             *** Update File: keep.txt\n\
             @@\n\
             -two\n\
             +TWO\n\
             *** Delete File: gone.txt\n\
             *** End Patch";
        let plan = plan(patch, cwd).unwrap();
        let summary = plan.apply().unwrap();

        assert_eq!(
            std::fs::read_to_string(cwd.join("new/hello.txt")).unwrap(),
            "hello\n"
        );
        assert_eq!(
            std::fs::read_to_string(cwd.join("keep.txt")).unwrap(),
            "one\nTWO\nthree\n"
        );
        assert!(!cwd.join("gone.txt").exists());
        assert!(summary.contains("A "), "{summary}");
        assert!(summary.contains("D "), "{summary}");
    }

    #[test]
    fn updates_with_a_rename_move_the_file() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path();
        write(cwd, "src/old.rs", "fn main() {}\n");

        let patch = "*** Begin Patch\n\
             *** Update File: src/old.rs\n\
             *** Move to: src/new.rs\n\
             @@\n\
             -fn main() {}\n\
             +fn main() { println!(\"hi\"); }\n\
             *** End Patch";
        plan(patch, cwd).unwrap().apply().unwrap();

        assert!(!cwd.join("src/old.rs").exists());
        assert_eq!(
            std::fs::read_to_string(cwd.join("src/new.rs")).unwrap(),
            "fn main() { println!(\"hi\"); }\n"
        );
    }

    #[test]
    fn context_markers_disambiguate_repeated_code() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path();
        write(cwd, "a.py", "def a():\n    pass\n\ndef b():\n    pass\n");

        let patch = "*** Begin Patch\n\
             *** Update File: a.py\n\
             @@ def b():\n\
             -    pass\n\
             +    return 2\n\
             *** End Patch";
        plan(patch, cwd).unwrap().apply().unwrap();
        assert_eq!(
            std::fs::read_to_string(cwd.join("a.py")).unwrap(),
            "def a():\n    pass\n\ndef b():\n    return 2\n"
        );
    }

    #[test]
    fn drifted_context_still_matches() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path();
        // The file has trailing whitespace and a smart quote the patch doesn't reproduce.
        write(cwd, "a.rs", "let s = \u{201C}hi\u{201D};   \nlet t = 1;\n");

        let patch = "*** Begin Patch\n\
             *** Update File: a.rs\n\
             @@\n\
             \x20let s = \"hi\";\n\
             -let t = 1;\n\
             +let t = 2;\n\
             *** End Patch";
        plan(patch, cwd).unwrap().apply().unwrap();
        assert!(
            std::fs::read_to_string(cwd.join("a.rs"))
                .unwrap()
                .contains("let t = 2;")
        );
    }

    #[test]
    fn nothing_is_written_when_one_hunk_does_not_apply() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path();
        write(cwd, "a.txt", "one\n");

        let patch = "*** Begin Patch\n\
             *** Add File: b.txt\n\
             +new file\n\
             *** Update File: a.txt\n\
             @@\n\
             -this line is not there\n\
             +replacement\n\
             *** End Patch";
        let err = plan(patch, cwd).unwrap_err();
        assert!(matches!(err, PatchError::Mismatch(_)), "{err}");
        assert!(
            !cwd.join("b.txt").exists(),
            "a failed patch writes nothing at all"
        );
    }

    #[test]
    fn paths_may_not_escape_the_working_directory() {
        let dir = tempfile::tempdir().unwrap();
        let cwd = dir.path().join("project");
        std::fs::create_dir_all(&cwd).unwrap();

        for path in ["../outside.txt", "a/../../outside.txt"] {
            let patch = format!("*** Begin Patch\n*** Add File: {path}\n+nope\n*** End Patch");
            let err = plan(&patch, &cwd).unwrap_err();
            assert!(matches!(err, PatchError::Path(_)), "{path}: {err}");
        }
    }

    #[test]
    fn adding_over_an_existing_file_is_refused() {
        let dir = tempfile::tempdir().unwrap();
        write(dir.path(), "a.txt", "already here\n");
        let err = plan(
            "*** Begin Patch\n*** Add File: a.txt\n+new\n*** End Patch",
            dir.path(),
        )
        .unwrap_err();
        assert!(matches!(err, PatchError::Mismatch(_)), "{err}");
    }

    #[test]
    fn end_of_file_chunks_append_at_the_end() {
        let dir = tempfile::tempdir().unwrap();
        write(dir.path(), "a.txt", "one\ntwo\n");
        let patch = "*** Begin Patch\n\
             *** Update File: a.txt\n\
             @@\n\
             +three\n\
             *** End of File\n\
             *** End Patch";
        plan(patch, dir.path()).unwrap().apply().unwrap();
        assert_eq!(
            std::fs::read_to_string(dir.path().join("a.txt")).unwrap(),
            "one\ntwo\nthree\n"
        );
    }

    #[test]
    fn the_preview_diff_names_each_file() {
        let dir = tempfile::tempdir().unwrap();
        write(dir.path(), "a.txt", "one\n");
        let plan = plan(
            "*** Begin Patch\n*** Update File: a.txt\n@@\n-one\n+ONE\n*** End Patch",
            dir.path(),
        )
        .unwrap();
        let diff = plan.diff();
        assert!(diff.contains("M "), "{diff}");
        assert!(diff.contains("-one"), "{diff}");
        assert!(diff.contains("+ONE"), "{diff}");
    }
}