cosh-tools 0.1.4

Tools crate for cosh - coding agent for the terminal
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use super::super::types::{AstEditOp, FsAstEdit, FsMetadata};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

/// A unique per-test temp dir that is removed on drop (even when an assertion
/// fails mid-test), keeping the tests hermetic and out of the repo tree.
struct TempDir(PathBuf);

static DIR_SEQ: AtomicU64 = AtomicU64::new(0);

impl TempDir {
    fn new() -> Self {
        let dir = std::env::temp_dir().join(format!(
            "cosh_ast_test_{}_{}",
            std::process::id(),
            DIR_SEQ.fetch_add(1, Ordering::Relaxed),
        ));
        std::fs::create_dir_all(&dir).expect("create temp dir");
        Self(dir)
    }

    fn root(&self) -> &Path {
        &self.0
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn meta(root: &Path) -> FsMetadata {
    FsMetadata {
        root: root.to_path_buf(),
        allowlist: None,
        blocklist: None,
    }
}

fn abs_path(root: &Path, name: &str) -> String {
    root.join(name).to_string_lossy().into_owned()
}

fn ops(pairs: &[(&str, &str)]) -> Vec<AstEditOp> {
    pairs
        .iter()
        .map(|(pat, out)| AstEditOp {
            pat: pat.to_string(),
            out: out.to_string(),
        })
        .collect()
}

#[tokio::test]
async fn ast_edit_rust_statement_rewrite_applies_once() {
    // A statement-level Rust pattern only rewrites full statements (including
    // the trailing semicolon via the contextual pattern), never a call nested
    // inside a `let` initialiser, and never twice (no double application across
    // the normal + contextual patterns).
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(
        &file,
        "fn main() {\n    foo(1);\n    let x = foo(2);\n    foo(3);\n}\n",
    )
    .unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("foo($A);", "qux($A);")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "fn main() {\n    qux(1);\n    let x = foo(2);\n    qux(3);\n}\n"
    );
}

#[tokio::test]
async fn ast_edits_rename_call_with_multi_metavar_in_rust() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {\n    oldApi(1, 2);\n    oldApi(x);\n}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("oldApi($$$ARGS)", "newApi($$$ARGS)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "fn main() {\n    newApi(1, 2);\n    newApi(x);\n}\n"
    );
}

#[tokio::test]
async fn ast_edits_delete_call_by_empty_out() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {\n    ignore(1);\n    log();\n}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("ignore($$$ARGS)", "")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "fn main() {\n    ;\n    log();\n}\n"
    );
}

#[tokio::test]
async fn ast_edit_enforces_metavariable_identity_in_js() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.js");
    std::fs::write(
        &file,
        "function f(x){ return x && x(); }\nfunction g(){ return a && b(); }\n",
    )
    .unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("$A && $A()", "$A?.()")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    // Only `x && x()` matches (same metavariable); `a && b()` is left alone.
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "function f(x){ return x?.(); }\nfunction g(){ return a && b(); }\n"
    );
}

#[tokio::test]
async fn ast_edit_swaps_two_arguments_in_js() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.js");
    std::fs::write(&file, "assertEqual(a, b);\nassertEqual(x, y);\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("assertEqual($A, $B)", "assertEqual($B, $A)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "assertEqual(b, a);\nassertEqual(y, x);\n"
    );
}

#[tokio::test]
async fn ast_edit_rewrites_python_print_to_logger() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.py");
    std::fs::write(&file, "print(\"hi\")\nprint(1, 2)\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("print($$$ARGS)", "logger.info($$$ARGS)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "logger.info(\"hi\")\nlogger.info(1, 2)\n"
    );
}

#[tokio::test]
async fn ast_edit_applies_multiple_ops_in_order() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {\n    foo(1);\n    bar(2);\n}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[
                ("foo($$$ARGS)", "qux($$$ARGS)"),
                ("bar($$$ARGS)", "baz($$$ARGS)"),
            ]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "fn main() {\n    qux(1);\n    baz(2);\n}\n"
    );
}

#[tokio::test]
async fn ast_edit_reports_no_files_touched_when_no_match() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() { hello(); }\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("missing($$$ARGS)", "nothing($$$ARGS)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    // No file was modified, but the no-match feedback must still surface to the
    // model instead of being swallowed as silence.
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].first_changed_line, None);
    assert_eq!(results[0].diff, None);
    assert!(
        results[0]
            .warnings
            .iter()
            .any(|w| w.contains("no AST matches found"))
    );
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "fn main() { hello(); }\n"
    );
}

#[tokio::test]
async fn ast_edit_rejects_unsupported_file() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.xyz");
    std::fs::write(&file, "hello\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("x", "y")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    // Unsupported files are skipped, so the result is just empty (no error).
    assert!(result.is_ok());
    assert!(result.unwrap().is_empty());
}

#[tokio::test]
async fn ast_edit_reports_first_changed_line() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {\n    untouched(0);\n    rename(a);\n}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("rename($$$A)", "qux($$$A)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await
    .expect("apply should succeed");
    assert_eq!(result.len(), 1);
    // `rename(a)` lives on line 3 of the original file; the reported line must
    // point at it, not at an offset skewed by the rewrite (the preceding op on
    // line 2 is untouched and must not shift the reported line).
    assert_eq!(result[0].first_changed_line, Some(3));
}

