agent-file-tools 0.49.2

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
use crate::helpers::AftProcess;
use std::fs;
use tempfile::TempDir;

const SAMPLE_MD: &str = r#"# Project Title

Some introduction text.

## Features

- Feature one
- Feature two

### Sub-feature A

Details about sub-feature A.

### Sub-feature B

Details about sub-feature B.

## Architecture

The architecture section.

### Component X

Info about X.

# Appendix

Final notes.
"#;

#[test]
fn markdown_outline_extracts_headings() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("readme.md");
    fs::write(&md_file, SAMPLE_MD).unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(&format!(
        r#"{{"id":"md-1","command":"outline","file":{}}}"#,
        crate::helpers::json_string(&md_file.display())
    ));

    assert_eq!(resp["success"], true, "outline should succeed: {:?}", resp);

    let text = resp["text"]
        .as_str()
        .expect("text field should be a string");

    // Single-file lines carry the signature directly (no `{vis} {kind}` prefix),
    // so a heading renders as its markdown signature: a leading run of '#'.
    assert!(
        text.contains("# Project Title"),
        "headings should render with their markdown signature: {text}"
    );

    // All headings should be present in the outline
    assert!(text.contains("Project Title"), "should have Project Title");
    assert!(text.contains("Features"), "should have Features");
    assert!(text.contains("Sub-feature A"), "should have Sub-feature A");
    assert!(text.contains("Sub-feature B"), "should have Sub-feature B");
    assert!(text.contains("Architecture"), "should have Architecture");
    assert!(text.contains("Appendix"), "should have Appendix");

    // Exactly 2 top-level headings: lines indented one level (two spaces) whose
    // signature starts with '#'. Nested headings are members and carry a '.' prefix.
    let top_level_count = text.lines().filter(|l| l.starts_with("  #")).count();
    assert_eq!(
        top_level_count, 2,
        "expected 2 top-level headings (Project Title and Appendix), got: {}",
        top_level_count
    );

    // Project Title and Appendix are top-level (their lines have no "." prefix)
    let project_title_line = text
        .lines()
        .find(|l| l.contains("Project Title"))
        .expect("Project Title line");
    assert!(
        !project_title_line.contains('.'),
        "Project Title should be top-level (no '.' prefix), got: {:?}",
        project_title_line
    );

    let appendix_line = text
        .lines()
        .find(|l| l.contains("Appendix"))
        .expect("Appendix line");
    assert!(
        !appendix_line.contains('.'),
        "Appendix should be top-level (no '.' prefix), got: {:?}",
        appendix_line
    );

    // Features and Architecture are nested (their lines have "." prefix)
    let features_line = text
        .lines()
        .find(|l| l.contains("Features"))
        .expect("Features line");
    assert!(
        features_line.contains('.'),
        "Features should be nested under Project Title (has '.' prefix), got: {:?}",
        features_line
    );

    let arch_line = text
        .lines()
        .find(|l| l.contains("Architecture"))
        .expect("Architecture line");
    assert!(
        arch_line.contains('.'),
        "Architecture should be nested under Project Title (has '.' prefix), got: {:?}",
        arch_line
    );
}

#[test]
fn markdown_outline_section_ranges_cover_content() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("doc.md");
    fs::write(&md_file, SAMPLE_MD).unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(&format!(
        r#"{{"id":"md-2","command":"outline","file":{}}}"#,
        crate::helpers::json_string(&md_file.display())
    ));

    assert_eq!(resp["success"], true);

    let text = resp["text"]
        .as_str()
        .expect("text field should be a string");

    // Find the Features heading line to extract its range
    let features_line = text
        .lines()
        .find(|l| l.contains("Features"))
        .expect("Features should be in outline");

    // The range is the last token in the line, format "start:end"
    let range_part = features_line
        .split_whitespace()
        .last()
        .expect("range at end of line");
    let (start_str, end_str) = range_part
        .split_once(':')
        .expect("range should be in start:end format");
    let start: u64 = start_str.parse().expect("start should be a number");
    let end: u64 = end_str.parse().expect("end should be a number");

    // Section should cover multiple lines (heading + list + sub-sections)
    assert!(
        end > start + 3,
        "Features section should span multiple lines: {}-{}",
        start,
        end
    );
}

