agent-file-tools 0.11.3

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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Handler for the `transaction` command: multi-file atomic edits with rollback.
//!
//! Applies edits to multiple files atomically. Phase model:
//! 1. Parse & validate all operations upfront
//! 2. Snapshot all existing files
//! 3. Apply each operation (write or edit_match)
//! 4. Validate syntax on all results
//! 5. Rollback all on any failure; success only if every file passes

use std::collections::HashSet;
use std::path::PathBuf;

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

/// A parsed operation ready for execution.
struct ParsedOp {
    file: PathBuf,
    kind: OpKind,
}

enum OpKind {
    Write {
        content: String,
    },
    EditMatch {
        match_str: String,
        replacement: String,
    },
}

/// Per-file result after a successful apply.
struct FileResult {
    file: String,
    syntax_valid: Option<bool>,
    formatted: bool,
    format_skipped_reason: Option<String>,
}

/// Handle a `transaction` request.
///
/// Params:
///   - `operations` (array, required) — each element is an object with:
///       - `file` (string) — target file path
///       - `command` (string) — "write" or "edit_match"
///       - For "write": `content` (string)
///       - For "edit_match": `match` (string) + `replacement` (string)
///   - `dry_run` (bool, optional) — preview without modifying disk
///
/// On success: `{ ok, files_modified, results: [{ file, syntax_valid, formatted, format_skipped_reason }] }`
/// On failure: `{ error: { code: "transaction_failed", message, failed_operation, rolled_back } }`
/// On dry-run: `{ ok, dry_run, diffs: [{ file, diff, syntax_valid }] }`
pub fn handle_transaction(req: &RawRequest, ctx: &AppContext) -> Response {
    // --- Parse operations ---
    let operations = match req.params.get("operations").and_then(|v| v.as_array()) {
        Some(ops) => ops,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "transaction: missing required param 'operations' (expected array)",
            );
        }
    };

    if operations.is_empty() {
        return Response::error(
            &req.id,
            "invalid_request",
            "transaction: 'operations' array must not be empty",
        );
    }

    // Validate all operations upfront before any mutations
    let mut parsed: Vec<ParsedOp> = Vec::with_capacity(operations.len());
    for (i, op) in operations.iter().enumerate() {
        match parse_operation(op, i) {
            Ok(p) => match validate_operation_path(p, &req.id, ctx) {
                Ok(validated) => parsed.push(validated),
                Err(resp) => return resp,
            },
            Err(msg) => return Response::error(&req.id, "invalid_request", msg),
        }
    }

    let dry_run = edit::is_dry_run(&req.params);

    // --- Dry-run path: compute diffs without touching disk ---
    if dry_run {
        return handle_dry_run(req, &parsed);
    }

    // --- Snapshot phase ---
    // Track which files existed (snapshotted) vs new (will be deleted on rollback).
    let mut snapshotted_files: Vec<PathBuf> = Vec::new();
    let mut new_files: Vec<PathBuf> = Vec::new();
    let mut seen: HashSet<PathBuf> = HashSet::new();

    for op in &parsed {
        if seen.contains(&op.file) {
            continue;
        }
        seen.insert(op.file.clone());

        if op.file.exists() {
            // Snapshot existing file — scoped borrow (D029)
            let snapshot_result = {
                let mut store = ctx.backup().borrow_mut();
                store.snapshot(&op.file, "transaction")
            };
            if let Err(e) = snapshot_result {
                return Response::error(&req.id, e.code(), e.to_string());
            }
            snapshotted_files.push(op.file.clone());
        } else {
            new_files.push(op.file.clone());
        }
    }

    // --- Apply phase ---
    let mut results: Vec<FileResult> = Vec::new();

    for (i, op) in parsed.iter().enumerate() {
        let new_content = match compute_new_content(op) {
            Ok(c) => c,
            Err(msg) => {
                let failures = rollback(ctx, &snapshotted_files, &new_files);
                return transaction_error(
                    &req.id,
                    i,
                    &snapshotted_files,
                    &new_files,
                    &msg,
                    &failures,
                );
            }
        };

        match edit::write_format_validate(&op.file, &new_content, &ctx.config(), &req.params) {
            Ok(wr) => {
                if let Ok(final_content) = std::fs::read_to_string(&op.file) {
                    ctx.lsp_notify_file_changed(&op.file, &final_content);
                }

                // Track this file as new if it was created by this operation
                // (in case earlier ops in the same transaction created it)
                if !snapshotted_files.contains(&op.file) && !new_files.contains(&op.file) {
                    new_files.push(op.file.clone());
                }
                results.push(FileResult {
                    file: op.file.display().to_string(),
                    syntax_valid: wr.syntax_valid,
                    formatted: wr.formatted,
                    format_skipped_reason: wr.format_skipped_reason,
                });
            }
            Err(e) => {
                let failures = rollback(ctx, &snapshotted_files, &new_files);
                return transaction_error(
                    &req.id,
                    i,
                    &snapshotted_files,
                    &new_files,
                    &format!("write failed: {}", e),
                    &failures,
                );
            }
        }
    }

    // --- Validate phase: check syntax_valid on all results ---
    for (i, result) in results.iter().enumerate() {
        if result.syntax_valid == Some(false) {
            let failures = rollback(ctx, &snapshotted_files, &new_files);
            return transaction_error(
                &req.id,
                i,
                &snapshotted_files,
                &new_files,
                &format!("syntax error in {}", result.file),
                &failures,
            );
        }
    }

    // --- Success ---
    let files_modified = results.len();
    let result_json: Vec<serde_json::Value> = results
        .iter()
        .map(|r| {
            let mut v = serde_json::json!({
                "file": r.file,
                "syntax_valid": r.syntax_valid,
                "formatted": r.formatted,
            });
            if let Some(ref reason) = r.format_skipped_reason {
                v["format_skipped_reason"] = serde_json::json!(reason);
            }
            v
        })
        .collect();

    log::debug!(
        "[aft] transaction: {} files modified successfully",
        files_modified
    );

    Response::success(
        &req.id,
        serde_json::json!({
            "ok": true,
            "files_modified": files_modified,
            "results": result_json,
        }),
    )
}

