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
use super::{
    import_byte_range, ImportBlock, ImportForm, ImportGroup, ImportKind, ImportRequest,
    ImportStatement, ImportSyntax,
};
use tree_sitter::{Node, Tree};

const C_INCLUDE_SYSTEM_KIND: &str = "system";
const C_INCLUDE_LOCAL_KIND: &str = "local";

/// Normalize an agent-supplied C/C++ include module.
///
/// Agents naturally pass the include with its delimiter (`<vector>` or
/// `"foo.h"`) because that is how includes are written. Strip the delimiter and
/// infer the include kind so dedup, classification, and generation all see the
/// bare header path — otherwise generation double-wraps into `#include
/// <<vector>>`, which fails syntax validation and rolls the edit back.
///
/// Returns the bare header path plus the inferred include kind, or the input
/// unchanged with `None` when no delimiter is present (the caller then honors an
/// explicit `import_kind`, defaulting to a system include).
pub(crate) fn normalize_include_module(module: &str) -> (String, Option<&'static str>) {
    let trimmed = module.trim();
    if let Some(inner) = trimmed.strip_prefix('<').and_then(|s| s.strip_suffix('>')) {
        (inner.trim().to_string(), Some(C_INCLUDE_SYSTEM_KIND))
    } else if let Some(inner) = trimmed.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
        (inner.trim().to_string(), Some(C_INCLUDE_LOCAL_KIND))
    } else {
        (module.to_string(), None)
    }
}

