rho-coding-agent 1.18.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
use pretty_assertions::assert_eq;

use super::*;
use crate::tui::render::{entry_lines, render_entry_with_images};

fn line_text(line: &Line<'_>) -> String {
    line.spans
        .iter()
        .map(|span| span.content.as_ref())
        .collect()
}

fn no_images(_: usize, _: &[MarkdownImageSource]) -> Vec<(usize, FeedImage)> {
    Vec::new()
}

#[test]
fn caches_code_block_copy_target_and_raw_contents() {
    let mut cache = HistoryLineCache::default();
    let entries = vec![Entry::Assistant(
        "before\n```rust\nlet x = 1;\nprintln!(\"{x}\");\n```\nafter".into(),
    )];

    let blocks = cache.code_blocks(&entries, 40, 10, &no_images);

    assert_eq!(blocks.len(), 1);
    assert_eq!(blocks[0].text.as_ref(), "let x = 1;\nprintln!(\"{x}\");");
    assert_eq!(blocks[0].line, 1);
    assert_eq!(blocks[0].copy_columns, 32..38);
}

#[test]
fn caches_unicode_wrapped_lines_and_code_copy_target_without_rendering_drift() {
    let mut cache = HistoryLineCache::default();
    let entries = vec![Entry::Assistant("你好你好你好\n```text\nλ🙂\n```".into())];
    let expected_lines = entry_lines(&entries[0], 12, 10);

    let mut cached_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        12,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );
    let blocks = cache.code_blocks(&entries, 12, 10, &no_images);

    assert_eq!(cached_lines, expected_lines);
    assert_eq!(
        blocks,
        &[CachedCodeBlock {
            line: 2,
            copy_columns: 4..10,
            text: Arc::from("λ🙂"),
        }]
    );
}

#[test]
fn incrementally_extends_assistant_markdown_without_rendering_drift() {
    let mut cache = HistoryLineCache::default();
    let mut entries = vec![Entry::Assistant("intro\n\nheader | value\n".into())];
    let mut cached_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        32,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("--- | ---\nrow | `one`\n");
    cache.assistant_appended(0);
    cached_lines.clear();
    cache.extend_visible_lines(
        &entries,
        32,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );
    assert_eq!(cached_lines, entry_lines(&entries[0], 32, 10));

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("\n## streamed heading\n");
    cache.assistant_appended(0);
    cached_lines.clear();
    cache.extend_visible_lines(
        &entries,
        32,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );
    assert_eq!(cached_lines, entry_lines(&entries[0], 32, 10));

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("\n```rust\nlet answer = 42;\n");
    cache.assistant_appended(0);
    cached_lines.clear();
    cache.extend_visible_lines(
        &entries,
        32,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );
    assert_eq!(cached_lines, entry_lines(&entries[0], 32, 10));

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("println!(\"{answer}\");\n```\ndone\n");
    cache.assistant_appended(0);
    cached_lines.clear();
    cache.extend_visible_lines(
        &entries,
        32,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );

    assert_eq!(cached_lines, entry_lines(&entries[0], 32, 10));
    assert_eq!(cache.code_blocks(&entries, 32, 10, &no_images).len(), 1);
    assert!(cache.assistant_caches[0]
        .is_some_and(|cached| cached.stable_source_len > "intro\n\n".len()));
}

#[test]
fn streams_mermaid_as_source_then_caches_the_closed_diagram_by_width() {
    let mut cache = HistoryLineCache::default();
    let mut entries = vec![Entry::Assistant(
        "```mermaid\nflowchart LR\nA[Parse] --> B[Render]".into(),
    )];
    let mut cached_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        80,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );
    assert_eq!(cached_lines, entry_lines(&entries[0], 80, 10));
    assert!(cached_lines
        .iter()
        .any(|line| line_text(line).contains("flowchart LR")));

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("\n```");
    cache.assistant_appended(0);
    cached_lines.clear();
    cache.extend_visible_lines(
        &entries,
        80,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut cached_lines,
        &no_images,
    );

    assert_eq!(cached_lines, entry_lines(&entries[0], 80, 10));
    assert!(cached_lines
        .iter()
        .any(|line| line_text(line).contains("MERMAID")));
    assert!(!cached_lines
        .iter()
        .any(|line| line_text(line).contains("flowchart LR")));
    assert_eq!(
        cache.code_blocks(&entries, 80, 10, &no_images)[0]
            .text
            .as_ref(),
        "flowchart LR\nA[Parse] --> B[Render]"
    );

    let mut narrow_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        36,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut narrow_lines,
        &no_images,
    );
    assert_eq!(narrow_lines, entry_lines(&entries[0], 36, 10));
    assert_ne!(cached_lines, narrow_lines);
}

