rho-coding-agent 2.15.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
use std::{
    io,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
};

use crossterm::event::{MouseButton, MouseEventKind};
use pretty_assertions::assert_eq;
use ratatui::{backend::TestBackend, Terminal};

use crate::tui::media_attach;

use super::{
    super::{tests::test_app, ComposerAttachment},
    ChatMedia, ChatTextDocument, Clipboard, CopyOutcome, ImageContent, MediaAttachId,
    PendingAttachmentSource,
};

struct FakeClipboard {
    text: String,
    paste_error: Option<String>,
    paste_calls: Arc<AtomicUsize>,
}

impl Clipboard for FakeClipboard {
    fn copy(&mut self, text: &str) -> io::Result<CopyOutcome> {
        self.text = text.to_string();
        Ok(CopyOutcome::Confirmed)
    }

    fn paste(&mut self) -> io::Result<String> {
        self.paste_calls.fetch_add(1, Ordering::Relaxed);
        match self.paste_error.as_ref() {
            Some(message) => Err(io::Error::other(message.clone())),
            None => Ok(self.text.clone()),
        }
    }
}

async fn insert_external_paste_and_finish(app: &mut super::App, text: &str) {
    app.insert_external_paste(text);
    if !app.media_attach_tasks.is_empty() {
        let outcome = media_attach::next_media_attach_completion(&mut app.media_attach_tasks).await;
        app.finish_media_attach(outcome);
    }
}

// Covers: main and side composers read host clipboard text once on right-click,
// never on release. The injected clipboard avoids a native desktop dependency.
// Owner: tui clipboard routing
#[test]
fn right_click_pastes_clipboard_text_into_composer() {
    for composer in [super::ComposerMode::Input, super::ComposerMode::Side] {
        let mut app = test_app();
        if matches!(composer, super::ComposerMode::Side) {
            app.open_side_chat();
        }
        let paste_calls = Arc::new(AtomicUsize::new(0));
        app.clipboard = Box::new(FakeClipboard {
            text: "hello from clip".into(),
            paste_error: None,
            paste_calls: paste_calls.clone(),
        });
        let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap();

        for kind in [
            MouseEventKind::Down(MouseButton::Right),
            MouseEventKind::Up(MouseButton::Right),
        ] {
            app.handle_mouse_event(kind, 10, 10, &mut terminal).unwrap();
            assert_eq!(paste_calls.load(Ordering::Relaxed), 1, "{composer:?}");
            let expected_parent = if matches!(composer, super::ComposerMode::Side) {
                assert!(!app.side_composer_is_empty());
                ""
            } else {
                "hello from clip"
            };
            assert_eq!(app.input_ui.text(), expected_parent, "{composer:?}");
        }
    }
}

// Covers: empty clipboard is a no-op; a paste backend failure surfaces as a toast.
// Owner: tui clipboard
#[test]
fn clipboard_text_paste_empty_and_error() {
    let mut app = test_app();
    app.clipboard = Box::new(FakeClipboard {
        text: String::new(),
        paste_error: None,
        paste_calls: Arc::default(),
    });
    app.paste_clipboard_text();
    assert_eq!(app.input_ui.text(), "");
    assert_eq!(app.status(), "");

    app.clipboard = Box::new(FakeClipboard {
        text: String::new(),
        paste_error: Some("no display".into()),
        paste_calls: Arc::default(),
    });
    app.paste_clipboard_text();
    assert_eq!(app.input_ui.text(), "");
    assert!(
        !app.status().is_empty(),
        "paste failure must surface a toast"
    );
}

#[test]
fn image_paste_is_unavailable_while_running() {
    let mut app = test_app();
    app.begin_provider_turn_ui();

    app.paste_clipboard_image();

    assert!(app.input_ui.attachments().is_empty());
}

#[tokio::test]
async fn attachment_task_poll_is_cancellation_safe() {
    let (release_tx, release_rx) = tokio::sync::oneshot::channel();
    let task = tokio::spawn(async {
        let _ = release_rx.await;
        media_attach::MediaAttachOutcome::Unsupported {
            original_text: "archive.bin".into(),
        }
    });
    let id = MediaAttachId::new();
    let mut pending = vec![media_attach::MediaAttachTask { id, task }];

    let mut first_poll = Box::pin(media_attach::next_media_attach_completion(&mut pending));
    assert!(futures_util::poll!(&mut first_poll).is_pending());
    drop(first_poll);
    assert_eq!(pending.len(), 1);

    let _ = release_tx.send(());
    let completion = media_attach::next_media_attach_completion(&mut pending).await;
    assert_eq!(completion.id, id);
    assert!(matches!(
        completion.outcome,
        media_attach::MediaAttachOutcome::Unsupported { original_text }
            if original_text == "archive.bin"
    ));
    assert!(pending.is_empty());
}

