agent-file-tools 0.47.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
479
use std::collections::{BTreeMap, HashSet};
use std::env;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use crate::context::AppContext;
use crate::grep_executor::bounded_fallback_walk_files;
use crate::protocol::{RawRequest, Response};
use crate::search_index::{
    build_path_filters, has_any_project_file_from, resolve_search_scope, sort_paths_by_mtime_desc,
};

use super::multi_path::{canonical_key, resolve_path_or_multi, SearchPathResolution};

#[derive(Debug)]
struct GlobDiscovery {
    files: Vec<PathBuf>,
    walk_truncated: bool,
    source: &'static str,
    entries_visited: usize,
    walk_time: Duration,
    scope_has_files: bool,
    scope_probe: Duration,
}

const MAX_GLOB_RESULTS: usize = 100;
const GLOB_TRUNCATED_MESSAGE: &str =
    "(Results are truncated: showing first 100 results. Consider using a more specific path or pattern.)";
const MAX_FLAT_FILES: usize = 20;
const MAX_FILES_PER_DIRECTORY: usize = 7;
const MAX_DISPLAY_FILES_PER_DIRECTORY: usize = 5;
const MAX_DIRECTORY_SECTIONS: usize = 8;
const MAX_DISPLAY_DIRECTORIES: usize = 6;

pub fn handle_glob(req: &RawRequest, ctx: &AppContext) -> Response {
    let pattern = match req.params.get("pattern").and_then(|value| value.as_str()) {
        Some(pattern) => pattern,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "glob: missing required param 'pattern'",
            );
        }
    };

    if let Err(error) = build_path_filters(&[pattern.to_string()], &[]) {
        return Response::error(
            &req.id,
            "invalid_request",
            format!("glob: invalid pattern: {}", error),
        );
    }

    let project_root = ctx
        .config()
        .project_root
        .clone()
        .unwrap_or_else(|| env::current_dir().unwrap_or_default());
    let project_root = std::fs::canonicalize(&project_root).unwrap_or(project_root);
    let search_roots = match req.params.get("path").and_then(|value| value.as_str()) {
        Some(path) => match resolve_path_or_multi(
            path,
            &project_root,
            |candidate| ctx.validate_path(&req.id, candidate),
            &req.id,
        ) {
            Ok(SearchPathResolution::Single(root)) => vec![root],
            Ok(SearchPathResolution::Multi(roots)) => roots,
            Err(resp) => return resp,
        },
        None => vec![resolve_search_scope(&project_root, None).root],
    };

    // Return clear error if the search path doesn't exist
    if let Some(missing_root) = search_roots.iter().find(|root| !root.exists()) {
        return Response::error(
            &req.id,
            "path_not_found",
            format!(
                "glob: search path does not exist: {}",
                missing_root.display()
            ),
        );
    }
    let total_started = Instant::now();
    let (
        mut files,
        walk_truncated,
        source,
        entries_visited,
        walk_time,
        scope_has_files,
        scope_probe,
    ) = if search_roots.len() == 1 {
        let discovery = glob_root(
            ctx,
            &project_root,
            &search_roots[0],
            pattern,
            MAX_GLOB_RESULTS + 1,
        );
        (
            discovery.files,
            discovery.walk_truncated,
            discovery.source,
            discovery.entries_visited,
            discovery.walk_time,
            discovery.scope_has_files,
            discovery.scope_probe,
        )
    } else {
        let discoveries: Vec<GlobDiscovery> = search_roots
            .iter()
            .map(|root| glob_root(ctx, &project_root, root, pattern, MAX_GLOB_RESULTS + 1))
            .collect();
        let walk_truncated = discoveries.iter().any(|d| d.walk_truncated);
        let entries_visited = discoveries.iter().map(|d| d.entries_visited).sum();
        let walk_time = discoveries.iter().map(|d| d.walk_time).sum();
        let scope_has_files = discoveries.iter().any(|d| d.scope_has_files);
        let scope_probe = discoveries.iter().map(|d| d.scope_probe).sum();
        let source = if discoveries.iter().all(|d| d.source == "index") {
            "index"
        } else {
            "mixed/fallback"
        };
        let files = merge_glob_files(discoveries.into_iter().flat_map(|d| d.files).collect());
        (
            files,
            walk_truncated,
            source,
            entries_visited,
            walk_time,
            scope_has_files,
            scope_probe,
        )
    };
    crate::slog_debug!(
        "perf glob phases: source={} walk={:.3}ms entries_visited={} scope_probe={:.3}ms discovery_total={:.3}ms",
        source,
        walk_time.as_secs_f64() * 1000.0,
        entries_visited,
        scope_probe.as_secs_f64() * 1000.0,
        total_started.elapsed().as_secs_f64() * 1000.0,
    );
    // Keep the lexically first results so output is deterministic across filesystems.
    // Partitioning yields the same subset as sorting the entire list first while
    // avoiding a full O(n log n) sort when a broad glob returns thousands of paths.
    let total = files.len();
    let result_truncated = total > MAX_GLOB_RESULTS;
    if result_truncated {
        files.select_nth_unstable(MAX_GLOB_RESULTS);
        files.truncate(MAX_GLOB_RESULTS);
    }
    files.sort();

    let mut body = serde_json::json!({
        "text": format_glob_text(&files, pattern, &project_root, result_truncated),
        "complete": !walk_truncated,
        "no_files_matched_scope": !scope_has_files,
        "files": files.iter().map(|path| path.display().to_string()).collect::<Vec<_>>(),
        "total": total,
        "truncated": result_truncated,
    });
    if walk_truncated {
        body["walk_truncated"] = serde_json::Value::Bool(true);
        let note = "(Fallback directory walk stopped early: file-count or time budget reached; results may be incomplete.)";
        body["text"] = serde_json::Value::String(format!(
            "{}\n\n{}",
            body["text"].as_str().unwrap_or_default(),
            note
        ));
    }

    Response::success(&req.id, body)
}

