rho-coding-agent 1.48.0

A lightweight agent harness inspired by Pi
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
use super::*;
use crate::tui::{PickerAction, PickerItem, UiPicker};
use pretty_assertions::assert_eq;
use ratatui::style::{Modifier, Style};
use ratatui::text::Span;

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

#[test]
fn display_width_ignores_control_characters_filtered_by_ratatui() {
    assert_eq!(display_width("left\tright"), 9);
    assert_eq!(display_width("left\rright"), 9);
    assert_eq!(display_width("left\u{1b}right"), 9);
}

// Covers: end truncation must keep its display-width and one-line output contract.
// Owner: pure TUI layout policy.
#[test]
fn truncate_one_line_matches_expected_outputs() {
    let cases = [
        ("zero width", "abc", 0, ""),
        ("width one", "abc", 1, "…"),
        ("empty", "", 4, ""),
        ("ASCII exact fit", "abc", 3, "abc"),
        ("ASCII truncation", "abcdef", 4, "abc…"),
        ("wide exact fit", "界a", 3, "界a"),
        ("wide truncation", "界ab", 3, "界…"),
        ("combining exact fit", "e\u{301}x", 2, "e\u{301}x"),
        ("combining truncation", "e\u{301}xy", 2, "e\u{301}…"),
        ("one newline", "ab\ncd", 5, "ab cd"),
        ("multiple newlines", "a\n\nbc", 4, "a  …"),
    ];

    for (name, text, width, expected) in cases {
        assert_eq!(truncate_one_line(text, width), expected, "{name}");
    }
}

// Covers: front truncation must keep its display-width and one-line output contract.
// Owner: pure TUI layout policy.
#[test]
fn truncate_keep_end_matches_expected_outputs() {
    let cases = [
        ("zero width", "abc", 0, ""),
        ("width one", "abc", 1, "…"),
        ("empty", "", 4, ""),
        ("ASCII exact fit", "abc", 3, "abc"),
        ("ASCII truncation", "abcdef", 4, "…def"),
        ("wide exact fit", "a界", 3, "a界"),
        ("wide truncation", "ab界", 3, "…界"),
        ("combining exact fit", "be\u{301}", 2, "be\u{301}"),
        ("combining truncation", "xabe\u{301}", 3, "…be\u{301}"),
        ("one newline", "ab\ncd", 5, "ab cd"),
        ("multiple newlines", "a\n\nbc", 4, "… bc"),
    ];

    for (name, text, width, expected) in cases {
        assert_eq!(truncate_keep_end(text, width), expected, "{name}");
    }
}

#[test]
fn complete_visual_prefix_preserves_trailing_newline_state() {
    assert_eq!(complete_visual_prefix_byte_index("a\n", 10), "a\n".len());
    assert_eq!(
        complete_visual_prefix_byte_index("a\n\n", 10),
        "a\n\n".len()
    );
    assert_eq!(complete_visual_prefix_byte_index("a\nb", 10), "a\n".len());
}

#[test]
fn complete_visual_prefix_keeps_multibyte_boundaries() {
    assert_eq!(complete_visual_prefix_byte_index("éa", 2), "éa".len());
    assert_eq!(complete_visual_prefix_byte_index("éab", 2), "éa".len());
}

#[test]
fn complete_visual_prefix_wraps_at_exact_width() {
    assert_eq!(complete_visual_prefix_byte_index("abc", 3), 3);
    assert_eq!(complete_visual_prefix_byte_index("abcd", 3), 3);
    assert_eq!(complete_visual_prefix_byte_index("abcdef", 3), 6);
}

// Covers: soft wrap must not put the break space at the start of the next line.
// Owner: pure unit (wrap layout math)
#[test]
fn wrapped_text_prefers_whitespace_boundaries() {
    let cases = [
        ("hello wide world", 10, vec!["hello wide", "world"]),
        // Overflow lands on the break space itself.
        ("models.dev mapping.", 10, vec!["models.dev", "mapping."]),
        // Extra spaces after a width-split must not indent the continuation.
        ("hello  world", 6, vec!["hello ", "world"]),
    ];

    for (text, width, expected) in cases {
        let mut lines = Vec::new();
        push_wrapped_text(&mut lines, text, width, Style::default(), LineFill::Natural);
        let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
        assert_eq!(
            rendered,
            expected
                .iter()
                .map(|part| (*part).to_string())
                .collect::<Vec<_>>(),
            "text {text:?} width {width}"
        );
    }
}

