agent-file-tools 0.47.1

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
//! Handler for the `move_file` command: rename/move a file with backup.

use std::fs;
use std::path::{Path, PathBuf};

use lsp_types::FileChangeType;

use crate::context::AppContext;
use crate::edit;
use crate::protocol::{RawRequest, Response};

/// Handle a `move_file` request.
///
/// Params:
///   - `file` (string, required) — source file path
///   - `destination` (string, required) — destination file path
///
/// Returns: `{ file, destination, moved, backup_id }`
pub fn handle_move_file(req: &RawRequest, ctx: &AppContext) -> Response {
    let op_id = crate::backup::new_op_id();
    let file = match req.params.get("file").and_then(|v| v.as_str()) {
        Some(f) => f,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "move_file: missing required param 'file'",
            );
        }
    };

    let destination = match req.params.get("destination").and_then(|v| v.as_str()) {
        Some(d) => d,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "move_file: missing required param 'destination'",
            );
        }
    };

    let src_path = match ctx.validate_path(&req.id, Path::new(file)) {
        Ok(path) => path,
        Err(resp) => return resp,
    };
    let dst_path = match ctx.validate_path(&req.id, Path::new(destination)) {
        Ok(path) => path,
        Err(resp) => return resp,
    };

    let source_metadata = match std::fs::symlink_metadata(&src_path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            // When the source is missing AND the destination already exists, the
            // most likely cause is that this rename was already done earlier in
            // the session (or by another process). Surfacing this distinction
            // saves the agent a round-trip to discover it via `ls` or stat.
            if std::fs::symlink_metadata(&dst_path).is_ok() {
                return Response::error(
                    &req.id,
                    "file_not_found",
                    format!(
                        "move_file: source file not found: {}. Destination '{}' already exists \
                         — was this file already moved earlier? Verify with `read` before retrying.",
                        file, destination
                    ),
                );
            }
            return Response::error(
                &req.id,
                "file_not_found",
                format!("move_file: source file not found: {}", file),
            );
        }
        Err(error) => {
            return Response::error(
                &req.id,
                "io_error",
                format!("move_file: failed to stat source file: {}", error),
            );
        }
    };

    if source_metadata.is_dir() {
        return Response::error(
            &req.id,
            "invalid_request",
            format!("move_file: '{}' is a directory, not a file", file),
        );
    }

    if std::fs::symlink_metadata(&dst_path).is_ok() {
        return Response::error(
            &req.id,
            "invalid_request",
            format!("move_file: destination already exists: {}", destination),
        );
    }

    // Backup source before moving
    let backup_id = match edit::auto_backup(
        ctx,
        req.session(),
        src_path.as_path(),
        "move_file: pre-move backup",
        Some(&op_id),
    ) {
        Ok(id) => id,
        Err(e) => {
            return Response::error(&req.id, e.code(), e.to_string());
        }
    };

    if let Err(e) = ctx.backup().lock().snapshot_op_tombstone(
        req.session(),
        &op_id,
        &dst_path,
        "move_file: destination created during move",
    ) {
        ctx.backup()
            .lock()
            .discard_operation_entries(req.session(), &op_id);
        return Response::error(&req.id, e.code(), e.to_string());
    }

    // Create parent directories for destination
    if let Some(parent) = dst_path.parent() {
        if !parent.exists() {
            if let Err(e) = std::fs::create_dir_all(parent) {
                ctx.backup()
                    .lock()
                    .discard_operation_entries(req.session(), &op_id);
                return Response::error(
                    &req.id,
                    "io_error",
                    format!("move_file: failed to create directories: {}", e),
                );
            }
        }
    }

    // Move the file
    let move_outcome = match move_file_on_disk(&src_path, &dst_path) {
        MoveOutcome::Moved => MoveOutcome::Moved,
        MoveOutcome::CopiedSourceDeleteFailed(message) => {
            crate::slog_warn!("move_file: copied but failed to remove source: {}", message);
            MoveOutcome::CopiedSourceDeleteFailed(message)
        }
        MoveOutcome::Failed(message) => {
            ctx.backup()
                .lock()
                .discard_operation_entries(req.session(), &op_id);
            return Response::error(
                &req.id,
                "io_error",
                format!("move_file: failed to move file: {}", message),
            );
        }
    };

    log::debug!("move_file: {} -> {}", file, destination);

    if move_outcome == MoveOutcome::Moved {
        let source_for_lsp = unresolved_existing_path(&src_path, Path::new(file));
        ctx.lsp_notify_watched_config_file(&source_for_lsp, FileChangeType::DELETED);
    }
    ctx.lsp_notify_watched_config_file(&dst_path, FileChangeType::CREATED);

    let mut result = move_success_result(file, destination, move_outcome);

    if let Some(ref id) = backup_id {
        result["backup_id"] = serde_json::json!(id);
    }

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