#[tokio::test]
async fn single_line_image_path_paste_attaches_image_instead_of_text() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("clip.png");
    let png = base64::Engine::decode(
        &base64::engine::general_purpose::STANDARD,
        "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
    )
    .unwrap();
    std::fs::write(&path, png).unwrap();

    let mut app = test_app();
    app.info.runtime.cwd = dir.path().to_path_buf();
    insert_external_paste_and_finish(&mut app, &path.to_string_lossy()).await;

    assert_eq!(app.input_ui.attachments().len(), 1);
    assert!(matches!(
        &app.input_ui.attachments()[0],
        ComposerAttachment::Ready(ChatMedia::Image(image)) if image.mime_type == "image/png"
    ));
    assert!(app.input_ui.text().is_empty());
}

#[tokio::test]
async fn text_document_path_paste_attaches_document_instead_of_text() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("notes.txt");
    std::fs::write(&path, "hello").unwrap();

    let mut app = test_app();
    app.info.runtime.cwd = dir.path().to_path_buf();
    app.insert_external_paste(&path.to_string_lossy());

    assert_eq!(
        app.input_ui.attachments(),
        &[ComposerAttachment::Pending {
            id: app.media_attach_tasks[0].id,
            source: PendingAttachmentSource::File,
            name: "notes.txt".into(),
        }]
    );

    let outcome = media_attach::next_media_attach_completion(&mut app.media_attach_tasks).await;
    app.finish_media_attach(outcome);

    assert_eq!(
        app.input_ui.attachments(),
        &[ComposerAttachment::Ready(ChatMedia::TextDocument(
            ChatTextDocument {
                name: "notes.txt".into(),
                mime: "text/plain".into(),
                body: "hello".into(),
                truncated: false,
                warnings: Vec::new(),
            }
        ))]
    );
    assert!(app.input_ui.text().is_empty());
}

#[tokio::test]
async fn unsupported_binary_path_paste_stays_text() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("archive.bin");
    std::fs::write(&path, [0, 1, 2, 3]).unwrap();

    let mut app = test_app();
    app.info.runtime.cwd = dir.path().to_path_buf();
    insert_external_paste_and_finish(&mut app, &path.to_string_lossy()).await;

    assert!(app.input_ui.attachments().is_empty());
    assert!(!app.input_ui.text().is_empty() || !app.input_ui.paste_segments().is_empty());
}

// Covers: an unsupported attachment restored as text confirms the collapse when
// it is large enough to hide behind a marker (same rule as external pastes).
// Owner: tui status surface
#[tokio::test]
async fn unsupported_attach_restore_of_large_text_confirms_collapse() {
    let min_lines = crate::tui::paste_burst::PASTE_COLLAPSE_MIN_LINES;
    let original = (0..min_lines)
        .map(|i| format!("line{i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let task = tokio::spawn(async move {
        media_attach::MediaAttachOutcome::Unsupported {
            original_text: original,
        }
    });
    let id = MediaAttachId::new();
    let mut app = test_app();
    app.media_attach_tasks
        .push(media_attach::MediaAttachTask { id, task });
    app.input_ui
        .push_pending_attachment(id, PendingAttachmentSource::File, "archive.bin".into());

    let outcome = media_attach::next_media_attach_completion(&mut app.media_attach_tasks).await;
    app.finish_media_attach(outcome);

    assert!(app.input_ui.attachments().is_empty());
    assert_eq!(
        app.input_ui.text(),
        format!("[ pasted: {min_lines} lines ]")
    );
    assert_eq!(app.status(), format!("pasted {min_lines} lines"));
}

#[cfg(unix)]
#[tokio::test]
async fn unreadable_image_path_paste_reports_error_without_inserting_text() {
    use std::os::unix::fs::PermissionsExt;

    // SAFETY: `geteuid` takes no pointers and has no preconditions.
    if unsafe { libc::geteuid() } == 0 {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("secret.png");
    let png = base64::Engine::decode(
        &base64::engine::general_purpose::STANDARD,
        "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
    )
    .unwrap();
    std::fs::write(&path, png).unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
    // Root or CAP_DAC_OVERRIDE can still open 0o000 files; skip if this process can.
    if std::fs::File::open(&path).is_ok() {
        let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
        return;
    }

    let mut app = test_app();
    app.info.runtime.cwd = dir.path().to_path_buf();
    insert_external_paste_and_finish(&mut app, &path.to_string_lossy()).await;

    let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));

    assert!(app.input_ui.attachments().is_empty());
    assert!(app.input_ui.text().is_empty());
}

