rho-coding-agent 0.28.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
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
use super::*;

#[test]
fn fullscreen_history_starts_at_bottom() {
    let mut app = test_app();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    let lines = app.active_lines_for_height(40, 12);
    let rendered = lines.iter().map(line_text).collect::<Vec<_>>().join("\n");

    assert!(rendered.contains("message 19"), "{rendered}");
    assert!(!rendered.contains("message 0"), "{rendered}");
}

#[test]
fn pageup_enters_manual_scroll_and_ctrl_g_returns_to_bottom() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    assert!(app
        .handle_history_key(
            KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE),
            &mut terminal,
        )
        .unwrap());
    assert!(matches!(app.history_scroll, HistoryScroll::Manual { .. }));
    assert!(app.should_show_jump_to_bottom(40, 12, Instant::now()));

    assert!(app
        .handle_history_key(
            KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL),
            &mut terminal,
        )
        .unwrap());
    assert_eq!(app.history_scroll, HistoryScroll::Bottom);
    assert!(!app.should_show_jump_to_bottom(40, 12, Instant::now()));
}

#[test]
fn small_scroll_to_rendered_bottom_resumes_bottom_following() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let history_len = app.history_len(40, Instant::now());
    let rendered_bottom_start =
        history_len.saturating_sub(app.history_height_for_screen(40, 12, Instant::now()));
    app.history_scroll = HistoryScroll::Manual {
        top_line: rendered_bottom_start.saturating_sub(1),
    };

    app.handle_mouse_event(MouseEventKind::ScrollDown, 0, 0, &mut terminal)
        .unwrap();

    assert_eq!(app.history_scroll, HistoryScroll::Bottom);
    assert!(!app.should_show_jump_to_bottom(40, 12, Instant::now()));
}

#[test]
fn pagedown_moves_manual_scroll_toward_bottom() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    app.handle_history_key(
        KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE),
        &mut terminal,
    )
    .unwrap();
    let before = match app.history_scroll {
        HistoryScroll::Manual { top_line } => top_line,
        HistoryScroll::Bottom => panic!("expected manual scroll"),
    };

    app.handle_history_key(
        KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE),
        &mut terminal,
    )
    .unwrap();

    match app.history_scroll {
        HistoryScroll::Manual { top_line } => assert!(top_line > before),
        HistoryScroll::Bottom => {}
    }
}

#[test]
fn jump_button_renders_above_composer_only_when_scrolled_up() {
    let mut app = test_app();
    app.input = "draft".into();
    app.input_cursor = app.input_char_len();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    let bottom_lines = app
        .active_lines_for_height(40, 12)
        .iter()
        .map(line_text)
        .collect::<Vec<_>>();
    assert!(!bottom_lines
        .iter()
        .any(|line| line.contains("jump to bottom")));

    app.scroll_history_page_up(40, 12, Instant::now());
    let scrolled_lines = app
        .active_lines_for_height(40, 12)
        .iter()
        .map(line_text)
        .collect::<Vec<_>>();
    let button_index = scrolled_lines
        .iter()
        .position(|line| line.contains("jump to bottom"))
        .unwrap();
    let input_index = scrolled_lines
        .iter()
        .position(|line| line.trim_end() == "draft")
        .unwrap();

    assert!(button_index < input_index, "{scrolled_lines:#?}");
}

#[test]
fn spinner_overlays_last_history_row_without_reducing_layout_height() {
    let mut app = test_app();
    let area = Rect::new(0, 0, 40, 12);
    let idle = app.screen_layout(area, Instant::now());

    app.running = true;
    let loading = app.screen_layout(area, Instant::now());

    assert_eq!(idle.history, loading.history);
    assert_eq!(idle.activity, None);
    let activity = loading.activity.unwrap();
    assert_eq!(activity.y, loading.history.bottom().saturating_sub(1));
    assert!(activity.width < loading.history.width);
}

