agent-file-tools 0.35.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
//! Handler for the `git_conflicts` command: discover and parse merge conflict regions.
//!
//! Auto-discovers conflicted files via `git ls-files --unmerged`, parses `<<<<<<<`/`=======`/`>>>>>>>`
//! markers, and returns line-numbered conflict regions with surrounding context — the same format
//! agents see from `read`, but only the conflict areas.

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::context::AppContext;
use crate::protocol::{RawRequest, Response};

/// Number of context lines to show before and after each conflict block.
const CONTEXT_LINES: usize = 3;

/// A single parsed conflict region within a file.
struct ConflictRegion {
    /// 1-based line number of the `<<<<<<<` marker.
    start_line: usize,
    /// 1-based line number of the `>>>>>>>` marker.
    end_line: usize,
}

/// Resolve the git toplevel for `base_dir`.
fn git_toplevel(base_dir: &Path) -> Result<PathBuf, String> {
    let output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(base_dir)
        .output()
        .map_err(|e| format!("failed to run git: {}", e))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("not a git repository") {
            return Err("not a git repository".to_string());
        }
        return Err(format!("git rev-parse failed: {}", stderr.trim()));
    }

    let toplevel = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if toplevel.is_empty() {
        return Err("git rev-parse returned an empty toplevel".to_string());
    }

    Ok(PathBuf::from(toplevel))
}

/// Find all conflicted files using index-unmerged state plus tracked working-tree markers.
/// Returns the git toplevel and unique file paths relative to that toplevel.
fn discover_conflicted_files(base_dir: &Path) -> Result<(PathBuf, Vec<String>), String> {
    let toplevel = git_toplevel(base_dir)?;
    let mut files: Vec<String> = Vec::new();
    let mut seen = HashSet::new();

    let output = Command::new("git")
        .args(["ls-files", "--unmerged"])
        .current_dir(&toplevel)
        .output()
        .map_err(|e| format!("failed to run git: {}", e))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("not a git repository") {
            return Err("not a git repository".to_string());
        }
        return Err(format!("git ls-files failed: {}", stderr.trim()));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    for line in stdout.lines() {
        // Format: "<mode> <hash> <stage>\t<filename>"
        if let Some(tab_pos) = line.find('\t') {
            let filename = &line[tab_pos + 1..];
            if seen.insert(filename.to_string()) {
                files.push(filename.to_string());
            }
        }
    }

    let grep_output = Command::new("git")
        .args(["grep", "-lE", r"^(<<<<<<< |>>>>>>> )"])
        .current_dir(&toplevel)
        .output()
        .map_err(|e| format!("failed to run git: {}", e))?;

    if grep_output.status.success() {
        let stdout = String::from_utf8_lossy(&grep_output.stdout);
        for filename in stdout.lines() {
            if seen.insert(filename.to_string()) {
                files.push(filename.to_string());
            }
        }
    } else {
        let stderr = String::from_utf8_lossy(&grep_output.stderr);
        if grep_output.status.code() != Some(1) || !stderr.trim().is_empty() {
            return Err(format!("git grep failed: {}", stderr.trim()));
        }
    }

    files.sort();
    Ok((toplevel, files))
}

/// Parse a file's content and find all conflict regions (marker line numbers).
fn find_conflict_regions(content: &str) -> Vec<ConflictRegion> {
    let mut regions = Vec::new();
    let mut current_start: Option<usize> = None;

    for (idx, line) in content.lines().enumerate() {
        let line_num = idx + 1; // 1-based
        if line.starts_with("<<<<<<<") {
            current_start = Some(line_num);
        } else if line.starts_with(">>>>>>>") {
            if let Some(start) = current_start {
                regions.push(ConflictRegion {
                    start_line: start,
                    end_line: line_num,
                });
                current_start = None;
            }
        }
    }

    regions
}