// Covers: Backspace removes the last visible attachment instead of an earlier extraction task.
// Owner: TUI composer attachment orchestration.
#[tokio::test]
async fn backspace_removes_ready_image_after_pending_document() {
    let (mut release_tx, release_rx) = tokio::sync::oneshot::channel::<()>();
    let task = tokio::spawn(async {
        let _ = release_rx.await;
        media_attach::MediaAttachOutcome::Unsupported {
            original_text: "notes.txt".into(),
        }
    });
    let id = MediaAttachId::new();
    let mut app = test_app();
    app.media_attach_tasks
        .push(media_attach::MediaAttachTask { id, task });
    app.input_ui
        .push_pending_attachment(id, PendingAttachmentSource::File, "notes.txt".into());
    app.attach_ready_image(ImageContent {
        data: "aW1hZ2U=".into(),
        mime_type: "image/png".into(),
    });

    app.backspace_input();

    assert_eq!(
        app.input_ui.attachments(),
        &[ComposerAttachment::Pending {
            id,
            source: PendingAttachmentSource::File,
            name: "notes.txt".into(),
        }]
    );
    assert_eq!(app.media_attach_tasks.len(), 1);
    assert_eq!(app.media_attach_tasks[0].id, id);

    app.backspace_input();

    assert!(app.input_ui.attachments().is_empty());
    assert!(app.media_attach_tasks.is_empty());
    tokio::time::timeout(std::time::Duration::from_secs(1), release_tx.closed())
        .await
        .expect("cancelled attachment task should drop its receiver");
}

// Covers: out-of-order completion replaces only its ID and pending items cannot become model media.
// Owner: TUI composer attachment orchestration.
#[tokio::test]
async fn completion_targets_pending_id_and_pending_attachments_cannot_submit() {
    let first_id = MediaAttachId::new();
    let second_id = MediaAttachId::new();
    let first_task = tokio::spawn(std::future::pending::<media_attach::MediaAttachOutcome>());
    let second_task = tokio::spawn(async {
        media_attach::MediaAttachOutcome::ready(ChatMedia::Image(ImageContent {
            data: "Y29tcGxldGVk".into(),
            mime_type: "image/webp".into(),
        }))
    });
    let mut app = test_app();
    app.media_attach_tasks.extend([
        media_attach::MediaAttachTask {
            id: first_id,
            task: first_task,
        },
        media_attach::MediaAttachTask {
            id: second_id,
            task: second_task,
        },
    ]);
    app.input_ui.push_pending_attachment(
        first_id,
        PendingAttachmentSource::File,
        "first.txt".into(),
    );
    app.input_ui.push_ready_attachment(
        ChatMedia::Image(ImageContent {
            data: "cmVhZHk=".into(),
            mime_type: "image/png".into(),
        }),
        None,
    );
    app.input_ui.push_pending_attachment(
        second_id,
        PendingAttachmentSource::File,
        "second.txt".into(),
    );

    let completion = media_attach::next_media_attach_completion(&mut app.media_attach_tasks).await;
    assert_eq!(completion.id, second_id);
    app.finish_media_attach(completion);

    let expected = vec![
        ComposerAttachment::Pending {
            id: first_id,
            source: PendingAttachmentSource::File,
            name: "first.txt".into(),
        },
        ComposerAttachment::Ready(ChatMedia::Image(ImageContent {
            data: "cmVhZHk=".into(),
            mime_type: "image/png".into(),
        })),
        ComposerAttachment::Ready(ChatMedia::Image(ImageContent {
            data: "Y29tcGxldGVk".into(),
            mime_type: "image/webp".into(),
        })),
    ];
    assert_eq!(app.input_ui.attachments(), expected);
    assert!(app.input_ui.take_ready_media().is_err());
    assert_eq!(app.input_ui.attachments(), expected);
    assert_eq!(app.media_attach_tasks.len(), 1);
    assert_eq!(app.media_attach_tasks[0].id, first_id);

    app.cancel_all_pending_attachments();
}

// Covers: an open panel overlay owns the pointer. Limits and Doctor used to
// take only the wheel, so a right click behind them pasted into the hidden
// composer.
// Owner: panel overlay mouse routing
#[test]
fn panel_overlays_swallow_right_click_paste() {
    for name in ["limits", "doctor"] {
        let mut app = test_app();
        match name {
            "limits" => app.start_limits_command(),
            _ => app.start_doctor_command().unwrap(),
        }
        let paste_calls = Arc::new(AtomicUsize::new(0));
        app.clipboard = Box::new(FakeClipboard {
            text: "hello from clip".into(),
            paste_error: None,
            paste_calls: paste_calls.clone(),
        });
        let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap();

        app.handle_mouse_event(
            MouseEventKind::Down(MouseButton::Right),
            10,
            10,
            &mut terminal,
        )
        .unwrap();

        assert_eq!(paste_calls.load(Ordering::Relaxed), 0, "{name}");
        assert_eq!(app.input_ui.text(), "", "{name}");
        assert!(
            matches!(app.input_ui.composer(), super::ComposerMode::Panel(_)),
            "{name}"
        );
    }
}