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
//! Integration tests for the `move_file` command, focused on error-message
//! quality (BUG-7 from the dogfooding triage).

use std::path::{Path, PathBuf};
use std::thread;
use std::time::{Duration, Instant};

use aft::commands::move_file::handle_move_file;
use aft::config::Config;
use aft::context::AppContext;
use aft::lsp::client::LspEvent;
use aft::lsp::registry::ServerKind;
use aft::parser::TreeSitterProvider;
use aft::protocol::RawRequest;
use serde_json::json;

use crate::helpers::AftProcess;

fn configure(aft: &mut AftProcess, root: &std::path::Path) {
    let resp = aft.configure(root);
    assert_eq!(resp["success"], true, "configure should succeed: {resp:?}");
}

fn send(aft: &mut AftProcess, request: serde_json::Value) -> serde_json::Value {
    aft.send(&serde_json::to_string(&request).expect("serialize request"))
}

fn fake_server_path() -> PathBuf {
    option_env!("CARGO_BIN_EXE_fake-lsp-server")
        .or(option_env!("CARGO_BIN_EXE_fake_lsp_server"))
        .map(PathBuf::from)
        .or_else(|| std::env::var_os("CARGO_BIN_EXE_fake-lsp-server").map(PathBuf::from))
        .or_else(|| std::env::var_os("CARGO_BIN_EXE_fake_lsp_server").map(PathBuf::from))
        .or_else(|| {
            let mut path = std::env::current_exe().ok()?;
            path.pop();
            path.pop();
            path.push("fake-lsp-server");
            Some(path)
        })
        .filter(|path| path.exists())
        .expect("fake-lsp-server binary path not set")
}

fn watched_file_change_count(events: &[serde_json::Value]) -> usize {
    events
        .iter()
        .filter_map(|event| event["changes"].as_array())
        .map(Vec::len)
        .sum()
}

fn collect_watched_file_events(
    ctx: &AppContext,
    expected_changes: usize,
) -> Vec<serde_json::Value> {
    let mut collected = Vec::new();
    let deadline = Instant::now() + Duration::from_secs(2);
    while Instant::now() < deadline {
        for event in ctx.lsp().drain_events() {
            if let LspEvent::Notification { method, params, .. } = event {
                if method == "custom/watchedFilesChanged" {
                    collected.push(params.expect("watched event params"));
                    if watched_file_change_count(&collected) >= expected_changes {
                        return collected;
                    }
                }
            }
        }
        thread::sleep(Duration::from_millis(25));
    }

    let actual_changes = watched_file_change_count(&collected);
    panic!(
        "timed out waiting for {expected_changes} watched-file changes; saw {actual_changes}: {collected:?}"
    );
}

fn notify_and_wait_for_publish(ctx: &AppContext, file_path: &Path, content: &str) {
    let outcome =
        ctx.lsp_notify_and_collect_diagnostics(file_path, content, Duration::from_secs(2));
    assert!(
        outcome.complete(),
        "timed out waiting for publishDiagnostics: {outcome:?}"
    );
}

#[test]
fn move_file_config_rename_notifies_deleted_and_created_in_one_event() {
    let tmp = tempfile::tempdir().expect("create temp dir");
    let root = tmp.path();
    let source = root.join("src").join("open.ts");
    let src_config = root.join("tsconfig.json");
    let dst_config = root.join("tsconfig.base.json");
    std::fs::create_dir_all(source.parent().unwrap()).expect("create src");
    std::fs::write(&source, "export const open = 1;\n").expect("write source");
    std::fs::write(&src_config, "{\"compilerOptions\":{}}\n").expect("write tsconfig");

    let ctx = AppContext::new(
        Box::new(TreeSitterProvider::new()),
        Config {
            project_root: Some(root.to_path_buf()),
            ..Config::default()
        },
    );
    ctx.lsp()
        .override_binary(ServerKind::TypeScript, fake_server_path());
    notify_and_wait_for_publish(&ctx, &source, "export const open = 2;\n");

    let req: RawRequest = serde_json::from_value(json!({
        "id": "move-tsconfig",
        "command": "move_file",
        "file": src_config.display().to_string(),
        "destination": dst_config.display().to_string(),
    }))
    .expect("request parses");
    let response = handle_move_file(&req, &ctx);
    let resp = serde_json::to_value(&response).expect("response serializes");
    assert_eq!(resp["success"], true, "move should succeed: {resp:?}");

    let events = collect_watched_file_events(&ctx, 2);
    let mut changes = Vec::new();
    for event in events {
        changes.extend(event["changes"].as_array().expect("changes array").clone());
    }
    assert_eq!(changes.len(), 2, "expected deleted+created events");
    assert!(changes.iter().any(|change| change["uri"]
        .as_str()
        .is_some_and(|uri| uri.ends_with("/tsconfig.json"))
        && change["type"] == 3));
    assert!(changes.iter().any(|change| change["uri"]
        .as_str()
        .is_some_and(|uri| uri.ends_with("/tsconfig.base.json"))
        && change["type"] == 1));
}