fn scope_has_files(project_root: &Path, search_root: &Path) -> bool {
    let catch_all = build_path_filters(&["**/*".to_string()], &[]).expect("valid catch-all glob");
    has_any_project_file_from(project_root, search_root, &catch_all)
}

fn glob_root(
    ctx: &AppContext,
    project_root: &Path,
    search_root: &Path,
    pattern: &str,
    max_results: usize,
) -> GlobDiscovery {
    let search_root_text = search_root.to_string_lossy();
    let search_scope = resolve_search_scope(project_root, Some(search_root_text.as_ref()));
    let indexed_snapshot = {
        let search_index = ctx
            .search_index()
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        match search_index.as_ref() {
            Some(index) if index.ready && search_scope.use_index => Some(index.snapshot()),
            _ => None,
        }
    };
    let indexed = indexed_snapshot.map(|snapshot| {
        let (files, scope_has_files, entries_visited) =
            snapshot.glob_profiled(pattern, &search_scope.root, false);
        GlobDiscovery {
            entries_visited,
            files,
            walk_truncated: false,
            source: "index",
            walk_time: Duration::ZERO,
            scope_has_files,
            scope_probe: Duration::ZERO,
        }
    });

    match indexed {
        Some(discovery) => discovery,
        None => {
            if search_scope.use_index {
                super::configure::trigger_search_index_reload_if_evicted(ctx);
            }
            if !search_scope.use_index {
                let walk_started = Instant::now();
                if let Some(outcome) =
                    super::grep::ripgrep_glob(&search_scope.root, pattern, max_results)
                {
                    let walk_time = walk_started.elapsed();
                    let scope_started = Instant::now();
                    let scope_has_files = scope_has_files(project_root, &search_scope.root);
                    return GlobDiscovery {
                        files: outcome.files,
                        walk_truncated: outcome.walk_truncated,
                        source: "fallback",
                        entries_visited: outcome.entries_visited,
                        walk_time,
                        scope_has_files,
                        scope_probe: scope_started.elapsed(),
                    };
                }
            }
            fallback_glob(project_root, &search_scope.root, pattern)
        }
    }
}

fn merge_glob_files(files: Vec<PathBuf>) -> Vec<PathBuf> {
    let mut seen = HashSet::new();
    let mut deduped = Vec::new();
    for file in files {
        if seen.insert(canonical_key(&file)) {
            deduped.push(file);
        }
    }
    sort_paths_by_mtime_desc(&mut deduped);
    deduped
}

fn fallback_glob(
    project_root: &std::path::Path,
    search_root: &std::path::Path,
    pattern: &str,
) -> GlobDiscovery {
    let filters = build_path_filters(&[pattern.to_string()], &[]).unwrap_or_default();
    let filter_root = if search_root.starts_with(project_root) {
        project_root
    } else {
        search_root
    };
    let walk_started = Instant::now();
    let outcome = bounded_fallback_walk_files(filter_root, search_root, &filters);
    let walk_time = walk_started.elapsed();
    let scope_started = Instant::now();
    let scope_has_files = scope_has_files(project_root, search_root);
    GlobDiscovery {
        files: outcome.files,
        walk_truncated: outcome.walk_truncated,
        source: "fallback",
        entries_visited: outcome.entries_visited,
        walk_time,
        scope_has_files,
        scope_probe: scope_started.elapsed(),
    }
}