#[test]
fn markdown_zoom_by_heading_name() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("readme.md");
    fs::write(&md_file, SAMPLE_MD).unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(&format!(
        r#"{{"id":"md-3","command":"zoom","file":{},"symbol":"Architecture"}}"#,
        crate::helpers::json_string(&md_file.display())
    ));

    assert_eq!(
        resp["success"], true,
        "zoom by heading name should work: {:?}",
        resp
    );
    let content = resp["content"].as_str().expect("content should be string");

    // Should contain the heading and its content
    assert!(
        content.contains("## Architecture"),
        "content should include heading"
    );
    assert!(
        content.contains("The architecture section"),
        "content should include body text"
    );
    assert!(
        content.contains("### Component X"),
        "content should include sub-heading"
    );
}

#[test]
fn markdown_zoom_accepts_copied_heading_prefixes() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("readme.md");
    fs::write(&md_file, SAMPLE_MD).unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());

    let bare = aft.send(&format!(
        r#"{{"id":"md-prefix-bare","command":"zoom","file":{},"symbol":"Architecture"}}"#,
        crate::helpers::json_string(&md_file.display())
    ));
    assert_eq!(bare["success"], true, "bare heading should work: {bare:?}");

    for (id, symbol, expected) in [
        (
            "md-prefix-h2",
            "## Architecture",
            "The architecture section",
        ),
        (
            "md-prefix-h3",
            "### Sub-feature A",
            "Details about sub-feature A",
        ),
        ("md-prefix-h1", "# Project Title", "Some introduction text"),
    ] {
        let resp = aft.send(&format!(
            r#"{{"id":"{}","command":"zoom","file":{},"symbol":"{}"}}"#,
            id,
            crate::helpers::json_string(&md_file.display()),
            symbol
        ));
        assert_eq!(
            resp["success"], true,
            "prefixed markdown heading {symbol:?} should work: {resp:?}"
        );
        if symbol == "## Architecture" {
            assert_eq!(
                resp["range"], bare["range"],
                "## Architecture should match the bare heading range"
            );
        }
        assert!(
            resp["content"].as_str().unwrap().contains(expected),
            "content should include {expected:?}: {resp:?}"
        );
    }
}

#[test]
fn markdown_zoom_normalizes_heading_identity_variants() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("decorated-headings.md");
    fs::write(
        &md_file,
        r#"# 12.1.5 Internationalization and links

Internationalization details.

## 📓 [Community Issue Tracker](https://example.com/issues)

Community tracker details.

## HTTP Flow

HTTP flow details.
"#,
    )
    .unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());

    for (id, symbol, expected) in [
        (
            "md-normalized-numeric",
            "Internationalization and links",
            "Internationalization details.",
        ),
        (
            "md-normalized-emoji-link",
            "Community Issue Tracker",
            "Community tracker details.",
        ),
        (
            "md-normalized-anchor-underscore",
            "#http_flow",
            "HTTP flow details.",
        ),
        (
            "md-normalized-anchor-hyphen",
            "http-flow",
            "HTTP flow details.",
        ),
    ] {
        let resp = aft.send(
            &serde_json::json!({
                "id": id,
                "command": "zoom",
                "file": md_file,
                "symbol": symbol,
            })
            .to_string(),
        );
        assert_eq!(resp["success"], true, "normalized heading zoom: {resp:?}");
        assert!(
            resp["content"].as_str().unwrap().contains(expected),
            "zoom should include {expected:?}: {resp:?}"
        );
    }

    let exact = aft.send(
        &serde_json::json!({
            "id": "md-normalized-exact",
            "command": "zoom",
            "file": md_file,
            "symbol": "📓 [Community Issue Tracker](https://example.com/issues)",
        })
        .to_string(),
    );
    assert_eq!(
        exact["success"], true,
        "raw heading should still match: {exact:?}"
    );
    assert_eq!(
        exact["name"],
        "📓 [Community Issue Tracker](https://example.com/issues)"
    );

    let suggestion = aft.send(
        &serde_json::json!({
            "id": "md-normalized-suggestion",
            "command": "zoom",
            "file": md_file,
            "symbol": "Community Issue Tracke",
        })
        .to_string(),
    );
    assert_eq!(
        suggestion["success"], false,
        "misspelled heading should fail"
    );
    let message = suggestion["message"].as_str().unwrap();
    assert!(message.contains("did you mean: [Community Issue Tracker]"));
    assert!(!message.contains("[Community Issue Tracker](https://example.com/issues)"));

    assert!(aft.shutdown().success());
}