#[test]
fn spinner_offsets_bottom_following_but_not_manual_scroll() {
    let mut app = test_app();
    app.running = true;

    assert_eq!(app.visible_history_window(20, 10), (11, 9));

    app.history_scroll = HistoryScroll::Manual { top_line: 3 };
    assert_eq!(app.visible_history_window(20, 10), (3, 10));
}

#[test]
fn spinner_and_jump_button_share_activity_row_with_jump_right_aligned() {
    let mut app = test_app();
    app.running = true;
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    app.scroll_history_page_up(40, 12, Instant::now());

    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let activity = layout.activity.unwrap();
    let button = layout.jump_to_bottom.unwrap();
    assert_eq!(activity.y, button.y);
    assert_eq!(activity.x, layout.history.x);
    assert!(activity.right() < button.x);
    assert_eq!(button.right(), layout.history.right());
}

#[test]
fn bottom_following_renders_last_message_above_spinner_overlay() {
    let mut app = test_app();
    app.running = true;
    for index in 0..20 {
        app.push_transcript_entry(Entry::Assistant(format!("message {index}")));
    }
    let width = 40;
    let height = 12;
    let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();

    terminal.draw(|frame| app.draw(frame)).unwrap();

    let layout = app.screen_layout(Rect::new(0, 0, width, height), Instant::now());
    let activity = layout.activity.unwrap();
    let rows = (0..height)
        .map(|row| buffer_row_text(terminal.backend().buffer(), row))
        .collect::<Vec<_>>();
    assert!(
        rows[..activity.y as usize]
            .iter()
            .any(|row| row.contains("message 19")),
        "{rows:#?}"
    );
    assert!(
        !rows[activity.y as usize].contains("message 19"),
        "{rows:#?}"
    );
}

#[test]
fn jump_button_preserves_uncovered_content_on_last_scrolled_row() {
    let mut app = test_app();
    let width = 40;
    let height = 12;
    let now = Instant::now();
    for index in 0..30 {
        app.push_transcript_entry(Entry::Assistant(format!(
            "message {index:02} remains visible across this row"
        )));
    }
    let history_len = app.history_len(width as usize, now);
    let layout = app.screen_layout(Rect::new(0, 0, width, height), now);
    let history_height = layout.history.height as usize;
    let top_line = (0..history_len.saturating_sub(history_height))
        .find(|top_line| {
            app.visible_history_lines(width as usize, now, *top_line, history_height)
                .last()
                .is_some_and(|line| !line_text(line).trim().is_empty())
        })
        .unwrap();
    app.history_scroll = HistoryScroll::Manual { top_line };
    let layout = app.screen_layout(Rect::new(0, 0, width, height), now);
    let button = layout.jump_to_bottom.unwrap();
    let expected = app
        .visible_history_lines(width as usize, now, top_line, history_height)
        .last()
        .map(line_text)
        .unwrap();
    let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();

    terminal.draw(|frame| app.draw(frame)).unwrap();

    let rendered = buffer_row_text(terminal.backend().buffer(), button.y);
    let preserved_width = button.x as usize;
    assert!(
        !expected[..preserved_width].trim().is_empty(),
        "{expected:?}"
    );
    assert_eq!(&rendered[..preserved_width], &expected[..preserved_width]);
    assert!(
        rendered.ends_with("↓ jump to bottom  ctrl+g"),
        "{rendered:?}"
    );
}

#[test]
fn compact_jump_button_renders_on_narrow_terminals() {
    let app = test_app();

    assert!(line_text(&app.jump_to_bottom_line(16)).contains("bottom ctrl+g"));
    assert!(line_text(&app.jump_to_bottom_line(40)).contains("ctrl+g"));
}