#[test]
fn undo_after_move_file_restores_source_and_removes_destination() {
    let tmp = tempfile::tempdir().expect("create temp dir");
    let root = tmp.path();
    let source = root.join("before.txt");
    let destination = root.join("nested").join("after.txt");
    std::fs::write(&source, "move me\n").expect("write source");

    let mut aft = AftProcess::spawn();
    configure(&mut aft, root);

    let move_resp = send(
        &mut aft,
        json!({
            "id": "move-before-undo",
            "command": "move_file",
            "file": source.display().to_string(),
            "destination": destination.display().to_string(),
        }),
    );
    assert_eq!(move_resp["success"], true, "move failed: {move_resp:?}");
    assert!(!source.exists(), "source should be moved away");
    assert_eq!(std::fs::read_to_string(&destination).unwrap(), "move me\n");

    let undo = send(
        &mut aft,
        json!({
            "id": "undo-move-operation",
            "command": "undo",
        }),
    );
    assert_eq!(undo["success"], true, "undo failed: {undo:?}");
    assert_eq!(undo["operation"], true);
    assert_eq!(std::fs::read_to_string(&source).unwrap(), "move me\n");
    assert!(
        !destination.exists(),
        "destination should be removed by move tombstone undo"
    );

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

#[cfg(unix)]
#[test]
fn undo_after_move_file_restores_symlink_source_and_removes_destination() {
    let tmp = tempfile::tempdir().expect("create temp dir");
    let root = tmp.path();
    let target = root.join("target.txt");
    let source = root.join("before-link.txt");
    let destination = root.join("nested").join("after-link.txt");
    std::fs::write(&target, "target contents\n").expect("write target");
    std::os::unix::fs::symlink(&target, &source).expect("create source symlink");

    let mut aft = AftProcess::spawn();
    configure(&mut aft, root);

    let move_resp = send(
        &mut aft,
        json!({
            "id": "move-symlink-before-undo",
            "command": "move_file",
            "file": source.display().to_string(),
            "destination": destination.display().to_string(),
        }),
    );
    assert_eq!(move_resp["success"], true, "move failed: {move_resp:?}");
    assert!(!source.exists(), "source symlink should be moved away");
    assert!(
        std::fs::symlink_metadata(&destination)
            .unwrap()
            .file_type()
            .is_symlink(),
        "destination should be the moved symlink"
    );

    let undo = send(
        &mut aft,
        json!({
            "id": "undo-symlink-move-operation",
            "command": "undo",
        }),
    );
    assert_eq!(undo["success"], true, "undo failed: {undo:?}");
    assert!(
        std::fs::symlink_metadata(&source)
            .unwrap()
            .file_type()
            .is_symlink(),
        "source should be restored as a symlink"
    );
    assert_eq!(std::fs::read_link(&source).unwrap(), target);
    assert!(
        !destination.exists(),
        "destination symlink should be removed"
    );
    assert_eq!(
        std::fs::read_to_string(&target).unwrap(),
        "target contents\n"
    );

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

#[cfg(target_os = "linux")]
#[test]
fn move_file_cross_fs_copy_delete_failure_reports_partial_success() {
    use std::os::unix::fs::PermissionsExt;
    use std::path::Path;

    if !Path::new("/dev/shm").exists() {
        return;
    }

    let src_tmp = tempfile::tempdir().expect("create source temp dir");
    let dst_tmp = tempfile::tempdir_in("/dev/shm").expect("create destination temp dir");
    let src_path = src_tmp.path().join("source.txt");
    let dst_path = dst_tmp.path().join("destination.txt");
    std::fs::write(&src_path, "content\n").expect("write source");

    let src_parent = src_path.parent().expect("source parent");
    let original_mode = std::fs::metadata(src_parent)
        .expect("source parent metadata")
        .permissions()
        .mode();
    std::fs::set_permissions(src_parent, std::fs::Permissions::from_mode(0o555))
        .expect("make source parent undeletable");

    let mut aft = AftProcess::spawn();
    configure(&mut aft, src_tmp.path());

    let resp = aft.send_with_timeout(
        &json!({
            "id": "move-partial-delete",
            "command": "move_file",
            "file": src_path.display().to_string(),
            "destination": dst_path.display().to_string(),
        })
        .to_string(),
        std::time::Duration::from_secs(120),
    );

    std::fs::set_permissions(src_parent, std::fs::Permissions::from_mode(original_mode))
        .expect("restore source parent permissions");

    assert_eq!(
        resp["success"], true,
        "copy succeeded, delete failed: {resp:?}"
    );
    assert_eq!(resp["moved"], true);
    assert_eq!(resp["complete"], false);
    assert_eq!(resp["source_delete_failed"], true);
    assert!(resp["warning"]
        .as_str()
        .is_some_and(|warning| warning.contains("Both paths now exist")));
    assert!(src_path.exists(), "source remains after partial move");
    assert!(dst_path.exists(), "destination was written");

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

/// Repro for the dogfooding complaint: an agent renamed `omo.png → alfonso.png`
/// earlier in a session, then later tried the same rename again. The
/// pre-existing message was just `move_file: source file not found: omo.png`,
/// which is technically correct but doesn't surface the obvious likely
/// cause (already-moved). The agent had to do an `ls` round-trip to
/// figure that out.
///
/// New behavior: when the source is missing AND the destination already
/// exists, the error message includes a hint suggesting the file may have
/// been moved earlier. Code stays `file_not_found` so the machine-readable
/// error class is unchanged.
#[test]
fn move_file_already_moved_hints_at_destination() {
    let tmp = tempfile::tempdir().expect("create temp dir");
    let root = tmp.path();

    // Simulate the "already moved" state: source missing, destination
    // present with the file the agent intended to move there.
    let dst_path = root.join("alfonso.png");
    std::fs::write(&dst_path, b"fake png bytes").expect("write destination");

    let mut aft = AftProcess::spawn();
    configure(&mut aft, root);

    let src_abs = root.join("omo.png").display().to_string();
    let dst_abs = root.join("alfonso.png").display().to_string();
    let resp = send(
        &mut aft,
        json!({
            "id": "move-already-done",
            "command": "move_file",
            "file": src_abs,
            "destination": dst_abs,
        }),
    );

    assert_eq!(resp["success"], false, "rename should fail: {resp:?}");
    assert_eq!(resp["code"], "file_not_found");

    let message = resp["message"].as_str().expect("error message string");

    // Original info still present so machine consumers don't break.
    assert!(
        message.contains("source file not found"),
        "should still say source not found: {message}"
    );
    assert!(
        message.contains("omo.png"),
        "should name the source: {message}"
    );

    // New: hints at the most likely cause without forcing the agent to
    // round-trip through `ls`/`stat` to figure it out.
    assert!(
        message.contains("alfonso.png"),
        "should mention the destination: {message}"
    );
    assert!(
        message.contains("already exists"),
        "should explicitly state the destination already exists: {message}"
    );

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

/// Counterpart: when the source is missing AND the destination is ALSO
/// missing, the agent didn't pre-rename anything — they just typed the
/// wrong path. The hint must NOT be added in that case (otherwise it
/// would mislead the agent into believing they had moved a non-existent
/// file).
#[test]
fn move_file_missing_source_without_existing_destination_keeps_short_message() {
    let tmp = tempfile::tempdir().expect("create temp dir");
    let root = tmp.path();

    let mut aft = AftProcess::spawn();
    configure(&mut aft, root);

    let src_abs = root.join("nope.png").display().to_string();
    let dst_abs = root.join("also-nope.png").display().to_string();
    let resp = send(
        &mut aft,
        json!({
            "id": "move-missing",
            "command": "move_file",
            "file": src_abs,
            "destination": dst_abs,
        }),
    );

    assert_eq!(resp["success"], false, "rename should fail: {resp:?}");
    assert_eq!(resp["code"], "file_not_found");

    let message = resp["message"].as_str().expect("error message string");

    assert!(
        message.contains("source file not found"),
        "should say source not found: {message}"
    );
    assert!(
        !message.contains("already exists"),
        "must NOT add the destination-exists hint when destination doesn't exist: {message}"
    );
    assert!(
        !message.contains("already moved"),
        "must NOT speculate about prior moves when there's no destination evidence: {message}"
    );

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