#[test]
fn complete_visual_prefix_prefers_whitespace_boundaries() {
    assert_eq!(
        complete_visual_prefix_byte_index("hello wide", 8),
        "hello ".len()
    );
    assert_eq!(
        complete_visual_prefix_byte_index("hello wide", 10),
        "hello wide".len()
    );
}

#[test]
fn wrapped_text_preserves_leading_repeated_and_trailing_whitespace() {
    let mut lines = Vec::new();
    push_wrapped_text(
        &mut lines,
        "  indented\na  b\ntrail  ",
        20,
        Style::default(),
        LineFill::Natural,
    );

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(
        rendered,
        vec![
            "  indented".to_string(),
            "a  b".to_string(),
            "trail  ".to_string()
        ]
    );
}

#[test]
fn wrapped_text_preserves_tabs_and_whitespace_only_lines() {
    let mut lines = Vec::new();
    push_wrapped_text(
        &mut lines,
        "\tindented\n   ",
        20,
        Style::default(),
        LineFill::Natural,
    );

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(rendered, vec!["\tindented".to_string(), "   ".to_string()]);
}

#[test]
fn wrapped_text_preserves_whitespace_when_breaking_at_boundary() {
    let mut lines = Vec::new();
    push_wrapped_text(
        &mut lines,
        "hello   wide",
        8,
        Style::default(),
        LineFill::Natural,
    );

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(rendered, vec!["hello   ".to_string(), "wide".to_string()]);
}

#[test]
fn complete_visual_prefix_and_rendering_agree_on_whitespace_boundary() {
    let text = "hello   wide";
    let split = complete_visual_prefix_byte_index(text, 8);
    let mut lines = Vec::new();
    push_wrapped_text(&mut lines, text, 8, Style::default(), LineFill::Natural);

    assert_eq!(&text[..split], "hello   ");
    assert_eq!(line_text(&lines[0]), "hello   ");
}

// Covers: a trailing wrap-boundary space is drained for streaming but not shown
// as its own indented continuation line.
// Owner: pure unit (wrap layout math)
#[test]
fn complete_visual_prefix_and_rendering_agree_on_exact_width_trailing_space() {
    let text = "abc ";
    let split = complete_visual_prefix_byte_index(text, 3);
    let mut lines = Vec::new();
    push_wrapped_text(&mut lines, text, 3, Style::default(), LineFill::Natural);

    assert_eq!(&text[..split], "abc");
    assert_eq!(
        lines.iter().map(line_text).collect::<Vec<_>>(),
        vec!["abc".to_string()]
    );
}

#[test]
fn wrapped_text_handles_wide_chars_in_narrow_width() {
    let mut lines = Vec::new();
    push_wrapped_text(&mut lines, "ä½ a", 1, Style::default(), LineFill::Natural);

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(rendered, vec!["ä½ ".to_string(), "a".to_string()]);
}

#[test]
fn wrapped_text_keeps_list_syntax_out_of_generic_wrapping() {
    assert_eq!(
        wrap_line_at_whitespace(
            "- fixtures/downstream/no-default-features/Cargo.toml: package 0.0.0",
            39,
        ),
        vec![
            "- ",
            "fixtures/downstream/no-default-features",
            "/Cargo.toml: package 0.0.0",
        ]
    );
}

#[test]
fn long_words_still_hard_wrap() {
    let mut lines = Vec::new();
    push_wrapped_text(
        &mut lines,
        "abcdefghijk",
        5,
        Style::default(),
        LineFill::Natural,
    );

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(
        rendered,
        vec!["abcde".to_string(), "fghij".to_string(), "k".to_string()]
    );
}

#[test]
fn stream_fragment_rendering_preserves_blank_lines() {
    let mut lines = Vec::new();
    push_wrapped_text(&mut lines, "a\n\n", 10, Style::default(), LineFill::Natural);

    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(rendered, vec!["a".to_string(), String::new()]);
}