#[derive(Debug, PartialEq, Eq)]
enum MoveOutcome {
    Moved,
    CopiedSourceDeleteFailed(String),
    Failed(String),
}

fn move_file_on_disk(src_path: &Path, dst_path: &Path) -> MoveOutcome {
    match fs::rename(src_path, dst_path) {
        Ok(()) => MoveOutcome::Moved,
        Err(rename_error) => {
            let source_is_symlink = fs::symlink_metadata(src_path)
                .is_ok_and(|metadata| metadata.file_type().is_symlink());
            if source_is_symlink {
                return move_symlink_after_rename_failure(src_path, dst_path, &rename_error);
            }

            match fs::copy(src_path, dst_path) {
                Ok(_) => remove_source_after_fallback(src_path),
                Err(_) => MoveOutcome::Failed(rename_error.to_string()),
            }
        }
    }
}

fn move_symlink_after_rename_failure(
    src_path: &Path,
    dst_path: &Path,
    rename_error: &std::io::Error,
) -> MoveOutcome {
    let target = match fs::read_link(src_path) {
        Ok(target) => target,
        Err(_) => return MoveOutcome::Failed(rename_error.to_string()),
    };

    // std::fs::copy follows a symlink and would silently turn the destination
    // into a regular file. Recreate the link itself, preserving its target text:
    // a relative target stays relative and can legitimately dangle after a move.
    let create_result = create_symlink(&target, src_path, dst_path);
    let create_result = if create_result
        .as_ref()
        .is_err_and(|error| error.kind() == std::io::ErrorKind::AlreadyExists)
        && rename_error.kind() == std::io::ErrorKind::CrossesDevices
    {
        match fs::remove_file(dst_path) {
            Ok(()) => create_symlink(&target, src_path, dst_path),
            Err(error) => Err(error),
        }
    } else {
        create_result
    };

    match create_result {
        Ok(()) => remove_source_after_fallback(src_path),
        Err(_) => MoveOutcome::Failed(rename_error.to_string()),
    }
}

fn remove_source_after_fallback(src_path: &Path) -> MoveOutcome {
    match fs::remove_file(src_path) {
        Ok(()) => MoveOutcome::Moved,
        Err(remove_error) => MoveOutcome::CopiedSourceDeleteFailed(remove_error.to_string()),
    }
}

#[cfg(unix)]
fn create_symlink(target: &Path, _src_path: &Path, dst_path: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(target, dst_path)
}

#[cfg(windows)]
fn create_symlink(target: &Path, src_path: &Path, dst_path: &Path) -> std::io::Result<()> {
    let resolved_target = if target.is_absolute() {
        target.to_path_buf()
    } else {
        src_path
            .parent()
            .unwrap_or_else(|| Path::new(""))
            .join(target)
    };
    if fs::metadata(resolved_target).is_ok_and(|metadata| metadata.is_dir()) {
        std::os::windows::fs::symlink_dir(target, dst_path)
    } else {
        // Dangling targets cannot reveal their type; file links are the safest
        // default and match this command's file-only contract.
        std::os::windows::fs::symlink_file(target, dst_path)
    }
}

#[cfg(not(any(unix, windows)))]
fn create_symlink(_target: &Path, _src_path: &Path, _dst_path: &Path) -> std::io::Result<()> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "symlink moves are unsupported on this platform",
    ))
}

fn unresolved_existing_path(resolved_path: &Path, requested_path: &Path) -> PathBuf {
    if requested_path.is_absolute() {
        requested_path.to_path_buf()
    } else {
        resolved_path.to_path_buf()
    }
}