fn validate_operation_path(
    mut op: ParsedOp,
    req_id: &str,
    ctx: &AppContext,
) -> Result<ParsedOp, Response> {
    op.file = ctx.validate_path(req_id, &op.file)?;
    Ok(op)
}

/// Parse and validate a single operation object.
fn parse_operation(op: &serde_json::Value, index: usize) -> Result<ParsedOp, String> {
    let file = op
        .get("file")
        .and_then(|v| v.as_str())
        .ok_or_else(|| format!("transaction: operation[{}] missing 'file'", index))?;

    let command = op
        .get("command")
        .and_then(|v| v.as_str())
        .ok_or_else(|| format!("transaction: operation[{}] missing 'command'", index))?;

    let kind = match command {
        "write" => {
            let content = op.get("content").and_then(|v| v.as_str()).ok_or_else(|| {
                format!(
                    "transaction: operation[{}] 'write' requires 'content'",
                    index
                )
            })?;
            OpKind::Write {
                content: content.to_string(),
            }
        }
        "edit_match" => {
            let match_str = op.get("match").and_then(|v| v.as_str()).ok_or_else(|| {
                format!(
                    "transaction: operation[{}] 'edit_match' requires 'match'",
                    index
                )
            })?;
            let replacement = op
                .get("replacement")
                .and_then(|v| v.as_str())
                .ok_or_else(|| {
                    "transaction: edit_match operation requires 'replacement' field".to_string()
                })?;
            OpKind::EditMatch {
                match_str: match_str.to_string(),
                replacement: replacement.to_string(),
            }
        }
        other => {
            return Err(format!(
                "transaction: operation[{}] unknown command '{}' (expected 'write' or 'edit_match')",
                index, other
            ));
        }
    };

    Ok(ParsedOp {
        file: PathBuf::from(file),
        kind,
    })
}

/// Compute the new content for a single operation.
fn compute_new_content(op: &ParsedOp) -> Result<String, String> {
    match &op.kind {
        OpKind::Write { content } => Ok(content.clone()),
        OpKind::EditMatch {
            match_str,
            replacement,
        } => {
            let source = if op.file.exists() {
                std::fs::read_to_string(&op.file)
                    .map_err(|e| format!("failed to read {}: {}", op.file.display(), e))?
            } else {
                String::new()
            };

            let fuzzy_matches = crate::fuzzy_match::find_all_fuzzy(&source, match_str);

            if fuzzy_matches.is_empty() {
                return Err(format!(
                    "match '{}' not found in {}",
                    match_str,
                    op.file.display()
                ));
            }
            if fuzzy_matches.len() > 1 {
                return Err(format!(
                    "match '{}' is ambiguous ({} occurrences) in {}",
                    match_str,
                    fuzzy_matches.len(),
                    op.file.display()
                ));
            }

            let m = &fuzzy_matches[0];
            edit::replace_byte_range(
                &source,
                m.byte_start,
                m.byte_start + m.byte_len,
                replacement,
            )
            .map_err(|e| e.to_string())
        }
    }
}