fn format_glob_text(
    files: &[PathBuf],
    pattern: &str,
    project_root: &Path,
    truncated: bool,
) -> String {
    // Convert to relative paths within project
    let relative_files: Vec<PathBuf> = files
        .iter()
        .map(|p| p.strip_prefix(project_root).unwrap_or(p).to_path_buf())
        .collect();

    let header = format!(
        "{} {} matching {}",
        relative_files.len(),
        if relative_files.len() == 1 {
            "file"
        } else {
            "files"
        },
        pattern
    );

    let text = if relative_files.is_empty() {
        header
    } else if relative_files.len() <= MAX_FLAT_FILES {
        let body = relative_files
            .iter()
            .map(|path| path.display().to_string())
            .collect::<Vec<_>>()
            .join("\n");
        format!("{}\n\n{}", header, body)
    } else {
        let grouped = group_files_by_directory(&relative_files);
        let total_directories = grouped.len();
        let displayed_directories = if total_directories > MAX_DIRECTORY_SECTIONS {
            MAX_DISPLAY_DIRECTORIES
        } else {
            total_directories
        };

        let mut sections = Vec::new();
        for (directory, names) in grouped.iter().take(displayed_directories) {
            let file_word = if names.len() == 1 { "file" } else { "files" };
            let names_text = if names.len() > MAX_FILES_PER_DIRECTORY {
                format!(
                    "{}, ...",
                    names
                        .iter()
                        .take(MAX_DISPLAY_FILES_PER_DIRECTORY)
                        .cloned()
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            } else {
                names.join(", ")
            };
            sections.push(format!(
                "{} ({} {})\n  {}",
                directory,
                names.len(),
                file_word,
                names_text
            ));
        }

        let mut body = format!("{}\n\n{}", header, sections.join("\n\n"));

        if total_directories > MAX_DIRECTORY_SECTIONS {
            let hidden_directories = &grouped[displayed_directories..];
            let hidden_file_count: usize = hidden_directories
                .iter()
                .map(|(_, names)| names.len())
                .sum();
            let hidden_directory_count = total_directories - displayed_directories;
            body.push_str(&format!(
                "\n\n... and {} more {} in {} {}",
                hidden_file_count,
                if hidden_file_count == 1 {
                    "file"
                } else {
                    "files"
                },
                hidden_directory_count,
                if hidden_directory_count == 1 {
                    "directory"
                } else {
                    "directories"
                }
            ));
        }

        body
    };

    if truncated {
        format!("{}\n\n{}", text, GLOB_TRUNCATED_MESSAGE)
    } else {
        text
    }
}

fn group_files_by_directory(files: &[PathBuf]) -> Vec<(String, Vec<String>)> {
    let mut grouped: BTreeMap<String, Vec<String>> = BTreeMap::new();

    for file in files {
        let directory = format_directory_label(file.parent());
        let file_name = file
            .file_name()
            .map(|name| name.to_string_lossy().into_owned())
            .unwrap_or_else(|| file.display().to_string());
        grouped.entry(directory).or_default().push(file_name);
    }

    grouped.into_iter().collect()
}

fn format_directory_label(directory: Option<&Path>) -> String {
    match directory {
        Some(path) if !path.as_os_str().is_empty() && path != Path::new(".") => {
            format!("{}/", path.display())
        }
        _ => "./".to_string(),
    }
}

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

    fn files(paths: &[&str]) -> Vec<PathBuf> {
        paths.iter().map(PathBuf::from).collect()
    }

    fn root() -> PathBuf {
        PathBuf::from("/project")
    }

    #[test]
    fn glob_uses_flat_list_for_small_results() {
        let text = format_glob_text(&files(&["src/a.rs", "src/b.rs"]), "**/*.rs", &root(), false);

        assert_eq!(text, "2 files matching **/*.rs\n\nsrc/a.rs\nsrc/b.rs");
    }

    #[test]
    fn glob_groups_directories_and_summarizes_overflow() {
        let text = format_glob_text(
            &files(&[
                "dir1/a.rs",
                "dir1/b.rs",
                "dir1/c.rs",
                "dir1/d.rs",
                "dir1/e.rs",
                "dir1/f.rs",
                "dir1/g.rs",
                "dir1/h.rs",
                "dir2/a.rs",
                "dir2/b.rs",
                "dir3/a.rs",
                "dir3/b.rs",
                "dir4/a.rs",
                "dir4/b.rs",
                "dir5/a.rs",
                "dir5/b.rs",
                "dir6/a.rs",
                "dir6/b.rs",
                "dir7/a.rs",
                "dir7/b.rs",
                "dir8/a.rs",
                "dir8/b.rs",
                "dir9/a.rs",
            ]),
            "**/*.rs",
            &root(),
            false,
        );

        assert!(text.starts_with("23 files matching **/*.rs\n\n"));
        assert!(text.contains("dir1/ (8 files)\n  a.rs, b.rs, c.rs, d.rs, e.rs, ..."));
        assert!(text.contains("dir6/ (2 files)\n  a.rs, b.rs"));
        assert!(!text.contains("dir7/ (2 files)\n  a.rs, b.rs"));
        assert!(text.ends_with("... and 5 more files in 3 directories"));
    }

    #[test]
    fn glob_appends_truncation_message() {
        let text = format_glob_text(&files(&["src/a.rs"]), "**/*.rs", &root(), true);

        assert_eq!(
            text,
            "1 file matching **/*.rs\n\nsrc/a.rs\n\n(Results are truncated: showing first 100 results. Consider using a more specific path or pattern.)"
        );
    }
}