Skip to main content

openmoat_core/
patch.rs

1//! Files named by a Codex `apply_patch` envelope, whether Codex sends it as
2//! its own tool or the agent runs `apply_patch` in a shell.
3//!
4//! ```text
5//! *** Begin Patch
6//! *** Add File: path        (create)
7//! *** Update File: path     (edit; may be followed by `*** Move to: other`)
8//! *** Delete File: path
9//! *** End Patch
10//! ```
11//!
12//! Every named path is written (a delete or a move removes its source). A
13//! patch that names no file is passed on empty and decided as unparseable.
14//!
15//! Codex trims a file header before matching it, so ` *** Delete File: x`
16//! after an added file's lines is a header to it. A line is matched the same
17//! way here, ignoring case as well, and wherever it appears: naming a file the
18//! patch does not touch only makes the decision stricter, while missing one
19//! that Codex writes would let it through unchecked.
20
21const HEADERS: &[&str] = &[
22    "*** Add File:",
23    "*** Update File:",
24    "*** Delete File:",
25    "*** Move to:",
26];
27
28/// Every file the patch `text` writes, in the order it names them.
29#[must_use]
30pub fn writes(text: &str) -> Vec<String> {
31    let mut paths: Vec<String> = Vec::new();
32    for line in text.lines() {
33        let line = line.trim();
34        let Some(path) = HEADERS
35            .iter()
36            .find_map(|h| strip_header(line, h))
37            .map(str::trim)
38            .filter(|p| !p.is_empty())
39        else {
40            continue;
41        };
42        if !paths.iter().any(|p| p == path) {
43            paths.push(path.to_owned());
44        }
45    }
46    paths
47}
48
49/// `line` after `header`, compared without regard to ASCII case.
50fn strip_header<'a>(line: &'a str, header: &str) -> Option<&'a str> {
51    let head = line.get(..header.len())?;
52    head.eq_ignore_ascii_case(header)
53        .then(|| &line[header.len()..])
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn every_header_names_a_written_file() {
62        let patch = "*** Begin Patch\n*** Add File: src/new.rs\n+fn x() {}\n\
63                     *** Update File: src/lib.rs\n*** Move to: src/core.rs\n@@\n-a\n+b\n\
64                     *** Delete File: old.txt\r\n*** Update File: src/lib.rs\n*** End Patch\n";
65        assert_eq!(
66            writes(patch),
67            ["src/new.rs", "src/lib.rs", "src/core.rs", "old.txt"]
68        );
69    }
70
71    #[test]
72    fn added_lines_that_look_like_headers_are_content() {
73        // An added line `+*** Add File: x` starts with `+`, so it is content.
74        assert_eq!(writes("*** Add File: a\n+*** Add File: ~/.zshrc\n"), ["a"]);
75    }
76
77    #[test]
78    fn indented_and_differently_cased_headers_are_writes() {
79        // Codex trims the line after an added file's content and reads it as
80        // the next header, so the indented delete is applied.
81        let patch = "*** Begin Patch\n*** Add File: a\n+x\n  *** Delete File: ~/.ssh/id_rsa\n\
82                     \t*** update file: ~/.zshrc\n*** End Patch\n";
83        assert_eq!(writes(patch), ["a", "~/.ssh/id_rsa", "~/.zshrc"]);
84    }
85
86    #[test]
87    fn headers_outside_the_envelope_still_count() {
88        let patch = "*** Begin Patch\n*** Add File: a\n+x\n*** End Patch\n*** Delete File: b\n";
89        assert_eq!(writes(patch), ["a", "b"]);
90    }
91
92    #[test]
93    fn a_root_path_is_passed_on_for_the_engine_to_decide() {
94        assert_eq!(writes("*** Delete File: /\n"), ["/"]);
95    }
96
97    #[test]
98    fn no_headers_means_no_files() {
99        assert!(writes("just text").is_empty());
100        assert!(writes("*** Add File:   \n").is_empty());
101    }
102}