// Covers: up/down arrow must land on the character under the target column,
// including on rows past the first, where hard newlines are skipped but soft
// wraps are not.
// Owner: pure unit (composer layout math)
#[test]
fn visual_cursor_index_maps_row_and_column_to_char_index() {
    let cases = [
        // (input, width, row, column, expected char index)
        ("ab\ncdef", 80, 0, 4, 2),
        ("界a界b", 3, 0, 2, 1),
        ("abcdef", 4, 0, 2, 2),
        // Row 1 after a hard newline: the '\n' itself is not addressable.
        ("ab\ncdef", 80, 1, 2, 5),
        ("ab\ncdef", 80, 1, 9, 7),
        // Row 1 after a soft wrap: every character stays addressable.
        ("abcdef", 4, 1, 1, 5),
        ("界a界b", 3, 1, 2, 3),
        // Two hard newlines put row 2 past both of them.
        ("a\nb\ncd", 80, 2, 1, 5),
        // Word wrap: break after the space, second row starts at "world".
        ("hello world", 8, 1, 0, 6),
        ("hello world", 8, 1, 2, 8),
        // Exact-width first word: preserve mode keeps the break space on row 1.
        ("hello world", 5, 1, 0, 5),
        ("hello world", 5, 1, 1, 6),
    ];

    for (input, width, row, column, expected) in cases {
        let lines = input_visual_lines(input, width);
        assert_eq!(
            input_cursor_index_on_visual_line(input, &lines, row, column),
            expected,
            "input {input:?} width {width} row {row} column {column} lines {lines:?}"
        );
    }
}

// Covers: click/hit-test mapping uses the editable visual lines (including the
// trailing empty row after a full-width wrap) so caret placement matches paint.
// Owner: pure unit (composer layout math)
#[test]
fn input_char_index_at_position_matches_editable_layout() {
    assert_eq!(input_char_index_at_position("hello", 80, 0, 2), 2);
    assert_eq!(input_char_index_at_position("hello", 80, 0, 99), 5);
    // Soft-wrapped second row.
    assert_eq!(input_char_index_at_position("abcdefghij", 5, 1, 2), 7);
    // Full-width first visual line leaves an empty editable row for the caret.
    assert_eq!(input_char_index_at_position("abcde", 5, 1, 0), 5);
}

// Covers: caret rows/columns derive from the same visual lines that paint, so
// a wrapped composer never places the caret on a row that does not hold its
// character.
// Owner: pure unit (composer layout math)
#[test]
fn visual_caret_position_tracks_paint_rows() {
    let caret = |input: &str, cursor: usize, width: usize| {
        let lines = editable_input_visual_lines(input, width);
        visual_caret_position(&lines, input, cursor)
    };
    // Mid-token and end of input on a single row.
    assert_eq!(caret("hello", 2, 80), Position { x: 2, y: 0 });
    assert_eq!(caret("hello", 5, 80), Position { x: 5, y: 0 });
    // A cursor before a hard newline stays on its own row; after it, the next.
    assert_eq!(caret("ab\ncd", 2, 80), Position { x: 2, y: 0 });
    assert_eq!(caret("ab\ncd", 3, 80), Position { x: 0, y: 1 });
    // Soft wrap: the caret falls to the painted row of its character.
    assert_eq!(caret("hello world", 7, 8), Position { x: 1, y: 1 });
    // A full-width first row leaves an empty editable row for the caret.
    assert_eq!(caret("abcde", 5, 5), Position { x: 0, y: 1 });
    // Multibyte characters count once and place by display width.
    assert_eq!(caret("héllo", 3, 80), Position { x: 3, y: 0 });
}

// Covers: composer soft-wraps on word boundaries instead of mid-word hard cuts.
// Owner: pure unit (composer layout math)
#[test]
fn composer_input_word_wraps_at_whitespace() {
    assert_eq!(
        input_visual_lines("hello wide world", 10),
        vec!["hello wide".to_string(), " world".to_string()]
    );
    // Long tokens still hard-wrap when no break fits.
    assert_eq!(
        input_visual_lines("abcdefghijk", 5),
        vec!["abcde".to_string(), "fghij".to_string(), "k".to_string()]
    );
    // Hard newlines still split first; soft wrap applies per logical line.
    assert_eq!(
        input_visual_lines("hello world\nnext line here", 10),
        vec![
            "hello ".to_string(),
            "world".to_string(),
            "next line ".to_string(),
            "here".to_string(),
        ]
    );
}