/// Dry-run: compute per-file diffs without touching disk.
fn handle_dry_run(req: &RawRequest, ops: &[ParsedOp]) -> Response {
    let mut diffs: Vec<serde_json::Value> = Vec::with_capacity(ops.len());

    for op in ops {
        let original = if op.file.exists() {
            std::fs::read_to_string(&op.file).unwrap_or_default()
        } else {
            String::new()
        };

        let new_content = match compute_new_content_dry(op, &original) {
            Ok(c) => c,
            Err(msg) => {
                return Response::error(&req.id, "invalid_request", msg);
            }
        };

        let dr = edit::dry_run_diff(&original, &new_content, &op.file);
        diffs.push(serde_json::json!({
            "file": op.file.display().to_string(),
            "diff": dr.diff,
            "syntax_valid": dr.syntax_valid,
        }));
    }

    Response::success(
        &req.id,
        serde_json::json!({
            "ok": true,
            "dry_run": true,
            "diffs": diffs,
        }),
    )
}

/// Compute new content for dry-run (uses provided original instead of re-reading).
fn compute_new_content_dry(op: &ParsedOp, original: &str) -> Result<String, String> {
    match &op.kind {
        OpKind::Write { content } => Ok(content.clone()),
        OpKind::EditMatch {
            match_str,
            replacement,
        } => {
            let fuzzy_matches = crate::fuzzy_match::find_all_fuzzy(original, match_str);

            if fuzzy_matches.is_empty() {
                return Err(format!(
                    "match '{}' not found in {}",
                    match_str,
                    op.file.display()
                ));
            }
            if fuzzy_matches.len() > 1 {
                return Err(format!(
                    "match '{}' is ambiguous ({} occurrences) in {}",
                    match_str,
                    fuzzy_matches.len(),
                    op.file.display()
                ));
            }

            let m = &fuzzy_matches[0];
            edit::replace_byte_range(
                original,
                m.byte_start,
                m.byte_start + m.byte_len,
                replacement,
            )
            .map_err(|e| e.to_string())
        }
    }
}

/// Rollback failure information
struct RollbackFailure {
    file: String,
    action: String,
    error: String,
}

/// Rollback: restore snapshotted files (reverse order), delete new files.
/// Returns a list of files that failed to rollback.
fn rollback(
    ctx: &AppContext,
    snapshotted: &[PathBuf],
    new_files: &[PathBuf],
) -> Vec<RollbackFailure> {
    let mut failures = Vec::new();

    // Restore snapshotted files in reverse order
    for path in snapshotted.iter().rev() {
        let result = {
            let mut store = ctx.backup().borrow_mut();
            store.restore_latest(path)
        };
        if let Err(e) = result {
            log::warn!(
                "[aft] transaction rollback: failed to restore {}: {}",
                path.display(),
                e
            );
            failures.push(RollbackFailure {
                file: path.display().to_string(),
                action: "restore".to_string(),
                error: e.to_string(),
            });
        }
    }

    // Delete new files
    for path in new_files {
        if path.exists() {
            if let Err(e) = std::fs::remove_file(path) {
                log::warn!(
                    "[aft] transaction rollback: failed to delete new file {}: {}",
                    path.display(),
                    e
                );
                failures.push(RollbackFailure {
                    file: path.display().to_string(),
                    action: "delete".to_string(),
                    error: e.to_string(),
                });
            }
        }
    }

    failures
}

/// Build the structured error response for a failed transaction.
fn transaction_error(
    req_id: &str,
    failed_index: usize,
    snapshotted: &[PathBuf],
    new_files: &[PathBuf],
    message: &str,
    rollback_failures: &[RollbackFailure],
) -> Response {
    let mut rolled_back: Vec<serde_json::Value> = snapshotted
        .iter()
        .map(|p| serde_json::json!({ "file": p.display().to_string(), "action": "restored" }))
        .collect();

    for p in new_files {
        rolled_back.push(serde_json::json!({
            "file": p.display().to_string(),
            "action": "deleted",
        }));
    }

    log::debug!(
        "[aft] transaction failed at operation[{}]: {} — rolled back {} files",
        failed_index,
        message,
        rolled_back.len()
    );

    let mut data = serde_json::json!({
        "failed_operation": failed_index,
        "rolled_back": rolled_back,
    });

    if !rollback_failures.is_empty() {
        data["rollback_failures"] = serde_json::json!(rollback_failures
            .iter()
            .map(|f| serde_json::json!({
                "file": f.file,
                "action": f.action,
                "error": f.error,
            }))
            .collect::<Vec<_>>());
    }

    Response::error_with_data(req_id, "transaction_failed", message, data)
}