#[test]
fn markdown_zoom_normalization_collision_is_ambiguous() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("ambiguous-headings.md");
    fs::write(
        &md_file,
        "# 1. Intro\n\nFirst intro.\n\n# 2. Intro\n\nSecond intro.\n",
    )
    .unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(
        &serde_json::json!({
            "id": "md-normalized-ambiguous",
            "command": "zoom",
            "file": md_file,
            "symbol": "Intro",
        })
        .to_string(),
    );

    assert_eq!(
        resp["success"], true,
        "collision should return a menu: {resp:?}"
    );
    assert_eq!(resp["kind"], "ambiguous_symbol");
    let content = resp["content"].as_str().unwrap();
    assert!(
        content.contains("1. Intro"),
        "raw candidate should be listed: {content}"
    );
    assert!(
        content.contains("2. Intro"),
        "raw candidate should be listed: {content}"
    );

    assert!(aft.shutdown().success());
}

#[test]
fn markdown_zoom_heading_not_found() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("readme.md");
    fs::write(&md_file, SAMPLE_MD).unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(&format!(
        r#"{{"id":"md-4","command":"zoom","file":{},"symbol":"Missing Heading"}}"#,
        crate::helpers::json_string(&md_file.display())
    ));

    assert_eq!(
        resp["success"], false,
        "missing heading should error: {:?}",
        resp
    );
    assert_eq!(resp["code"], "symbol_not_found");
}

#[test]
fn markdown_write_preserves_content() {
    let dir = TempDir::new().unwrap();
    let md_file = dir.path().join("new.md");

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());

    let new_content = "# New Doc\\n\\nSome content.\\n";
    let resp = aft.send(&format!(
        r#"{{"id":"md-5","command":"write","file":{},"content":"{}"}}"#,
        crate::helpers::json_string(&md_file.display()),
        new_content
    ));

    assert_eq!(resp["success"], true, "write should succeed: {:?}", resp);
    let on_disk = fs::read_to_string(&md_file).unwrap();
    assert!(
        on_disk.contains("# New Doc"),
        "written content should be on disk"
    );
}

#[test]
fn markdown_mdx_extension_supported() {
    let dir = TempDir::new().unwrap();
    let mdx_file = dir.path().join("doc.mdx");
    fs::write(&mdx_file, "# MDX Doc\n\nSome content.\n").unwrap();

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());
    let resp = aft.send(&format!(
        r#"{{"id":"md-6","command":"outline","file":{}}}"#,
        crate::helpers::json_string(&mdx_file.display())
    ));

    assert_eq!(resp["success"], true, "mdx should be supported: {:?}", resp);

    let text = resp["text"]
        .as_str()
        .expect("text field should be a string");
    assert!(
        text.contains("MDX Doc"),
        "outline should contain MDX Doc heading, got: {:?}",
        text
    );
}