// Covers: highlight lockstep stays aligned across preserved soft-wrap spaces.
// Owner: pure unit (composer layout math)
#[test]
fn composer_input_lines_highlight_survives_word_wrap() {
    let frame = input_frame(
        "hello world",
        /*cursor*/ 11,
        /*width*/ 8,
        Some(6..11),
    );
    let lines = frame.lines;
    let rendered = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(rendered, vec!["hello ".to_string(), "world".to_string()]);
    assert_eq!(lines[1].spans.len(), 1);
    assert_eq!(lines[1].spans[0].content.as_ref(), "world");
    assert!(lines[1].spans[0]
        .style
        .add_modifier
        .contains(ratatui::style::Modifier::REVERSED));
}

// Covers: a picker must list more entries on a tall terminal instead of staying
// at the count that fits the default-height fallback.
// Owner: pure unit (picker line generation); a PTY scenario would re-assert this
// same line math through a slower path.
#[test]
fn picker_lists_more_items_on_a_taller_viewport() {
    let items = (0..40)
        .map(|index| PickerItem {
            section: None,
            label: format!("model-{index}"),
            detail: None,
            preview: None,
            badge: None,
            value: format!("model-{index}"),
            selection_verb: None,
        })
        .collect();
    let picker = UiPicker::new("models", items, PickerAction::SelectModel);

    let item_rows = |height: usize| {
        picker_lines(&picker, 80, height)
            .iter()
            // Item rows carry a selection marker before the label.
            .filter(|line| line_text(line).contains("model-"))
            .count()
    };

    assert_eq!(item_rows(18), 8);
    assert_eq!(item_rows(40), 30);
    // A viewport with no room left still shows the selected item.
    assert_eq!(item_rows(4), 1);
}

// Covers: wrapped key-hint rows must reduce the item viewport so the extra
// footer line is reserved instead of clipping the last bind.
// Owner: pure unit (picker line generation)
#[test]
fn picker_reserves_wrapped_footer_rows() {
    use crate::tui::PickerKeyHints;

    let items = (0..20)
        .map(|index| PickerItem {
            section: None,
            label: format!("model-{index}"),
            detail: None,
            preview: None,
            badge: None,
            value: format!("model-{index}"),
            selection_verb: None,
        })
        .collect();
    let picker = UiPicker::new("select model", items, PickerAction::SelectModel).with_key_hints(
        PickerKeyHints {
            pin_toggle: Some("Ctrl+P".into()),
            scope_toggle: Some("Ctrl+O".into()),
            tab_complete: true,
            ..Default::default()
        },
    );

    let lines = picker_lines(&picker, 80, 18);
    let item_rows = lines
        .iter()
        .filter(|line| line_text(line).contains("model-"))
        .count();
    let footer: Vec<String> = lines
        .iter()
        .map(line_text)
        .filter(|text| {
            text.contains("Type to search")
                || text.contains("Enter select")
                || text.contains("Ctrl+P")
                || text.contains("Tab complete")
                || text.contains("Esc cancel")
        })
        .collect();
    assert_eq!(item_rows, 7);
    assert_eq!(
        footer,
        vec![
            "  select model · Type to search · Enter select · Ctrl+P pin/unpin".to_string(),
            "  Ctrl+O all/pinned · Tab complete · Esc cancel".to_string(),
        ]
    );
    assert!(footer.iter().all(|line| !line.contains('…')));
}

// Covers: side gutters must not extend link underlines past the URL text.
// Owner: pure TUI layout policy.
#[test]
fn pad_display_line_strips_underline_from_edge_spaces() {
    let link = Theme::markdown_link();
    let padded = pad_display_line(Line::from(Span::styled("https://example.com", link)));

    assert_eq!(
        padded
            .spans
            .iter()
            .map(|span| span.content.as_ref())
            .collect::<Vec<_>>(),
        [" ", "https://example.com", " "]
    );
    assert_eq!(
        padded.spans[0].style,
        chrome_edge_style(link),
        "leading gutter keeps colors without underline"
    );
    assert_eq!(padded.spans[1].style, link, "link body keeps underline");
    assert_eq!(
        padded.spans[2].style,
        chrome_edge_style(link),
        "trailing gutter keeps colors without underline"
    );
    assert!(!padded.spans[0]
        .style
        .add_modifier
        .contains(Modifier::UNDERLINED));
    assert!(!padded.spans[2]
        .style
        .add_modifier
        .contains(Modifier::UNDERLINED));
}

