rho-coding-agent 0.26.0

A lightweight agent harness inspired by Pi
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
use pretty_assertions::assert_eq;
use serde_json::json;
use tempfile::TempDir;

use super::*;

fn test_context() -> (TempDir, ToolContext) {
    let dir = tempfile::tempdir().unwrap();
    let ctx = ToolContext {
        cwd: dir.path().to_path_buf(),
        max_output_bytes: 12000,
    };
    (dir, ctx)
}

async fn call(args: serde_json::Value, ctx: ToolContext) -> Result<ToolResult, ToolError> {
    EditFile.call(args, ctx, "call_1".into()).await
}

#[tokio::test]
async fn replaces_unique_occurrence_with_legacy_arguments() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "alpha beta gamma").unwrap();

    let result = call(
        json!({"path": "sample.txt", "old_string": "beta", "new_string": "delta"}),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert!(result.ok);
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("sample.txt")).unwrap(),
        "alpha delta gamma"
    );
    assert!(result.content.contains("--- a/sample.txt"));
    assert!(result.content.contains("+++ b/sample.txt"));
    assert!(result.content.contains("-alpha beta gamma"));
    assert!(result.content.contains("+alpha delta gamma"));
}

#[tokio::test]
async fn applies_multiple_edits_across_multiple_files() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("first.txt"), "one old two old").unwrap();
    std::fs::write(ctx.cwd.join("second.txt"), "alpha").unwrap();

    let result = call(
        json!({"edits": [
            {
                "path": "first.txt",
                "old_string": "old",
                "new_string": "new",
                "expected_match_count": 2
            },
            {"path": "second.txt", "old_string": "alpha", "new_string": "beta"}
        ]}),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("first.txt")).unwrap(),
        "one new two new"
    );
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("second.txt")).unwrap(),
        "beta"
    );
    assert!(result
        .content
        .contains("edited 2 file(s); applied 2 edit(s), replaced 3 occurrence(s)"));
    assert!(result.content.contains("--- a/first.txt"));
    assert!(result.content.contains("--- a/second.txt"));
}

#[tokio::test]
async fn applies_multiple_edits_to_the_same_file_in_order() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "alpha beta").unwrap();

    call(
        json!({"edits": [
            {"path": "sample.txt", "old_string": "alpha", "new_string": "gamma"},
            {"path": "sample.txt", "old_string": "gamma beta", "new_string": "done"}
        ]}),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("sample.txt")).unwrap(),
        "done"
    );
}

#[tokio::test]
async fn applies_same_file_edits_across_path_aliases() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "alpha beta").unwrap();

    call(
        json!({"edits": [
            {"path": "sample.txt", "old_string": "alpha", "new_string": "gamma"},
            {"path": "./sample.txt", "old_string": "gamma beta", "new_string": "done"}
        ]}),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("sample.txt")).unwrap(),
        "done"
    );
}

#[tokio::test]
async fn applies_ordered_edits_across_hard_links() {
    let (_dir, ctx) = test_context();
    let original = ctx.cwd.join("original.txt");
    let alias = ctx.cwd.join("alias.txt");
    std::fs::write(&original, "alpha beta").unwrap();
    std::fs::hard_link(&original, &alias).unwrap();

    let result = call(
        json!({"edits": [
            {"path": "original.txt", "old_string": "alpha", "new_string": "gamma"},
            {"path": "alias.txt", "old_string": "gamma beta", "new_string": "done"}
        ]}),
        ctx,
    )
    .await
    .unwrap();

    assert_eq!(std::fs::read_to_string(original).unwrap(), "done");
    assert_eq!(std::fs::read_to_string(alias).unwrap(), "done");
    assert!(result
        .content
        .contains("edited 1 file(s); applied 2 edit(s)"));
}