#[test]
fn resizing_moves_a_wide_diagram_between_art_and_explained_source() {
    let source = crate::tui::markdown::PHASE_CHAIN_FLOWCHART;
    let entries = vec![Entry::Assistant(format!("```mermaid\n{source}\n```"))];
    let mut cache = HistoryLineCache::default();

    let render_at = |cache: &mut HistoryLineCache, width: usize| {
        let mut lines = Vec::new();
        cache.extend_visible_lines(
            &entries,
            width,
            10,
            HistoryLineSlice {
                start: 0,
                count: usize::MAX,
            },
            &mut lines,
            &no_images,
        );
        lines.iter().map(line_text).collect::<Vec<_>>()
    };

    let wide = render_at(&mut cache, 100);
    assert!(wide.iter().any(|line| line.contains("Phase 5")), "{wide:?}");
    assert!(!wide.iter().any(|line| line.contains("flowchart LR")));

    let narrow = render_at(&mut cache, 40);
    assert!(
        narrow.iter().any(|line| line.contains("PANE TOO NARROW")),
        "{narrow:?}"
    );
    assert!(narrow.iter().any(|line| line.contains("flowchart LR")));

    assert_eq!(render_at(&mut cache, 100), wide);
    for width in [100, 40] {
        assert_eq!(
            cache.code_blocks(&entries, width, 10, &no_images)[0]
                .text
                .as_ref(),
            source
        );
    }
}

#[test]
fn invalidating_an_assistant_entry_refreshes_code_block_contents() {
    let mut cache = HistoryLineCache::default();
    let mut entries = vec![Entry::Assistant("```\nfirst\n```".into())];
    assert_eq!(
        cache.code_blocks(&entries, 30, 10, &no_images)[0]
            .text
            .as_ref(),
        "first"
    );

    entries[0] = Entry::Assistant("```\nsecond\n```".into());
    cache.invalidate_from(0);

    assert_eq!(
        cache.code_blocks(&entries, 30, 10, &no_images)[0]
            .text
            .as_ref(),
        "second"
    );
}

#[test]
fn markdown_image_placement_renders_after_images_resolve() {
    use image::{DynamicImage, ImageFormat};
    use rho_sdk::tool::ToolAsset;
    use std::io::Cursor;

    let image = DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(
        300,
        600,
        image::Rgba([20, 40, 60, 255]),
    ));
    let mut bytes = Cursor::new(Vec::new());
    image.write_to(&mut bytes, ImageFormat::Png).unwrap();
    let asset = ToolAsset::new("image/png", bytes.into_inner());
    let mut picker = ratatui_image::picker::Picker::halfblocks();
    picker.set_protocol_type(ratatui_image::picker::ProtocolType::Kitty);
    let feed = FeedImage::load(&asset, &picker).unwrap();

    let mut cache = HistoryLineCache::default();
    let mut entries = vec![Entry::Assistant(format!(
        "before\n\n![photo](photo.png)\n\n{}\n```\ncopy me\n```",
        (0..15)
            .map(|index| format!("stable {index}"))
            .collect::<Vec<_>>()
            .join("\n")
    ))];

    // Before any images are marked ready there is no placement.
    let placements = cache.visible_image_placements(&entries, 40, 20, 0, usize::MAX, &no_images);
    assert!(placements.is_empty());

    // Re-render with a resolver after the load invalidates this entry.
    cache.invalidate_from(0);
    let feed_clone = feed.clone();
    let mut lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        40,
        20,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut lines,
        &|_index, _sources| vec![(0, feed_clone.clone())],
    );

    let placements =
        cache.visible_image_placements(&entries, 40, 20, 0, usize::MAX, &|_index, _sources| {
            vec![(0, feed.clone())]
        });
    assert_eq!(placements.len(), 1, "expected one markdown image placement");
    assert_eq!(placements[0].row, 2);
    let with_images = render_entry_with_images(&entries[0], 40, 20, Some(&[(0, feed.clone())]));
    assert_eq!(
        cache.code_blocks(&entries, 40, 20, &|_index, _sources| vec![(
            0,
            feed.clone()
        )])[0]
            .line,
        with_images.code_blocks[0].top_line
    );

    let Entry::Assistant(text) = &mut entries[0] else {
        unreachable!();
    };
    text.push_str("\nstreamed tail");
    cache.assistant_appended(0);
    lines.clear();
    cache.extend_visible_lines(
        &entries,
        40,
        20,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut lines,
        &|_index, _sources| vec![(0, feed.clone())],
    );
    let expected = render_entry_with_images(&entries[0], 40, 20, Some(&[(0, feed.clone())])).lines;
    assert_eq!(lines, expected);
}