// Covers: user-message side gutters still carry the message background band.
// Owner: pure TUI layout policy.
#[test]
fn pad_display_line_keeps_user_message_background() {
    let style = Theme::user_message();
    let padded = pad_display_line(Line::from(Span::styled("hi", style)));
    assert_eq!(padded.spans[0].style.bg, style.bg);
    assert_eq!(padded.spans[2].style.bg, style.bg);
}

// Covers: Entry::Error must stay distinguishable from Notice without color.
// Owner: pure TUI render policy.
#[test]
fn error_entries_include_text_severity_marker() {
    struct RestoreTheme(String);
    impl Drop for RestoreTheme {
        fn drop(&mut self) {
            Theme::apply_committed(&self.0);
        }
    }

    let _theme_lock = crate::tui::theme::theme_test_lock();
    let _restore_theme = RestoreTheme(Theme::committed_id());
    Theme::apply_committed("monochrome-dark");

    let message = "could not save theme";
    let error_lines = entry_lines(
        &crate::tui::Entry::Error(message.into()),
        40,
        /*max_tool_output_lines*/ 0,
        /*max_image_height*/ 0,
    );
    let notice_lines = entry_lines(
        &crate::tui::Entry::Notice(message.into()),
        40,
        /*max_tool_output_lines*/ 0,
        /*max_image_height*/ 0,
    );

    let error_text = line_text(&error_lines[0]);
    let notice_text = line_text(&notice_lines[0]);
    assert_eq!(
        error_text.trim(),
        format!("error: {message}"),
        "errors keep a stable text marker under monochrome"
    );
    assert_eq!(notice_text.trim(), message, "notices stay unmarked");
    assert_ne!(
        error_text.trim(),
        notice_text.trim(),
        "same body text must not collide once color is gone"
    );
}

// Covers: duration receipts stay off the last markdown body row.
// Owner: entry render layout.
#[test]
fn duration_receipt_leaves_a_blank_row_after_body() {
    let _theme_lock = crate::tui::theme::theme_test_lock();
    let elapsed = std::time::Duration::from_millis(2_300);
    let body_only = crate::tui::Entry::Assistant("Hello".into());
    let with_receipt = crate::tui::Entry::Assistant(crate::tui::AssistantEntry {
        text: "Hello".into(),
        worked_for: Some(elapsed),
    });
    let summary_only =
        crate::tui::Entry::Assistant(crate::tui::AssistantEntry::summary_only(elapsed));

    let without = entry_lines(
        &body_only, 40, /*max_tool_output_lines*/ 0, /*max_image_height*/ 0,
    );
    let with = entry_lines(
        &with_receipt,
        40,
        /*max_tool_output_lines*/ 0,
        /*max_image_height*/ 0,
    );
    let summary = entry_lines(
        &summary_only,
        40,
        /*max_tool_output_lines*/ 0,
        /*max_image_height*/ 0,
    );
    let without_text: Vec<String> = without.iter().map(line_text).collect();
    let with_text: Vec<String> = with.iter().map(line_text).collect();
    let summary_text: Vec<String> = summary.iter().map(line_text).collect();
    let body_end = without_text.len() - 1;

    assert_eq!(
        &with_text[..body_end],
        &without_text[..body_end],
        "receipt should leave the body prefix unchanged: {with_text:?}"
    );
    assert!(
        with_text[body_end].trim().is_empty(),
        "body and receipt need a blank between them: {with_text:?}"
    );
    assert!(
        !with_text[body_end + 1].trim().is_empty(),
        "receipt should sit above the trailing spacer: {with_text:?}"
    );
    assert_eq!(
        with_text.last(),
        without_text.last(),
        "receipt should keep the trailing spacer: {with_text:?}"
    );
    assert!(
        !summary_text
            .first()
            .is_some_and(|line| line.trim().is_empty()),
        "summary-only entries should not lead with a blank: {summary_text:?}"
    );
}