pub(crate) fn parse_c_imports(source: &str, tree: &Tree) -> ImportBlock {
    let root = tree.root_node();
    let mut imports = Vec::new();

    let mut cursor = root.walk();
    if cursor.goto_first_child() {
        loop {
            let node = cursor.node();
            if node.kind() == "preproc_include" {
                if let Some(imp) = parse_c_include(source, &node) {
                    imports.push(imp);
                }
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }

    let byte_range = import_byte_range(&imports);
    ImportBlock {
        imports,
        byte_range,
    }
}

fn parse_c_include(source: &str, node: &Node) -> Option<ImportStatement> {
    let (module_path, import_kind) = c_include_target(source, node)?;
    if module_path.is_empty() {
        return None;
    }

    let byte_range = include_byte_range(source, node);
    let raw_text = source[byte_range.clone()].to_string();
    let group = classify_group_c_import_kind(import_kind);
    let import_kind_string = import_kind.to_string();

    Some(ImportStatement {
        module_path,
        names: Vec::new(),
        // Preserve the include delimiter through legacy organize paths, which
        // regenerate from flat fields before they reach the structured form.
        default_import: Some(import_kind_string.clone()),
        namespace_import: None,
        kind: ImportKind::SideEffect,
        group,
        byte_range,
        raw_text,
        form: ImportForm::Structured {
            named: Vec::new(),
            namespace: None,
            alias: None,
            modifiers: Vec::new(),
            import_kind: Some(import_kind_string),
        },
    })
}

fn include_byte_range(source: &str, node: &Node) -> std::ops::Range<usize> {
    let start = node.byte_range().start;
    let mut end = node.byte_range().end;
    let bytes = source.as_bytes();
    while end > start && matches!(bytes[end - 1], b'\n' | b'\r') {
        end -= 1;
    }
    start..end
}

fn c_include_target(source: &str, node: &Node) -> Option<(String, &'static str)> {
    let mut cursor = node.walk();
    if cursor.goto_first_child() {
        loop {
            let child = cursor.node();
            match child.kind() {
                "system_lib_string" => {
                    let module_path = delimited_include_path(source, &child, '<', '>')?;
                    return Some((module_path, C_INCLUDE_SYSTEM_KIND));
                }
                "string_literal" => {
                    let module_path = delimited_include_path(source, &child, '"', '"')?;
                    return Some((module_path, C_INCLUDE_LOCAL_KIND));
                }
                _ => {}
            }
            if !cursor.goto_next_sibling() {
                break;
            }
        }
    }
    None
}

fn delimited_include_path(source: &str, node: &Node, open: char, close: char) -> Option<String> {
    let raw = source[node.byte_range()].trim();
    if !raw.starts_with(open) || !raw.ends_with(close) {
        return None;
    }

    let start = open.len_utf8();
    let end = raw.len().checked_sub(close.len_utf8())?;
    let module_path = raw[start..end].trim();
    if module_path.is_empty() {
        None
    } else {
        Some(module_path.to_string())
    }
}

pub(crate) fn classify_group_c_import_kind(import_kind: &str) -> ImportGroup {
    // C/C++ include delimiters carry search-path semantics: angle/system
    // includes are grouped as stdlib, while quote/local includes are grouped as
    // external project/third-party headers. They are deliberately distinct.
    if import_kind == C_INCLUDE_SYSTEM_KIND {
        ImportGroup::Stdlib
    } else {
        ImportGroup::External
    }
}

pub(crate) fn classify_group_c(_module_path: &str) -> ImportGroup {
    // The registry-level classifier only receives a header path, not the
    // delimiter. Default to system because the legacy add path also defaults to
    // generating an angle include when no `import_kind` is supplied. Parsed
    // includes use `classify_group_c_import_kind` and preserve the real delimiter.
    ImportGroup::Stdlib
}

pub(crate) fn generate_c_import_line(req: &ImportRequest) -> String {
    let import_kind = req
        .import_kind
        .or(req.default_import)
        .unwrap_or(C_INCLUDE_SYSTEM_KIND);

    if import_kind == C_INCLUDE_LOCAL_KIND {
        format!("#include \"{}\"", req.module_path)
    } else {
        format!("#include <{}>", req.module_path)
    }
}

pub struct CSyntax;

impl ImportSyntax for CSyntax {
    fn parse(&self, source: &str, tree: &Tree) -> ImportBlock {
        parse_c_imports(source, tree)
    }

    fn generate_line(&self, req: &ImportRequest) -> String {
        generate_c_import_line(req)
    }

    fn classify_group(&self, module_path: &str) -> ImportGroup {
        classify_group_c(module_path)
    }
}

pub static C_SYNTAX: CSyntax = CSyntax;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::imports::{generate_import, parse_imports};
    use crate::parser::{grammar_for, LangId};
    use std::collections::BTreeSet;
    use tree_sitter::Parser;

    fn parse_c_like(source: &str, lang: LangId) -> (Tree, ImportBlock) {
        let grammar = grammar_for(lang);
        let mut parser = Parser::new();
        parser.set_language(&grammar).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let block = parse_imports(source, &tree, lang);
        (tree, block)
    }

    fn structured(import: &ImportStatement) -> Option<&str> {
        match &import.form {
            ImportForm::Structured {
                named,
                namespace,
                alias,
                modifiers,
                import_kind,
            } => {
                assert!(named.is_empty());
                assert!(namespace.is_none());
                assert!(alias.is_none());
                assert!(modifiers.is_empty());
                import_kind.as_deref()
            }
            other => panic!("expected C/C++ Structured import, got {other:?}"),
        }
    }

    /// Grammar fixture: lock the exact tree-sitter-c and tree-sitter-cpp node
    /// kinds this parser depends on. Both grammars currently emit
    /// `preproc_include`, with `system_lib_string` for `<...>` headers and
    /// `string_literal` / `string_content` for `"..."` headers.
    #[test]
    fn c_include_grammar_node_kinds_are_stable() {
        for (lang, source) in [
            (
                LangId::C,
                "#include <stdio.h>\n#include \"local.h\"\nint main(void) { return 0; }\n",
            ),
            (
                LangId::Cpp,
                "#include <vector>\n#include \"foo.hpp\"\nint main() { return 0; }\n",
            ),
        ] {
            let (tree, _) = parse_c_like(source, lang);
            assert!(!tree.root_node().has_error(), "parse errors for {lang:?}");

            let mut kinds = BTreeSet::new();
            fn walk(node: Node, kinds: &mut BTreeSet<String>) {
                kinds.insert(node.kind().to_string());
                let mut cursor = node.walk();
                if cursor.goto_first_child() {
                    loop {
                        walk(cursor.node(), kinds);
                        if !cursor.goto_next_sibling() {
                            break;
                        }
                    }
                }
            }
            walk(tree.root_node(), &mut kinds);

            for required in [
                "translation_unit",
                "preproc_include",
                "#include",
                "system_lib_string",
                "string_literal",
                "string_content",
            ] {
                assert!(
                    kinds.contains(required),
                    "{lang:?} grammar missing node kind {required:?}; present: {kinds:?}"
                );
            }
        }
    }

    #[test]
    fn parse_c_and_cpp_includes_preserves_delimiters() {
        for (lang, source, system_path, local_path) in [
            (
                LangId::C,
                "#include <stdio.h>\n#include \"project/header.h\"\nint main(void) { return 0; }\n",
                "stdio.h",
                "project/header.h",
            ),
            (
                LangId::Cpp,
                "#include <vector>\n#include \"foo/bar.hpp\"\nint main() { return 0; }\n",
                "vector",
                "foo/bar.hpp",
            ),
        ] {
            let (_, block) = parse_c_like(source, lang);
            assert_eq!(block.imports.len(), 2, "parse imports for {lang:?}");
            assert_c_include(
                &block.imports[0],
                system_path,
                C_INCLUDE_SYSTEM_KIND,
                ImportGroup::Stdlib,
            );
            assert_c_include(
                &block.imports[1],
                local_path,
                C_INCLUDE_LOCAL_KIND,
                ImportGroup::External,
            );
        }
    }

    fn assert_c_include(
        imp: &ImportStatement,
        module_path: &str,
        import_kind: &str,
        group: ImportGroup,
    ) {
        assert_eq!(imp.module_path, module_path);
        assert_eq!(imp.names, Vec::<String>::new());
        assert_eq!(imp.default_import.as_deref(), Some(import_kind));
        assert_eq!(imp.namespace_import, None);
        assert_eq!(imp.kind, ImportKind::SideEffect);
        assert_eq!(imp.group, group);
        assert_eq!(structured(imp), Some(import_kind));
    }

    #[test]
    fn generate_c_and_cpp_supported_forms() {
        for lang in [LangId::C, LangId::Cpp] {
            assert_eq!(
                generate_import(
                    lang,
                    &ImportRequest {
                        module_path: "stdio.h",
                        names: &[],
                        default_import: None,
                        namespace: None,
                        alias: None,
                        type_only: false,
                        modifiers: &[],
                        import_kind: Some(C_INCLUDE_SYSTEM_KIND),
                    },
                ),
                "#include <stdio.h>"
            );
            assert_eq!(
                generate_import(
                    lang,
                    &ImportRequest {
                        module_path: "project/header.h",
                        names: &[],
                        default_import: None,
                        namespace: None,
                        alias: None,
                        type_only: false,
                        modifiers: &[],
                        import_kind: Some(C_INCLUDE_LOCAL_KIND),
                    },
                ),
                "#include \"project/header.h\""
            );
        }
    }

    #[test]
    fn generate_c_preserves_organized_flat_markers() {
        for (marker, expected) in [
            (C_INCLUDE_SYSTEM_KIND, "#include <stdio.h>"),
            (C_INCLUDE_LOCAL_KIND, "#include \"stdio.h\""),
        ] {
            assert_eq!(
                generate_import(
                    LangId::C,
                    &ImportRequest::legacy("stdio.h", &[], Some(marker), None, false),
                ),
                expected
            );
        }
    }

    #[test]
    fn c_and_cpp_round_trip_through_parse_generate() {
        for (lang, samples) in [
            (
                LangId::C,
                ["#include <stdio.h>", "#include \"project/header.h\""],
            ),
            (LangId::Cpp, ["#include <vector>", "#include \"foo.hpp\""]),
        ] {
            for src in samples {
                let (_, block) = parse_c_like(src, lang);
                assert_eq!(block.imports.len(), 1, "parse {src:?} for {lang:?}");
                let imp = &block.imports[0];
                let import_kind = structured(imp);
                let regenerated = generate_import(
                    lang,
                    &ImportRequest {
                        module_path: &imp.module_path,
                        names: &imp.names,
                        default_import: imp.default_import.as_deref(),
                        namespace: None,
                        alias: None,
                        type_only: false,
                        modifiers: &[],
                        import_kind,
                    },
                );
                assert_eq!(regenerated, src, "round-trip mismatch for {src:?}");
            }
        }
    }

    #[test]
    fn normalize_include_module_strips_delimiters_and_infers_kind() {
        // Angle/system delimiter.
        assert_eq!(
            normalize_include_module("<vector>"),
            ("vector".to_string(), Some(C_INCLUDE_SYSTEM_KIND))
        );
        // Quote/local delimiter.
        assert_eq!(
            normalize_include_module("\"foo/bar.h\""),
            ("foo/bar.h".to_string(), Some(C_INCLUDE_LOCAL_KIND))
        );
        // Surrounding whitespace tolerated.
        assert_eq!(
            normalize_include_module("  <stdio.h>  "),
            ("stdio.h".to_string(), Some(C_INCLUDE_SYSTEM_KIND))
        );
        // Bare header: unchanged, no inferred kind (caller defaults to system).
        assert_eq!(
            normalize_include_module("string"),
            ("string".to_string(), None)
        );
    }
}