#[cfg(target_os = "linux")]
#[tokio::test]
async fn rolls_back_earlier_file_when_a_later_write_fails() {
    fn file_changes(path: &std::path::Path, original: &str, updated: &str) -> FileChanges {
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(path)
            .unwrap();
        FileChanges {
            path: path.to_path_buf(),
            file: tokio::fs::File::from_std(file.try_clone().unwrap()),
            identity: Handle::from_file(file).unwrap(),
            display_path: path.display().to_string(),
            original: original.into(),
            updated: updated.into(),
        }
    }

    let (root, _ctx) = test_context();
    let first_path = root.path().join("first.txt");
    std::fs::write(&first_path, "original").unwrap();
    let mut files = vec![
        file_changes(&first_path, "original", "updated"),
        file_changes(std::path::Path::new("/dev/full"), "", "cannot write"),
    ];

    let error = write_all_or_rollback(&mut files).await.unwrap_err();

    assert_eq!(std::fs::read_to_string(first_path).unwrap(), "original");
    assert!(error.to_string().contains("failed to write /dev/full"));
    assert!(error
        .to_string()
        .contains("rollback also failed for /dev/full"));
}

#[tokio::test]
async fn writes_nothing_when_a_later_edit_is_missing() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("first.txt"), "old").unwrap();
    std::fs::write(ctx.cwd.join("second.txt"), "present").unwrap();

    let error = call(
        json!({"edits": [
            {"path": "first.txt", "old_string": "old", "new_string": "new"},
            {"path": "second.txt", "old_string": "absent", "new_string": "new"}
        ]}),
        ctx.clone(),
    )
    .await
    .unwrap_err();

    assert_eq!(
        error.to_string(),
        "edit 2 (second.txt) failed: missing match: found 0 occurrence(s), expected 1"
    );
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("first.txt")).unwrap(),
        "old"
    );
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("second.txt")).unwrap(),
        "present"
    );
}

#[tokio::test]
async fn writes_nothing_when_a_later_edit_is_ambiguous() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("first.txt"), "old").unwrap();
    std::fs::write(ctx.cwd.join("second.txt"), "old old").unwrap();

    let error = call(
        json!({"edits": [
            {"path": "first.txt", "old_string": "old", "new_string": "new"},
            {"path": "second.txt", "old_string": "old", "new_string": "new"}
        ]}),
        ctx.clone(),
    )
    .await
    .unwrap_err();

    assert_eq!(
        error.to_string(),
        "edit 2 (second.txt) failed: ambiguous match: found 2 occurrence(s), expected 1"
    );
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("first.txt")).unwrap(),
        "old"
    );
    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("second.txt")).unwrap(),
        "old old"
    );
}

#[tokio::test]
async fn reports_a_missing_expected_match() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "old").unwrap();

    let error = call(
        json!({"edits": [{
            "path": "sample.txt",
            "old_string": "old",
            "new_string": "new",
            "expected_match_count": 2
        }]}),
        ctx,
    )
    .await
    .unwrap_err();

    assert_eq!(
        error.to_string(),
        "edit 1 (sample.txt) failed: missing match: found 1 occurrence(s), expected 2"
    );
}

#[tokio::test]
async fn expected_match_count_is_supported_for_legacy_arguments() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "old old").unwrap();

    call(
        json!({
            "path": "sample.txt",
            "old_string": "old",
            "new_string": "new",
            "expected_match_count": 2
        }),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("sample.txt")).unwrap(),
        "new new"
    );
}

#[tokio::test]
async fn legacy_replace_all_remains_supported() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "old old").unwrap();

    let result = call(
        json!({
            "path": "sample.txt",
            "old_string": "old",
            "new_string": "new",
            "replace_all": true
        }),
        ctx.clone(),
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(ctx.cwd.join("sample.txt")).unwrap(),
        "new new"
    );
    assert!(result.content.contains("replaced 2 occurrence(s)"));
}

#[tokio::test]
async fn display_lines_keep_the_first_context_line() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "first\nold\nlast\n").unwrap();
    let args = json!({"edits": [{
        "path": "sample.txt",
        "old_string": "old",
        "new_string": "new"
    }]});

    let result = call(args.clone(), ctx.clone()).await.unwrap();

    assert_eq!(
        EditFile.display_lines(&args, &ctx, &result),
        vec!["edit_file sample.txt", "first\n-old\n+new\nlast"]
    );
}