#[test]
fn transcript_entries_use_trailing_blank_only() {
    let mut cache = HistoryLineCache::default();
    let entries = vec![
        Entry::Assistant("Hello model output".into()),
        Entry::Reasoning(crate::tui::ReasoningEntry::new("thinking about it")),
        Entry::User("follow up".into()),
    ];

    let mut lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        60,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut lines,
        &no_images,
    );

    let expected = entries
        .iter()
        .flat_map(|entry| entry_lines(entry, 60, 10))
        .collect::<Vec<_>>();
    assert_eq!(lines, expected);

    // Each entry owns exactly one trailing spacer; consecutive blocks stay one
    // blank apart without stacking trailing+leading gaps.
    let mut content_starts = Vec::new();
    for (index, line) in lines.iter().enumerate() {
        if !line_text(line).trim().is_empty() {
            content_starts.push(index);
        }
    }
    assert!(content_starts.len() >= 3);
    // First content line of the first entry sits at the top of the entry.
    assert_eq!(content_starts[0], 0);
    for window in content_starts.windows(2) {
        // Within an entry content is contiguous; across entries there is one blank.
        assert!(
            window[1] == window[0] + 1 || window[1] == window[0] + 2,
            "unexpected gap between content rows {}:{} in {lines:?}",
            window[0],
            window[1],
            lines = lines.iter().map(line_text).collect::<Vec<_>>()
        );
    }

    let first_entry_end = entry_lines(&entries[0], 60, 10).len();
    assert!(!line_text(&lines[0]).trim().is_empty());
    assert!(line_text(&lines[first_entry_end - 1]).trim().is_empty());
    assert!(!line_text(&lines[first_entry_end]).trim().is_empty());
}

#[test]
fn open_stream_tail_omits_trailing_blank_until_closed() {
    use crate::tui::render::{render_entry_with_options, TrailingBlank};

    let mut cache = HistoryLineCache::default();
    let entries = vec![Entry::Assistant("Hello committed line\n".into())];

    cache.set_open_stream_tail(true);
    let open_count = cache.line_count(&entries, 60, 10, &no_images);
    let mut open_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        60,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut open_lines,
        &no_images,
    );
    assert_eq!(open_lines.len(), open_count);
    assert_eq!(
        open_lines,
        render_entry_with_options(&entries[0], 60, 10, TrailingBlank::Omit).lines
    );

    cache.set_open_stream_tail(false);
    let closed_count = cache.line_count(&entries, 60, 10, &no_images);
    assert_eq!(closed_count, open_count + 1);
    let mut closed_lines = Vec::new();
    cache.extend_visible_lines(
        &entries,
        60,
        10,
        HistoryLineSlice {
            start: 0,
            count: usize::MAX,
        },
        &mut closed_lines,
        &no_images,
    );
    assert_eq!(closed_lines, entry_lines(&entries[0], 60, 10));
}