/// Format conflict regions for a single file with line-numbered content and context.
fn format_file_conflicts(
    file_path: &str,
    content: &str,
    regions: &[ConflictRegion],
    context_lines: usize,
) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let total_lines = lines.len();
    let mut out = String::new();

    // File header
    let conflict_word = if regions.len() == 1 {
        "conflict"
    } else {
        "conflicts"
    };
    out.push_str(&format!(
        "── {} [{} {}] ──\n",
        file_path,
        regions.len(),
        conflict_word,
    ));

    let mut last_printed_line = 0usize;
    for (i, region) in regions.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }

        // Calculate context window (clamp to file bounds)
        let calculated_ctx_start = if region.start_line > context_lines {
            region.start_line - context_lines
        } else {
            1
        };
        let ctx_start = std::cmp::max(calculated_ctx_start, last_printed_line + 1);
        let ctx_end = std::cmp::min(region.end_line + context_lines, total_lines);

        if ctx_start > ctx_end {
            continue;
        }

        // Output lines with line numbers (matching `read` format)
        for line_num in ctx_start..=ctx_end {
            let line_content = lines.get(line_num - 1).unwrap_or(&"");
            // Right-align line numbers to match read output
            out.push_str(&format!("{:>4}: {}\n", line_num, line_content));
        }
        last_printed_line = ctx_end;
    }

    out
}