#[tokio::test]
async fn display_lines_label_each_file_diff() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("first.txt"), "old one\n").unwrap();
    std::fs::write(ctx.cwd.join("second.txt"), "old two\n").unwrap();
    let args = json!({"edits": [
        {"path": "first.txt", "old_string": "old", "new_string": "new"},
        {"path": "second.txt", "old_string": "old", "new_string": "new"}
    ]});

    let result = call(args.clone(), ctx.clone()).await.unwrap();

    assert_eq!(
        EditFile.display_lines(&args, &ctx, &result),
        vec![
            "edit_file first.txt, second.txt",
            "first.txt\n-old one\n+new one\n\nsecond.txt\n-old two\n+new two"
        ]
    );
}

#[test]
fn display_content_lists_multiple_files() {
    let (_dir, ctx) = test_context();
    let args = json!({"edits": [
        {"path": "first.txt", "old_string": "a", "new_string": "b"},
        {"path": "second.txt", "old_string": "c", "new_string": "d"},
        {"path": "first.txt", "old_string": "e", "new_string": "f"}
    ]});

    assert_eq!(
        EditFile.display_content(&args, &ctx),
        Some("first.txt, second.txt".into())
    );
}

#[tokio::test]
async fn rejects_identical_old_and_new_string_with_edit_context() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "alpha").unwrap();

    let error = call(
        json!({"edits": [{
            "path": "sample.txt",
            "old_string": "alpha",
            "new_string": "alpha"
        }]}),
        ctx,
    )
    .await
    .unwrap_err();

    assert_eq!(
        error.to_string(),
        "edit 1 (sample.txt) failed: old_string and new_string are identical; nothing to change"
    );
}

#[tokio::test]
async fn rejects_zero_expected_match_count() {
    let (_dir, ctx) = test_context();
    std::fs::write(ctx.cwd.join("sample.txt"), "alpha").unwrap();

    let error = call(
        json!({"edits": [{
            "path": "sample.txt",
            "old_string": "alpha",
            "new_string": "beta",
            "expected_match_count": 0
        }]}),
        ctx,
    )
    .await
    .unwrap_err();

    assert_eq!(
        error.to_string(),
        "edit 1 (sample.txt) failed: expected_match_count must be at least 1"
    );
}

#[tokio::test]
async fn edits_crlf_file_with_lf_tool_strings() {
    let (root, ctx) = test_context();
    let path = root.path().join("hello.txt");
    std::fs::write(&path, "one\r\ntwo\r\nthree\r\n").unwrap();

    call(
        json!({"edits": [{
            "path": "hello.txt",
            "old_string": "one\ntwo\n",
            "new_string": "1\n2\n"
        }]}),
        ctx,
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(path).unwrap(),
        "1\r\n2\r\nthree\r\n"
    );
}

#[tokio::test]
async fn edits_lf_file_with_crlf_tool_strings() {
    let (root, ctx) = test_context();
    let path = root.path().join("hello.txt");
    std::fs::write(&path, "one\ntwo\nthree\n").unwrap();

    call(
        json!({"edits": [{
            "path": "hello.txt",
            "old_string": "one\r\ntwo\r\n",
            "new_string": "1\r\n2\r\n"
        }]}),
        ctx,
    )
    .await
    .unwrap();

    assert_eq!(std::fs::read_to_string(path).unwrap(), "1\n2\nthree\n");
}

#[tokio::test]
async fn edits_bare_cr_file_with_lf_tool_strings() {
    let (root, ctx) = test_context();
    let path = root.path().join("hello.txt");
    std::fs::write(&path, "one\rtwo\rthree\r").unwrap();

    call(
        json!({"edits": [{
            "path": "hello.txt",
            "old_string": "one\ntwo\n",
            "new_string": "1\n2\n"
        }]}),
        ctx,
    )
    .await
    .unwrap();

    assert_eq!(std::fs::read_to_string(path).unwrap(), "1\r2\rthree\r");
}

#[tokio::test]
async fn multiple_replacements_preserve_mixed_line_endings_outside_matches() {
    let (root, ctx) = test_context();
    let path = root.path().join("hello.txt");
    std::fs::write(&path, "old\r\nkeep\nold\r\n").unwrap();

    call(
        json!({"edits": [{
            "path": "hello.txt",
            "old_string": "old\n",
            "new_string": "new\n",
            "expected_match_count": 2
        }]}),
        ctx,
    )
    .await
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(path).unwrap(),
        "new\r\nkeep\nnew\r\n"
    );
}