agent-file-tools 0.42.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
use std::fs;

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

use serde_json::json;

use super::helpers::AftProcess;

fn configure_with_backup_disabled(aft: &mut AftProcess, root: &std::path::Path) {
    let resp = aft.send(
        &json!({
            "id": "cfg",
            "command": "configure",
            "harness": "opencode",
            "project_root": root,
            "config": [{
                "tier": "user",
                "source": "test",
                "doc": r#"{ "backup": { "enabled": false } }"#,
            }],
        })
        .to_string(),
    );
    assert_eq!(resp["success"], true, "configure should succeed: {resp:?}");
}

#[test]
fn edit_match_glob_rolls_back_prior_files_when_later_write_fails() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let a = root.join("a.ts");
    let b = root.join("b.ts");
    let c = root.join("c.ts");

    fs::write(&a, "const a = \"OLD\";\n").unwrap();
    fs::write(&b, "const b = \"OLD\";\n").unwrap();
    fs::write(&c, "const c = \"OLD\";\n").unwrap();

    let mut readonly = fs::metadata(&b).unwrap().permissions();
    readonly.set_readonly(true);
    fs::set_permissions(&b, readonly).unwrap();

    let mut aft = AftProcess::spawn();
    configure_with_backup_disabled(&mut aft, root);
    let req = json!({
        "id": "glob-rollback-write-failure",
        "command": "edit_match",
        "file": format!("{}/*.ts", root.display()),
        "match": "OLD",
        "replacement": "NEW"
    });
    let resp = aft.send(&req.to_string());

    make_writable(&b);

    assert_eq!(resp["success"], false, "glob edit should fail: {resp:?}");
    assert_eq!(resp["code"], "write_error");
    assert_eq!(fs::read_to_string(&a).unwrap(), "const a = \"OLD\";\n");
    assert_eq!(fs::read_to_string(&b).unwrap(), "const b = \"OLD\";\n");
    assert_eq!(fs::read_to_string(&c).unwrap(), "const c = \"OLD\";\n");

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_glob_rolls_back_when_any_file_becomes_syntax_invalid() {
    // Glob edit_match is atomic w.r.t. syntax: if any file ends up syntax-
    // invalid after the replacement, the whole batch rolls back to the
    // pre-edit checkpoint. Previously this code reported per-file
    // `syntax_valid: false` and left edits applied, which silently broke
    // the project. The new contract: agent gets a clear `syntax_invalid`
    // error and the working tree is unchanged.
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let a = root.join("a.ts");
    let b = root.join("b.ts");
    let c = root.join("c.ts");

    let original_a = "const a = 1; // TARGET\n";
    let original_b = "const b = \"TARGET\";\n";
    let original_c = "const c = TARGET;\n";
    fs::write(&a, original_a).unwrap();
    fs::write(&b, original_b).unwrap();
    fs::write(&c, original_c).unwrap();

    let mut aft = AftProcess::spawn();
    configure_with_backup_disabled(&mut aft, root);
    let req = json!({
        "id": "glob-rollback-syntax-failure",
        "command": "edit_match",
        "file": format!("{}/*.ts", root.display()),
        "match": "TARGET",
        "replacement": "{;"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(
        resp["success"], false,
        "glob edit must fail when any file becomes syntax-invalid: {resp:?}"
    );
    assert_eq!(resp["code"], "syntax_invalid", "wrong error code: {resp:?}");
    let msg = resp["message"].as_str().expect("message");
    assert!(
        msg.contains("rolled back"),
        "error message should mention rollback: {msg}"
    );

    // All three files must be unchanged from their original contents.
    assert_eq!(fs::read_to_string(&a).unwrap(), original_a);
    assert_eq!(fs::read_to_string(&b).unwrap(), original_b);
    assert_eq!(fs::read_to_string(&c).unwrap(), original_c);

    let undo = aft.send(&json!({"id": "undo-after-glob-rollback", "command": "undo"}).to_string());
    assert_eq!(
        undo["success"], false,
        "glob rollback should discard operation undo entries: {undo:?}"
    );
    assert_eq!(undo["code"], "backups_disabled");

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_glob_succeeds_when_all_files_remain_syntax_valid() {
    // Companion to the rollback test: if the replacement keeps every file
    // syntax-valid, the batch commits normally and per-file results report
    // syntax_valid: true. This guards against an over-eager rollback that
    // would block legitimate batch edits.
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let a = root.join("a.ts");
    let b = root.join("b.ts");

    fs::write(&a, "const a = TARGET;\n").unwrap();
    fs::write(&b, "const b = TARGET;\n").unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "glob-syntax-clean",
        "command": "edit_match",
        "file": format!("{}/*.ts", root.display()),
        "match": "TARGET",
        "replacement": "42"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(resp["success"], true, "expected success: {resp:?}");
    let files = resp["files"].as_array().expect("files array");
    assert_eq!(files.len(), 2);
    for file in files {
        assert_eq!(file["syntax_valid"], true, "file should be valid: {file:?}");
    }
    assert_eq!(fs::read_to_string(&a).unwrap(), "const a = 42;\n");
    assert_eq!(fs::read_to_string(&b).unwrap(), "const b = 42;\n");

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_omits_syntax_valid_when_language_unsupported() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("notes.unusual_extension");
    fs::write(&file, "hello old world\n").unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "unsupported-syntax-valid",
        "command": "edit_match",
        "file": file,
        "match": "old",
        "replacement": "new"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(resp["success"], true, "expected edit success: {resp:?}");
    assert!(
        resp.get("syntax_valid").is_none(),
        "syntax_valid must be absent when validation could not run: {resp:?}"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("notes.unusual_extension")).unwrap(),
        "hello new world\n"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

// Regression for #83: a fuzzy (rstrip) match must not drop the trailing
// newline of the matched range. The fuzzy line matcher includes the newline
// after the last matched line in its byte range; applying the replacement
// verbatim used to merge the last replaced line with the following line.
#[test]
fn edit_match_fuzzy_preserves_trailing_newline() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("greet.js");
    // Trailing spaces after two lines force the rstrip fuzzy pass (pass 2),
    // since the clean `match` below won't match byte-for-byte.
    fs::write(
        &file,
        "function greet(name) {\n  const message = \"Hello, \" + name;   \n  console.log(message);   \n  return message;\n}\n",
    )
    .unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "fuzzy-trailing-newline",
        "command": "edit_match",
        "file": file,
        // No trailing spaces in the match (forces fuzzy) and no trailing newline
        // in the replacement (the bug condition).
        "match": "  const message = \"Hello, \" + name;\n  console.log(message);\n  return message;",
        "replacement": "  const msg = \"Hello, \" + name;\n  console.log(msg);\n  return msg;"
    });
    let resp = aft.send(&req.to_string());
    assert_eq!(resp["success"], true, "expected edit success: {resp:?}");

    let after = fs::read_to_string(&file).unwrap();
    // The newline before `}` must survive — `return msg;` and `}` stay on
    // separate lines instead of merging into `  return msg;}`.
    assert!(
        after.contains("  return msg;\n}"),
        "trailing newline before closing brace was dropped: {after:?}"
    );
    assert!(
        !after.contains("return msg;}"),
        "last replaced line merged with the next line: {after:?}"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_replace_all_rejects_overlapping_fuzzy_matches() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("over.js");
    // Trailing spaces defeat the exact (pass 1) matcher and force the fuzzy
    // line pass, which steps line-by-line. A 2-line needle over three identical
    // lines yields two OVERLAPPING matches (lines 0-1 and lines 1-2). Applying
    // them in reverse would silently corrupt the file, so replace_all must
    // reject with `overlapping_edits` instead.
    let original = "row \nrow \nrow \n";
    fs::write(&file, original).unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "replace-all-overlap",
        "command": "edit_match",
        "file": file,
        "match": "row\nrow",
        "replacement": "X\nX",
        "replace_all": true
    });
    let resp = aft.send(&req.to_string());
    assert_eq!(
        resp["success"], false,
        "overlapping replace_all must fail cleanly: {resp:?}"
    );
    assert_eq!(
        resp["code"], "overlapping_edits",
        "wrong error code: {resp:?}"
    );
    // File must be untouched — no partial/corrupt write.
    assert_eq!(
        fs::read_to_string(&file).unwrap(),
        original,
        "file was modified despite the overlap rejection"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_reflow_replaces_formatter_split() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("split.js");
    let original =
        "function demo() {\n  const value = alpha +\n    beta +\n    gamma;\n  return value;\n}\n";
    fs::write(&file, original).unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "reflow-split",
        "command": "edit_match",
        "file": file,
        "match": "  const value = alpha + beta + gamma;",
        "replacement": "  const value = alpha + beta + delta;"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(resp["success"], true, "expected edit success: {resp:?}");
    assert_eq!(
        fs::read_to_string(dir.path().join("split.js")).unwrap(),
        "function demo() {\n  const value = alpha + beta + delta;\n  return value;\n}\n"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_reflow_replaces_formatter_join() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("join.js");
    let original = "function demo() {\n  const value = alpha + beta + gamma;\n}\n";
    fs::write(&file, original).unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "reflow-join",
        "command": "edit_match",
        "file": file,
        "match": "  const value = alpha +\n    beta +\n    gamma;",
        "replacement": "  const value = alpha +\n    beta +\n    delta;"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(resp["success"], true, "expected edit success: {resp:?}");
    assert_eq!(
        fs::read_to_string(dir.path().join("join.js")).unwrap(),
        "function demo() {\n  const value = alpha +\n    beta +\n    delta;\n}\n"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_reflow_ambiguous_does_not_edit() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("ambiguous.js");
    let original =
        "const value = alpha +\n  beta +\n  gamma;\n\nconst value = alpha +\n  beta +\n  gamma;\n";
    fs::write(&file, original).unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "reflow-ambiguous",
        "command": "edit_match",
        "file": file,
        "match": "const value = alpha + beta + gamma;",
        "replacement": "const value = alpha + beta + delta;"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(
        resp["success"], false,
        "ambiguous edit should fail: {resp:?}"
    );
    assert_eq!(
        resp["code"], "ambiguous_match",
        "wrong error code: {resp:?}"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("ambiguous.js")).unwrap(),
        original
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn edit_match_reflow_near_miss_does_not_edit() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("near_miss.js");
    let original = "const value = alpha +\n  beta +\n  gamma;\n";
    fs::write(&file, original).unwrap();

    let mut aft = AftProcess::spawn();
    let req = json!({
        "id": "reflow-near-miss",
        "command": "edit_match",
        "file": file,
        "match": "const value = alpha + beta + delta;",
        "replacement": "const value = alpha + beta + epsilon;"
    });
    let resp = aft.send(&req.to_string());

    assert_eq!(resp["success"], false, "near miss should fail: {resp:?}");
    assert_eq!(
        resp["code"], "match_not_found",
        "wrong error code: {resp:?}"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("near_miss.js")).unwrap(),
        original
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[cfg(unix)]
fn make_writable(path: &std::path::Path) {
    let mut permissions = fs::metadata(path).unwrap().permissions();
    permissions.set_mode(0o644);
    fs::set_permissions(path, permissions).unwrap();
}

#[cfg(windows)]
fn make_writable(path: &std::path::Path) {
    let mut permissions = fs::metadata(path).unwrap().permissions();
    // On Windows this clears the read-only file attribute, which is exactly the
    // intent here; the clippy lint targets the Unix world-writable footgun that
    // does not apply to this windows-only helper.
    #[allow(clippy::permissions_set_readonly_false)]
    permissions.set_readonly(false);
    fs::set_permissions(path, permissions).unwrap();
}