#[test]
fn mouse_wheel_scrolls_history_and_clicking_jump_button_returns_to_bottom() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    app.handle_mouse_event(MouseEventKind::ScrollUp, 0, 0, &mut terminal)
        .unwrap();
    assert!(matches!(app.history_scroll, HistoryScroll::Manual { .. }));

    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let button = layout.jump_to_bottom.unwrap();
    assert!(button.x > 0);
    app.handle_mouse_event(
        MouseEventKind::Down(MouseButton::Left),
        button.x,
        button.y,
        &mut terminal,
    )
    .unwrap();

    assert_eq!(app.history_scroll, HistoryScroll::Bottom);
}

#[test]
fn scrollbar_hides_until_scroll_or_hover() {
    let mut app = test_app();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let height = 12;
    let mut terminal = Terminal::new(TestBackend::new(40, height)).unwrap();

    terminal.draw(|frame| app.draw(frame)).unwrap();
    let rows = (0..height)
        .map(|row| buffer_row_text(terminal.backend().buffer(), row))
        .collect::<Vec<_>>();

    assert!(!rows.iter().any(|row| row.ends_with('â–ˆ')), "{rows:#?}");
}

#[test]
fn scrollbar_renders_briefly_after_mouse_wheel_scroll() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }

    app.handle_mouse_event(MouseEventKind::ScrollUp, 0, 0, &mut terminal)
        .unwrap();

    assert!(app.should_render_history_scrollbar(Instant::now()));
}

#[test]
fn scrollbar_renders_while_hovered() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let scrollbar = layout.history_scrollbar.unwrap();

    app.handle_mouse_event(
        MouseEventKind::Moved,
        scrollbar.rect.x,
        scrollbar.rect.y,
        &mut terminal,
    )
    .unwrap();

    assert!(app.should_render_history_scrollbar(Instant::now()));
}

#[test]
fn dragging_scrollbar_updates_history_scroll() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..30 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    app.scroll_history_page_up(40, 12, Instant::now());
    app.scroll_history_page_up(40, 12, Instant::now());
    app.scroll_history_page_up(40, 12, Instant::now());
    app.reveal_history_scrollbar(Instant::now());
    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let scrollbar = layout.history_scrollbar.unwrap();
    let thumb_row = (scrollbar.rect.y..scrollbar.rect.y.saturating_add(scrollbar.rect.height))
        .find(|row| {
            matches!(
                scrollbar.begin_drag(*row),
                HistoryScrollbarDrag::Thumb { .. }
            )
        })
        .unwrap();

    app.handle_mouse_event(
        MouseEventKind::Down(MouseButton::Left),
        scrollbar.rect.x,
        thumb_row,
        &mut terminal,
    )
    .unwrap();
    assert!(matches!(
        app.history_scrollbar_drag,
        Some(HistoryScrollbarDrag::Thumb { .. })
    ));
    let before = match app.history_scroll {
        HistoryScroll::Manual { top_line } => top_line,
        HistoryScroll::Bottom => panic!("expected manual scroll"),
    };

    let drag_row = scrollbar.rect.y;

    app.handle_mouse_event(
        MouseEventKind::Drag(MouseButton::Left),
        scrollbar.rect.x,
        drag_row,
        &mut terminal,
    )
    .unwrap();
    let after = match app.history_scroll {
        HistoryScroll::Manual { top_line } => top_line,
        HistoryScroll::Bottom => usize::MAX,
    };
    assert!(after < before, "before={before} after={after}");

    app.handle_mouse_event(
        MouseEventKind::Up(MouseButton::Left),
        scrollbar.rect.x,
        drag_row,
        &mut terminal,
    )
    .unwrap();
    assert_eq!(app.history_scrollbar_drag, None);
}

#[test]
fn clicking_scrollbar_track_jumps_history_scroll() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..30 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let scrollbar = layout.history_scrollbar.unwrap();
    app.reveal_history_scrollbar(Instant::now());

    app.handle_mouse_event(
        MouseEventKind::Down(MouseButton::Left),
        scrollbar.rect.x,
        scrollbar.rect.y.saturating_add(scrollbar.rect.height - 1),
        &mut terminal,
    )
    .unwrap();

    assert_eq!(app.history_scroll, HistoryScroll::Bottom);
}