fn move_success_result(
    file: &str,
    destination: &str,
    move_outcome: MoveOutcome,
) -> serde_json::Value {
    let mut result = serde_json::json!({
        "file": file,
        "destination": destination,
        "moved": true,
    });

    if let MoveOutcome::CopiedSourceDeleteFailed(message) = move_outcome {
        result["complete"] = serde_json::json!(false);
        result["source_delete_failed"] = serde_json::json!(true);
        result["warning"] = serde_json::json!(format!(
            "destination was written, but source file could not be deleted after copy: {message}. Both paths now exist; retry deleting the source or accept the duplicate."
        ));
    }

    result
}

#[cfg(test)]
mod tests {
    use super::{move_success_result, MoveOutcome};

    #[cfg(unix)]
    use super::move_symlink_after_rename_failure;
    #[cfg(unix)]
    use std::io::{Error, ErrorKind};
    #[cfg(unix)]
    use std::path::Path;

    #[test]
    fn copied_but_source_delete_failed_shape_marks_partial_success() {
        let result = move_success_result(
            "src.txt",
            "dst.txt",
            MoveOutcome::CopiedSourceDeleteFailed("permission denied".to_string()),
        );

        assert_eq!(result["moved"], true);
        assert_eq!(result["complete"], false);
        assert_eq!(result["source_delete_failed"], true);
        assert!(result["warning"]
            .as_str()
            .is_some_and(|warning| warning.contains("Both paths now exist")));
    }

    #[cfg(unix)]
    #[test]
    fn symlink_fallback_preserves_relative_target_text() {
        let temp = tempfile::tempdir().expect("tempdir");
        let source_dir = temp.path().join("source");
        let destination_dir = temp.path().join("destination");
        std::fs::create_dir_all(&source_dir).expect("source directory");
        std::fs::create_dir_all(&destination_dir).expect("destination directory");
        std::fs::write(source_dir.join("target.txt"), "target contents\n").expect("target file");
        let source = source_dir.join("link.txt");
        let destination = destination_dir.join("link.txt");
        let target = Path::new("target.txt");
        std::os::unix::fs::symlink(target, &source).expect("source symlink");

        let outcome = move_symlink_after_rename_failure(
            &source,
            &destination,
            &Error::from(ErrorKind::CrossesDevices),
        );

        assert_eq!(outcome, MoveOutcome::Moved);
        assert!(std::fs::symlink_metadata(&source).is_err());
        assert!(std::fs::symlink_metadata(&destination)
            .expect("destination metadata")
            .file_type()
            .is_symlink());
        assert_eq!(
            std::fs::read_link(&destination).expect("link target"),
            target
        );
    }

    #[cfg(unix)]
    #[test]
    fn symlink_fallback_moves_dangling_link() {
        let temp = tempfile::tempdir().expect("tempdir");
        let source = temp.path().join("source-link");
        let destination = temp.path().join("destination-link");
        let target = Path::new("missing/target.txt");
        std::os::unix::fs::symlink(target, &source).expect("dangling source symlink");

        let outcome = move_symlink_after_rename_failure(
            &source,
            &destination,
            &Error::from(ErrorKind::CrossesDevices),
        );

        assert_eq!(outcome, MoveOutcome::Moved);
        assert!(std::fs::symlink_metadata(&source).is_err());
        assert!(std::fs::symlink_metadata(&destination)
            .expect("destination metadata")
            .file_type()
            .is_symlink());
        assert_eq!(
            std::fs::read_link(&destination).expect("link target"),
            target
        );
    }

    #[cfg(unix)]
    #[test]
    fn cross_device_symlink_fallback_replaces_existing_destination() {
        let temp = tempfile::tempdir().expect("tempdir");
        let source = temp.path().join("source-link");
        let destination = temp.path().join("destination-link");
        let target = Path::new("target.txt");
        std::os::unix::fs::symlink(target, &source).expect("source symlink");
        std::fs::write(&destination, "stale destination\n").expect("existing destination");

        let outcome = move_symlink_after_rename_failure(
            &source,
            &destination,
            &Error::from(ErrorKind::CrossesDevices),
        );

        assert_eq!(outcome, MoveOutcome::Moved);
        assert!(std::fs::symlink_metadata(&destination)
            .expect("destination metadata")
            .file_type()
            .is_symlink());
        assert_eq!(
            std::fs::read_link(&destination).expect("link target"),
            target
        );
    }
}