agent-file-tools 0.43.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
//! Parser for the opencode `*** Begin Patch` envelope.
//!
//! This ports the pure parsing half of `packages/opencode-plugin/src/patch-parser.ts`.

use regex::Regex;
use std::sync::OnceLock;

/// Maximum patch text size in bytes to prevent memory exhaustion.
pub const MAX_PATCH_SIZE: usize = 1024 * 1024;
/// Maximum number of file operations per patch.
pub const MAX_HUNKS: usize = 500;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Hunk {
    Add {
        path: String,
        contents: String,
    },
    Delete {
        path: String,
    },
    Update {
        path: String,
        move_path: Option<String>,
        chunks: Vec<UpdateFileChunk>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateFileChunk {
    pub old_lines: Vec<String>,
    pub new_lines: Vec<String>,
    pub change_context: Option<String>,
    pub is_end_of_file: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchHeader {
    pub file_path: String,
    pub move_path: Option<String>,
    pub next_idx: usize,
}

/// Strip a whole-input heredoc wrapper; mirrors `patch-parser.ts:33-36`.
///
/// The TypeScript regex uses a backreference for the closing delimiter. Rust's
/// `regex` crate intentionally omits backreferences, so this uses `regex` for
/// the opening heredoc syntax and then verifies the matching closing delimiter
/// manually to preserve the same anchored wrapper behavior.
pub fn strip_heredoc(input: &str) -> String {
    static OPEN_RE: OnceLock<Regex> = OnceLock::new();
    let open_re = OPEN_RE.get_or_init(|| {
        Regex::new(r#"^(?:cat\s+)?<<['"]?([A-Za-z0-9_]+)['"]?\s*\n"#)
            .expect("heredoc opening regex should compile")
    });

    let Some(captures) = open_re.captures(input) else {
        return input.to_owned();
    };
    let Some(opening) = captures.get(0) else {
        return input.to_owned();
    };
    let delimiter = captures
        .get(1)
        .expect("heredoc regex has a delimiter capture")
        .as_str();
    let rest = &input[opening.end()..];

    for (offset, _) in rest.match_indices('\n') {
        let after_newline = &rest[offset + 1..];
        let Some(after_delimiter) = after_newline.strip_prefix(delimiter) else {
            continue;
        };
        if after_delimiter.chars().all(char::is_whitespace) {
            return rest[..offset].to_owned();
        }
    }

    input.to_owned()
}

/// Parse a file-operation header line and return its path, optional move destination, and next line index.
pub fn parse_patch_header(lines: &[&str], start_idx: usize) -> Option<PatchHeader> {
    let line = *lines.get(start_idx)?;

    if let Some(path) = line.strip_prefix("*** Add File:") {
        let file_path = path.trim();
        return (!file_path.is_empty()).then(|| PatchHeader {
            file_path: file_path.to_owned(),
            move_path: None,
            next_idx: start_idx + 1,
        });
    }

    if let Some(path) = line.strip_prefix("*** Delete File:") {
        let file_path = path.trim();
        return (!file_path.is_empty()).then(|| PatchHeader {
            file_path: file_path.to_owned(),
            move_path: None,
            next_idx: start_idx + 1,
        });
    }

    if let Some(path) = line.strip_prefix("*** Update File:") {
        let file_path = path.trim();
        if file_path.is_empty() {
            return None;
        }

        let mut move_path = None;
        let mut next_idx = start_idx + 1;
        if let Some(next_line) = lines.get(next_idx) {
            if let Some(path) = next_line.strip_prefix("*** Move to:") {
                move_path = Some(path.trim().to_owned());
                next_idx += 1;
            }
        }

        return Some(PatchHeader {
            file_path: file_path.to_owned(),
            move_path,
            next_idx,
        });
    }

    None
}

/// Parse added file content by collecting every `+` line until the next patch marker.
pub fn parse_add_file_content(lines: &[&str], start_idx: usize) -> (String, usize) {
    let mut content = String::new();
    let mut i = start_idx;

    while i < lines.len() && !lines[i].starts_with("***") {
        if let Some(line) = lines[i].strip_prefix('+') {
            content.push_str(line);
            content.push('\n');
        }
        i += 1;
    }

    if content.ends_with('\n') {
        content.pop();
    }

    (content, i)
}

/// Parse `@@` update chunks into old/new line vectors and optional context anchors.
pub fn parse_update_file_chunks(lines: &[&str], start_idx: usize) -> (Vec<UpdateFileChunk>, usize) {
    let mut chunks = Vec::new();
    let mut i = start_idx;

    while i < lines.len() && !lines[i].starts_with("***") {
        if lines[i].starts_with("@@") {
            let context_line = lines[i]["@@".len()..].trim();
            i += 1;

            let mut old_lines = Vec::new();
            let mut new_lines = Vec::new();
            let mut is_end_of_file = false;

            while i < lines.len() && !lines[i].starts_with("@@") {
                let change_line = lines[i];

                if change_line == "*** End of File" {
                    is_end_of_file = true;
                    i += 1;
                    break;
                }
                if change_line.starts_with("***") {
                    break;
                }

                if let Some(content) = change_line.strip_prefix(' ') {
                    old_lines.push(content.to_owned());
                    new_lines.push(content.to_owned());
                } else if let Some(content) = change_line.strip_prefix('-') {
                    old_lines.push(content.to_owned());
                } else if let Some(content) = change_line.strip_prefix('+') {
                    new_lines.push(content.to_owned());
                }

                i += 1;
            }

            chunks.push(UpdateFileChunk {
                old_lines,
                new_lines,
                change_context: (!context_line.is_empty()).then(|| context_line.to_owned()),
                is_end_of_file,
            });
        } else {
            i += 1;
        }
    }

    (chunks, i)
}

/// Parse an opencode apply_patch envelope; mirrors `patch-parser.ts:148-201`.
///
/// The size guard uses `patch_text.len()` bytes. TypeScript uses string length,
/// but the port intentionally guards bytes so Rust bounds actual allocation size.
pub fn parse_patch(patch_text: &str) -> Result<Vec<Hunk>, String> {
    if patch_text.len() > MAX_PATCH_SIZE {
        return Err(format!(
            "Patch too large: {} bytes exceeds limit of {} bytes",
            patch_text.len(),
            MAX_PATCH_SIZE
        ));
    }

    let trimmed = patch_text.trim();
    let cleaned = strip_heredoc(trimmed);
    let lines: Vec<&str> = cleaned.split('\n').collect();
    let mut hunks = Vec::new();

    let begin_idx = lines
        .iter()
        .position(|line| line.trim() == "*** Begin Patch");
    let end_idx = lines.iter().position(|line| line.trim() == "*** End Patch");

    let (Some(begin_idx), Some(end_idx)) = (begin_idx, end_idx) else {
        return Err(
            "Invalid patch format: missing *** Begin Patch / *** End Patch markers".to_owned(),
        );
    };
    if begin_idx >= end_idx {
        return Err(
            "Invalid patch format: missing *** Begin Patch / *** End Patch markers".to_owned(),
        );
    }

    let mut i = begin_idx + 1;
    while i < end_idx {
        let Some(header) = parse_patch_header(&lines, i) else {
            i += 1;
            continue;
        };

        if hunks.len() >= MAX_HUNKS {
            return Err(format!(
                "Patch exceeds maximum of {} file operations",
                MAX_HUNKS
            ));
        }

        if lines[i].starts_with("*** Add File:") {
            let (contents, next_idx) = parse_add_file_content(&lines, header.next_idx);
            hunks.push(Hunk::Add {
                path: header.file_path,
                contents,
            });
            i = next_idx;
        } else if lines[i].starts_with("*** Delete File:") {
            hunks.push(Hunk::Delete {
                path: header.file_path,
            });
            i = header.next_idx;
        } else if lines[i].starts_with("*** Update File:") {
            let (chunks, next_idx) = parse_update_file_chunks(&lines, header.next_idx);
            hunks.push(Hunk::Update {
                path: header.file_path,
                move_path: header.move_path,
                chunks,
            });
            i = next_idx;
        } else {
            i += 1;
        }
    }

    Ok(hunks)
}

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

    fn assert_parse_error(patch: &str, expected: &str) {
        assert_eq!(parse_patch(patch).unwrap_err(), expected);
    }

    #[test]
    fn parse_patch_missing_markers_matches_patch_parser_test_4_9() {
        assert_parse_error(
            "*** Add File: hello.txt\n+hello",
            "Invalid patch format: missing *** Begin Patch / *** End Patch markers",
        );
    }

    #[test]
    fn parse_patch_empty_body_matches_patch_parser_test_11_13() {
        assert_eq!(
            parse_patch("*** Begin Patch\n*** End Patch").unwrap(),
            vec![]
        );
    }

    #[test]
    fn parse_patch_ignores_empty_add_header_matches_patch_parser_test_15_17() {
        assert_eq!(
            parse_patch("*** Begin Patch\n*** Add File:\n+hello\n*** End Patch").unwrap(),
            vec![]
        );
    }

    #[test]
    fn parse_patch_size_limit_matches_patch_parser_test_19_25() {
        let oversized_patch = "x".repeat(MAX_PATCH_SIZE + 1);
        assert_parse_error(
            &oversized_patch,
            "Patch too large: 1048577 bytes exceeds limit of 1048576 bytes",
        );
    }

    #[test]
    fn parse_patch_hunk_limit_matches_patch_parser_test_27_38() {
        let mut patch = vec!["*** Begin Patch".to_owned()];
        for index in 0..=MAX_HUNKS {
            patch.push(format!("*** Add File: file-{index}.txt"));
            patch.push(format!("+line {index}"));
        }
        patch.push("*** End Patch".to_owned());

        assert_parse_error(
            &patch.join("\n"),
            "Patch exceeds maximum of 500 file operations",
        );
    }

    #[test]
    fn parse_patch_invalid_heredoc_matches_patch_parser_test_40_56() {
        let wrapped_patch = [
            "<<EOF",
            "*** Begin Patch",
            "*** Add File: hello.txt",
            "+hello world",
            "*** End Patch",
            "NOT_EOF",
        ]
        .join("\n");

        let expected = vec![Hunk::Add {
            path: "hello.txt".to_owned(),
            contents: "hello world".to_owned(),
        }];
        assert_eq!(parse_patch(&wrapped_patch).unwrap(), expected);
        assert_eq!(
            parse_patch(&format!("prefix\n{wrapped_patch}")).unwrap(),
            expected
        );
    }

    #[test]
    fn strip_heredoc_accepts_whole_input_wrapper_from_patch_parser_source_33_36() {
        let wrapped_patch = [
            "cat <<'PATCH'",
            "*** Begin Patch",
            "*** Add File: hello.txt",
            "+hello world",
            "*** End Patch",
            "PATCH",
        ]
        .join("\n");

        assert_eq!(
            parse_patch(&wrapped_patch).unwrap(),
            vec![Hunk::Add {
                path: "hello.txt".to_owned(),
                contents: "hello world".to_owned(),
            }]
        );
    }

    #[test]
    fn parse_patch_round_trips_add_delete_update_move_from_parser_source_38_141() {
        let patch = [
            "*** Begin Patch",
            "*** Add File: src/new.txt",
            "+hello",
            "+world",
            "*** Delete File: src/old.txt",
            "*** Update File: src/edit.txt",
            "@@ function demo()",
            " const keep = true;",
            "-const value = 1;",
            "+const value = 2;",
            "*** Update File: src/from.txt",
            "*** Move to: src/to.txt",
            "@@",
            "-old",
            "+new",
            "*** End of File",
            "*** End Patch",
        ]
        .join("\n");

        assert_eq!(
            parse_patch(&patch).unwrap(),
            vec![
                Hunk::Add {
                    path: "src/new.txt".to_owned(),
                    contents: "hello\nworld".to_owned(),
                },
                Hunk::Delete {
                    path: "src/old.txt".to_owned(),
                },
                Hunk::Update {
                    path: "src/edit.txt".to_owned(),
                    move_path: None,
                    chunks: vec![UpdateFileChunk {
                        old_lines: vec![
                            "const keep = true;".to_owned(),
                            "const value = 1;".to_owned()
                        ],
                        new_lines: vec![
                            "const keep = true;".to_owned(),
                            "const value = 2;".to_owned()
                        ],
                        change_context: Some("function demo()".to_owned()),
                        is_end_of_file: false,
                    }],
                },
                Hunk::Update {
                    path: "src/from.txt".to_owned(),
                    move_path: Some("src/to.txt".to_owned()),
                    chunks: vec![UpdateFileChunk {
                        old_lines: vec!["old".to_owned()],
                        new_lines: vec!["new".to_owned()],
                        change_context: None,
                        is_end_of_file: true,
                    }],
                },
            ]
        );
    }

    #[test]
    fn parse_patch_supports_multiple_chunks_in_one_update_from_parser_source_91_141() {
        let patch = [
            "*** Begin Patch",
            "*** Update File: src/multi.txt",
            "@@ first",
            "-one",
            "+two",
            "@@ second",
            " three",
            "-four",
            "+five",
            "*** End Patch",
        ]
        .join("\n");

        assert_eq!(
            parse_patch(&patch).unwrap(),
            vec![Hunk::Update {
                path: "src/multi.txt".to_owned(),
                move_path: None,
                chunks: vec![
                    UpdateFileChunk {
                        old_lines: vec!["one".to_owned()],
                        new_lines: vec!["two".to_owned()],
                        change_context: Some("first".to_owned()),
                        is_end_of_file: false,
                    },
                    UpdateFileChunk {
                        old_lines: vec!["three".to_owned(), "four".to_owned()],
                        new_lines: vec!["three".to_owned(), "five".to_owned()],
                        change_context: Some("second".to_owned()),
                        is_end_of_file: false,
                    },
                ],
            }]
        );
    }
}