#[test]
fn clicking_hidden_scrollbar_does_not_scroll_history() {
    let mut app = test_app();
    let mut terminal = Terminal::new(TestBackend::new(40, 12)).unwrap();
    for index in 0..30 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    app.scroll_history_page_up(40, 12, Instant::now());
    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let scrollbar = layout.history_scrollbar.unwrap();
    let before = app.history_scroll;

    app.handle_mouse_event(
        MouseEventKind::Down(MouseButton::Left),
        scrollbar.rect.x,
        scrollbar
            .rect
            .y
            .saturating_add(scrollbar.rect.height.saturating_sub(2)),
        &mut terminal,
    )
    .unwrap();

    assert_eq!(app.history_scroll, before);
    assert_eq!(app.history_scrollbar_drag, None);
}

#[test]
fn clamping_bottom_scroll_preserves_scrollbar_hover() {
    let mut app = test_app();
    for index in 0..30 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let layout = app.screen_layout(Rect::new(0, 0, 40, 12), Instant::now());
    let scrollbar = layout.history_scrollbar.unwrap();
    app.update_history_scrollbar_hover(
        layout.history_scrollbar,
        scrollbar.rect.x,
        scrollbar.rect.y,
    );

    app.clamp_history_scroll(40, 12, Instant::now());

    assert!(app.history_scrollbar_hovered);
    assert!(app.should_render_history_scrollbar(Instant::now()));
}

#[test]
fn manual_scroll_preserves_top_line_when_new_output_arrives() {
    let mut app = test_app();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    app.scroll_history_page_up(40, 12, Instant::now());
    let before_len = app.history_len(40, Instant::now());
    let before_start = app.visible_history_start(before_len, 6);

    app.push_transcript_entry(Entry::Assistant("new output".into()));
    let after_len = app.history_len(40, Instant::now());
    let after_start = app.visible_history_start(after_len, 6);

    assert_eq!(after_start, before_start);
}

#[test]
fn bottom_scroll_follows_new_output() {
    let mut app = test_app();
    for index in 0..20 {
        app.push_transcript_entry(Entry::User(format!("message {index}")));
    }
    let before_len = app.history_len(40, Instant::now());
    let before_start = app.visible_history_start(before_len, 6);

    app.push_transcript_entry(Entry::Assistant("new output".into()));
    let after_len = app.history_len(40, Instant::now());
    let after_start = app.visible_history_start(after_len, 6);

    assert!(after_start > before_start);
}

#[test]
fn repeated_statusline_frames_render_once() {
    let mut app = test_app();

    for _ in 0..10_000 {
        app.statusline_lines(120);
    }

    assert_eq!(app.statusline.render_count(), 1);
}

#[test]
fn hidden_reasoning_shows_thinking_placeholder() {
    let mut app = test_app();
    app.active_turn_show_reasoning_output = false;
    app.record_agent_event(AgentEvent::StepStarted(1));

    let thinking = app
        .history_live_lines(60, Instant::now())
        .into_iter()
        .find(|line| line_text(line).contains("Thinking..."))
        .unwrap();
    assert_eq!(thinking.spans[1].style, StreamKind::Reasoning.style());

    app.reset_streams();
    assert!(!app.hidden_reasoning_active);
}

#[test]
fn started_tool_display_ignores_late_argument_previews() {
    let mut app = test_app();

    app.record_agent_event(AgentEvent::ToolStarted {
        name: "edit_file".into(),
        command: None,
        display_style: ToolDisplayStyle::file_diff(),
        display_lines: vec!["edit_file src/main.rs".into()],
    });
    app.record_agent_event(AgentEvent::ToolCallUpdated {
        display_lines: vec!["edit_file".into()],
    });

    assert_eq!(
        app.pending_tool_call
            .as_ref()
            .map(|tool| tool.display_lines.as_slice()),
        Some(["edit_file src/main.rs".to_string()].as_slice())
    );
}