/// Handle a `git_conflicts` request.
///
/// No params required. Auto-discovers conflicted files via git and returns
/// line-numbered conflict regions with context.
///
/// Returns text output with conflict regions formatted like `read` output.
pub fn handle_git_conflicts(ctx: &AppContext, req: &RawRequest) -> Response {
    let project_root = match &ctx.config().project_root {
        Some(root) => std::path::PathBuf::from(root),
        None => std::env::current_dir().unwrap_or_default(),
    };
    let context_lines = req
        .params
        .get("context_lines")
        .and_then(|v| v.as_u64())
        .map(|v| v as usize)
        .unwrap_or(CONTEXT_LINES);
    let requested_path = req
        .params
        .get("path")
        .or_else(|| {
            req.params
                .get("params")
                .and_then(|params| params.get("path"))
        })
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|path| !path.is_empty());
    let discovery_base = if let Some(path) = requested_path {
        let path = PathBuf::from(path);
        let path = if path.is_relative() {
            project_root.join(path)
        } else {
            path
        };
        match ctx.validate_path(&req.id, &path) {
            Ok(path) => path,
            Err(resp) => return resp,
        }
    } else {
        project_root
    };

    // Discover conflicted files
    let (git_toplevel, files) = match discover_conflicted_files(&discovery_base) {
        Ok(result) => result,
        Err(e) => {
            if requested_path.is_some() && e == "not a git repository" {
                return Response::error(
                    &req.id,
                    "not_a_git_repository",
                    format!(
                        "path is not inside a git repository: {}",
                        discovery_base.display()
                    ),
                );
            }
            return Response::error(&req.id, "git_error", e);
        }
    };
    let checked_root = git_toplevel.to_string_lossy().to_string();

    if files.is_empty() {
        return Response::success(
            &req.id,
            serde_json::json!({
                "text": format!("No merge conflicts found.\nChecked repo root: {}", checked_root),
                "file_count": 0,
                "conflict_count": 0,
                "checked_root": checked_root,
            }),
        );
    }

    let mut output = String::new();
    let mut total_conflicts = 0;
    let mut files_with_conflicts = 0;

    for file_path in &files {
        let full_path = git_toplevel.join(file_path);
        let validated_path = match ctx.validate_path(&req.id, &full_path) {
            Ok(path) => path,
            Err(resp) => return resp,
        };

        // Read file content
        let content = match std::fs::read_to_string(&validated_path) {
            Ok(c) => c,
            Err(e) => {
                output.push_str(&format!("── {} [error: {}] ──\n\n", file_path, e));
                continue;
            }
        };

        // Find conflict regions
        let regions = find_conflict_regions(&content);
        if regions.is_empty() {
            // File is in unmerged state but has no conflict markers
            // (could be a deleted-vs-modified conflict)
            output.push_str(&format!(
                "── {} [unmerged — no conflict markers found] ──\n\n",
                file_path
            ));
            continue;
        }

        total_conflicts += regions.len();
        files_with_conflicts += 1;

        // Format this file's conflicts
        let formatted = format_file_conflicts(file_path, &content, &regions, context_lines);
        output.push_str(&formatted);
        output.push('\n');
    }

    // Prepend summary header
    let header = format!(
        "{} {}, {} {}\nChecked repo root: {}\n\n",
        files_with_conflicts,
        if files_with_conflicts == 1 {
            "file"
        } else {
            "files"
        },
        total_conflicts,
        if total_conflicts == 1 {
            "conflict"
        } else {
            "conflicts"
        },
        checked_root,
    );

    let text = format!("{}{}", header, output.trim_end());

    Response::success(
        &req.id,
        serde_json::json!({
            "text": text,
            "file_count": files_with_conflicts,
            "conflict_count": total_conflicts,
            "checked_root": checked_root,
        }),
    )
}

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

    #[test]
    fn test_find_conflict_regions_basic() {
        let content = r#"line 1
line 2
<<<<<<< HEAD
our change
=======
their change
>>>>>>> upstream/dev
line 8
"#;
        let regions = find_conflict_regions(content);
        assert_eq!(regions.len(), 1);
        assert_eq!(regions[0].start_line, 3);
        assert_eq!(regions[0].end_line, 7);
    }

    #[test]
    fn test_find_conflict_regions_multiple() {
        let content = r#"line 1
<<<<<<< HEAD
ours 1
=======
theirs 1
>>>>>>> dev
line 7
line 8
<<<<<<< HEAD
ours 2
=======
theirs 2
>>>>>>> dev
line 14
"#;
        let regions = find_conflict_regions(content);
        assert_eq!(regions.len(), 2);
        assert_eq!(regions[0].start_line, 2);
        assert_eq!(regions[0].end_line, 6);
        assert_eq!(regions[1].start_line, 9);
        assert_eq!(regions[1].end_line, 13);
    }

    #[test]
    fn test_find_conflict_regions_diff3() {
        let content = r#"before
<<<<<<< HEAD
our code
||||||| base
base code
=======
their code
>>>>>>> upstream
after
"#;
        let regions = find_conflict_regions(content);
        assert_eq!(regions.len(), 1);
        assert_eq!(regions[0].start_line, 2);
        assert_eq!(regions[0].end_line, 8);
    }

    #[test]
    fn test_find_conflict_regions_none() {
        let content = "no conflicts here\njust normal code\n";
        let regions = find_conflict_regions(content);
        assert_eq!(regions.len(), 0);
    }

    #[test]
    fn test_format_file_conflicts() {
        let content = r#"line 1
line 2
line 3
<<<<<<< HEAD
our change
=======
their change
>>>>>>> upstream/dev
line 9
line 10
line 11"#;
        let regions = find_conflict_regions(content);
        let output = format_file_conflicts("src/foo.ts", content, &regions, 3);

        assert!(output.contains("── src/foo.ts [1 conflict] ──"));
        assert!(output.contains("   1: line 1"));
        assert!(output.contains("   4: <<<<<<< HEAD"));
        assert!(output.contains("   5: our change"));
        assert!(output.contains("   6: ======="));
        assert!(output.contains("   7: their change"));
        assert!(output.contains("   8: >>>>>>> upstream/dev"));
        assert!(output.contains("  11: line 11"));
    }

    #[test]
    fn test_format_file_conflicts_context_clamp() {
        // Conflict at the very start of file — context shouldn't go negative
        let content = r#"<<<<<<< HEAD
ours
=======
theirs
>>>>>>> dev
line 6"#;
        let regions = find_conflict_regions(content);
        let output = format_file_conflicts("start.ts", content, &regions, 3);

        assert!(output.contains("   1: <<<<<<< HEAD"));
        assert!(output.contains("   6: line 6"));
    }

    #[test]
    fn test_format_plural_conflicts() {
        let content = r#"<<<<<<< HEAD
a
=======
b
>>>>>>> dev
middle
<<<<<<< HEAD
c
=======
d
>>>>>>> dev"#;
        let regions = find_conflict_regions(content);
        let output = format_file_conflicts("multi.ts", content, &regions, 1);

        assert!(output.contains("[2 conflicts]"));
    }
}