#[tokio::test]
async fn ast_edit_first_changed_line_across_two_ops_and_utf8() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.py");
    std::fs::write(&file, "import sys\nwarn(\"ol\u{e1}\")\nhelper(\"x\")\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            // First op rewrites a multi-byte UTF-8 string (`\u{e1}`); second op
            // then matches a later line against already-rewritten text. Offsets
            // for the second op are no longer the identity mapping, so this
            // exercises the anchor remap.
            ops: ops(&[("warn($$$A)", "log($$$A)"), ("helper($A)", "run($A)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await
    .expect("apply should succeed");
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].first_changed_line, Some(2));
    // Content correctness across the two rewrites, with the multi-byte char kept.
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "import sys\nlog(\"ol\u{e1}\")\nrun(\"x\")\n"
    );
}

#[tokio::test]
async fn ast_edit_rejects_empty_pattern() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("", "x")]),
            paths: vec![file],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("non-empty pattern"));
}

#[tokio::test]
async fn ast_edit_rejects_duplicate_pattern() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("foo($A)", "bar($A)"), ("foo($A)", "baz($A)")]),
            paths: vec![file],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("duplicate rewrite pattern"));
}

#[tokio::test]
async fn ast_edit_empty_file_metavar_pattern() {
    // Regression: an empty supported file + a metavariable pattern used to panic
    // (`anchor` is empty while ast-grep yields a zero-length root match). It must
    // now be a clean no-op with a warning, not a crash.
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "empty.rs");
    std::fs::write(&file, "").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("$A", "x")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(
        result.is_ok(),
        "empty file must not panic: {:?}",
        result.err()
    );
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].first_changed_line, None);
    assert_eq!(results[0].diff, None);
    assert!(
        results[0]
            .warnings
            .iter()
            .any(|w| w.contains("no AST matches found"))
    );
    // The empty file is left untouched.
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "");
}

#[cfg(unix)]
#[tokio::test]
async fn ast_edit_does_not_follow_symlinks() {
    // A symlinked directory inside the tree must not be traversed (it could
    // escape the root or cycle), and a symlinked file must not be rewritten.
    let dir = TempDir::new();
    let target = std::env::temp_dir().join(format!(
        "cosh_ast_outside_{}_{}",
        std::process::id(),
        DIR_SEQ.fetch_add(1, Ordering::Relaxed),
    ));
    std::fs::create_dir_all(&target).unwrap();
    let outside = target.join("outside.rs");
    std::fs::write(&outside, "fn main() { foo(1); }\n").unwrap();

    let root = dir.root();
    std::os::unix::fs::symlink(&target, root.join("linked")).expect("make file symlink");
    let result = crate::fs::ast_edit::ast_edit(
        meta(root),
        FsAstEdit {
            ops: ops(&[("foo($A)", "bar($A)")]),
            paths: vec![root.to_string_lossy().into_owned()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    // Nothing outside was touched.
    assert_eq!(
        std::fs::read_to_string(&outside).unwrap(),
        "fn main() { foo(1); }\n"
    );
    let _ = std::fs::remove_dir_all(&target);
}

#[tokio::test]
async fn ast_edit_second_op_after_full_delete() {
    // Regression: the first op deletes the whole file (so `text` becomes empty
    // and `anchor` empty), then a second op matches against the empty buffer. The
    // engine must not index an empty `anchor` (no panic) and must keep the first
    // changed line well-defined.
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    std::fs::write(&file, "fn main() {}\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("fn main() {}", ""), ("$A", "x")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_ok(), "must not panic: {:?}", result.err());
    let results = result.unwrap();
    // Whatever the exact rewrite, the engine must not have panicked and the
    // full-delete + second-op flow must yield a valid, reported edit.
    assert!(!results.is_empty());
    assert_eq!(results[0].first_changed_line, Some(1));
}

#[tokio::test]
async fn ast_edit_refuses_auto_generated_file() {
    let dir = TempDir::new();
    let file = abs_path(dir.root(), "sample.rs");
    let original = "// Code generated by tool. DO NOT EDIT.\nfn main() { foo(1); }\n";
    std::fs::write(&file, original).unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("foo($A)", "bar($A)")]),
            paths: vec![file.clone()],
            ..Default::default()
        },
    )
    .await;
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.contains("auto-generated"),
        "expected an auto-generated refusal, got: {err}"
    );
    assert_eq!(std::fs::read_to_string(&file).unwrap(), original);
}

#[tokio::test]
async fn ast_edit_skips_nonexistent_path() {
    let dir = TempDir::new();
    // No file is created; the explicit path does not exist.
    let missing = abs_path(dir.root(), "missing.rs");

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("foo($A)", "bar($A)")]),
            paths: vec![missing],
            ..Default::default()
        },
    )
    .await;
    // A nonexistent path yields nothing rather than a hard error (upstream-like).
    assert!(result.is_ok());
    assert!(result.unwrap().is_empty());
}

#[tokio::test]
async fn ast_edit_caps_files_with_warning() {
    let dir = TempDir::new();
    let a = abs_path(dir.root(), "a.rs");
    let b = abs_path(dir.root(), "b.rs");
    std::fs::write(&a, "fn main() { foo(1); }\n").unwrap();
    std::fs::write(&b, "fn main() { foo(2); }\n").unwrap();

    let result = crate::fs::ast_edit::ast_edit(
        meta(dir.root()),
        FsAstEdit {
            ops: ops(&[("foo($A)", "bar($A)")]),
            paths: vec![dir.root().to_string_lossy().into_owned()],
            max_files: Some(1),
        },
    )
    .await;
    assert!(result.is_ok(), "unexpected error: {:?}", result.err());
    let results = result.unwrap();
    // Only one of the two matched files is edited, and the cap is surfaced.
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .iter()
            .any(|w| w.contains("limit reached"))
    );
    let edited = results[0].path.contains("a.rs") || results[0].path.contains("b.rs");
    assert!(
        edited,
        "one of the capped files should be reported as edited"
    );
}