rho-coding-agent 1.29.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
//! External editor behavior driven through a real pseudo-terminal.

#![cfg(unix)]

use std::{path::PathBuf, time::Duration};

use rho_tui_pty::{IsolatedHome, Key, PtyHarness, PtySize, RhoLaunchPlan, WaitTimeout};

const ALTERNATE_SCREEN_ENTER: &[u8] = b"\x1b[?1049h";

#[test]
fn composer_round_trips_through_interactive_external_editor() {
    use std::os::unix::fs::PermissionsExt;

    let home = IsolatedHome::new().unwrap();
    let editor_dir = tempfile::Builder::new()
        .prefix("rho editor test ")
        .tempdir()
        .unwrap();
    let editor = editor_dir.path().join("interactive editor");
    let terminal_state = editor_dir.path().join("terminal state");
    let original_draft = editor_dir.path().join("original draft");
    std::fs::write(
        &editor,
        r#"#!/bin/sh
stty -a > "$1"
cat "$3" > "$2"
printf 'EXTERNAL_EDITOR_READY\n'
IFS= read -r replacement
printf '%s\n' "$replacement" > "$3"
"#,
    )
    .unwrap();
    std::fs::set_permissions(&editor, std::fs::Permissions::from_mode(0o700)).unwrap();

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let editor_command = format!(
        "'{}' '{}' '{}'",
        editor.display(),
        terminal_state.display(),
        original_draft.display()
    );
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("EDITOR", editor_command);
    let mut harness = PtyHarness::spawn_named(&plan, "external_editor").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();

    harness.paste("alpha\nbeta").unwrap();
    harness.settle_input();
    let expected_screen_entries = harness.raw_sequence_occurrences(ALTERNATE_SCREEN_ENTER) + 1;
    harness.inject_key(&Key::Ctrl('g')).unwrap();
    harness
        .wait_for_text(
            "EXTERNAL_EDITOR_READY",
            WaitTimeout::secs(10, "interactive editor ready"),
        )
        .unwrap();
    harness.type_text("edited in external editor").unwrap();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_raw_sequence_occurrences(
            ALTERNATE_SCREEN_ENTER,
            expected_screen_entries,
            WaitTimeout::secs(10, "TUI resumed after editor"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "edited in external editor",
            WaitTimeout::secs(10, "editor return"),
        )
        .unwrap();

    assert_eq!(
        std::fs::read_to_string(&original_draft).unwrap(),
        "alpha\nbeta"
    );
    let state = std::fs::read_to_string(&terminal_state).unwrap();
    assert!(
        state
            .split_ascii_whitespace()
            .map(|word| word.trim_matches(';'))
            .any(|word| word == "icanon"),
        "editor inherited non-canonical terminal state: {state}"
    );
    let raw = harness.raw_output();
    let leave_at = raw
        .windows(8)
        .position(|bytes| bytes == b"\x1b[?1049l")
        .expect("leave alternate screen");
    let clear_at = raw[leave_at..]
        .windows(4)
        .position(|bytes| bytes == b"\x1b[2J")
        .map(|offset| leave_at + offset)
        .expect("main screen clear after leaving alternate screen");
    let opening_at = raw
        .windows(b"Opening editor".len())
        .position(|bytes| bytes == b"Opening editor")
        .expect("opening editor status");
    let ready_at = raw
        .windows(b"EXTERNAL_EDITOR_READY".len())
        .position(|bytes| bytes == b"EXTERNAL_EDITOR_READY")
        .expect("external editor ready marker");
    assert!(
        leave_at < clear_at && clear_at < opening_at && opening_at < ready_at,
        "handoff should leave alternate screen, clear main buffer, show status, then start the editor"
    );
    assert!(
        raw.windows(8)
            .filter(|bytes| *bytes == b"\x1b[?1049h")
            .count()
            >= 2,
        "alternate screen was not re-entered after the editor"
    );

    harness.settle_input();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "fixture response: edited in external editor",
            WaitTimeout::secs(20, "edited prompt response"),
        )
        .unwrap();

    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn external_editor_errors_preserve_the_composer() {
    use std::os::unix::fs::PermissionsExt;

    let editor_dir = tempfile::tempdir().unwrap();
    let failing_editor = editor_dir.path().join("failing-editor");
    std::fs::write(&failing_editor, "#!/bin/sh\nexit 7\n").unwrap();
    std::fs::set_permissions(&failing_editor, std::fs::Permissions::from_mode(0o700)).unwrap();
    let cases = [
        ("unset", None, "EDITOR is not set"),
        ("empty", Some(String::new()), "EDITOR is not set"),
        (
            "missing",
            Some("/rho/does/not/exist".into()),
            "could not start editor",
        ),
        (
            "nonzero",
            Some(failing_editor.display().to_string()),
            "editor exited with exit status: 7",
        ),
    ];

    for (name, editor, expected_error) in cases {
        let home = IsolatedHome::new().unwrap();
        let mut plan = RhoLaunchPlan::matrix(
            PathBuf::from(env!("CARGO_BIN_EXE_rho")),
            &home,
            PtySize {
                rows: 28,
                cols: 100,
            },
        );
        if let Some(editor) = editor {
            plan = plan.with_env("EDITOR", editor);
        }
        let mut harness =
            PtyHarness::spawn_named(&plan, format!("external_editor_{name}")).unwrap();
        harness
            .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
            .unwrap();
        let draft = format!("preserved {name} draft");
        harness.type_text(&draft).unwrap();
        harness.inject_key(&Key::Ctrl('g')).unwrap();
        harness
            .wait_for_text(expected_error, WaitTimeout::secs(10, "editor error status"))
            .unwrap();
        harness.settle_input();
        harness.inject_key(&Key::Enter).unwrap();
        harness
            .wait_for_text(
                &format!("fixture response: {draft}"),
                WaitTimeout::secs(20, "preserved draft response"),
            )
            .unwrap();
        assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
    }
}

#[test]
fn visual_is_preferred_over_editor_at_launch() {
    use std::os::unix::fs::PermissionsExt;

    let home = IsolatedHome::new().unwrap();
    let editor_dir = tempfile::tempdir().unwrap();
    let visual = editor_dir.path().join("visual-editor");
    let editor = editor_dir.path().join("fallback-editor");
    std::fs::write(
        &visual,
        r#"#!/bin/sh
printf 'edited by visual\n' > "$1"
"#,
    )
    .unwrap();
    std::fs::write(
        &editor,
        r#"#!/bin/sh
printf 'edited by editor\n' > "$1"
"#,
    )
    .unwrap();
    std::fs::set_permissions(&visual, std::fs::Permissions::from_mode(0o700)).unwrap();
    std::fs::set_permissions(&editor, std::fs::Permissions::from_mode(0o700)).unwrap();

    let plan = RhoLaunchPlan::matrix(
        PathBuf::from(env!("CARGO_BIN_EXE_rho")),
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("VISUAL", visual.display().to_string())
    .with_env("EDITOR", editor.display().to_string());
    let mut harness = PtyHarness::spawn_named(&plan, "external_editor_visual_pref").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.type_text("prefer visual").unwrap();
    harness.inject_key(&Key::Ctrl('g')).unwrap();
    harness
        .wait_for_text(
            "edited by visual",
            WaitTimeout::secs(10, "visual editor return"),
        )
        .unwrap();
    harness.settle_input();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "fixture response: edited by visual",
            WaitTimeout::secs(20, "visual preference response"),
        )
        .unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn editor_job_control_signals_do_not_interrupt_rho() {
    use std::os::unix::fs::PermissionsExt;

    let home = IsolatedHome::new().unwrap();
    let editor_dir = tempfile::tempdir().unwrap();
    let editor = editor_dir.path().join("interruptible-editor");
    std::fs::write(
        &editor,
        r#"#!/bin/sh
trap 'exit 130' INT
trap 'printf "INTERRUPTIBLE_EDITOR_CONTINUED\\n"' CONT
printf 'INTERRUPTIBLE_EDITOR_READY\n'
while :; do sleep 1; done
"#,
    )
    .unwrap();
    std::fs::set_permissions(&editor, std::fs::Permissions::from_mode(0o700)).unwrap();
    let plan = RhoLaunchPlan::matrix(
        PathBuf::from(env!("CARGO_BIN_EXE_rho")),
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("EDITOR", editor.display().to_string());
    let mut harness = PtyHarness::spawn_named(&plan, "external_editor_ctrl_c").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness
        .type_text("draft survives editor interrupt")
        .unwrap();
    let expected_screen_entries = harness.raw_sequence_occurrences(ALTERNATE_SCREEN_ENTER) + 1;
    harness.inject_key(&Key::Ctrl('g')).unwrap();
    harness
        .wait_for_text(
            "INTERRUPTIBLE_EDITOR_READY",
            WaitTimeout::secs(10, "editor ready"),
        )
        .unwrap();

    harness.inject_key(&Key::Ctrl('z')).unwrap();
    std::thread::sleep(Duration::from_millis(200));
    let process_group = i32::try_from(harness.child_pid().unwrap()).unwrap();
    assert_eq!(unsafe { libc::kill(-process_group, libc::SIGCONT) }, 0);
    harness
        .wait_for_text(
            "INTERRUPTIBLE_EDITOR_CONTINUED",
            WaitTimeout::secs(10, "editor continued after ctrl-z"),
        )
        .unwrap();

    harness.inject_key(&Key::Ctrl('c')).unwrap();
    harness
        .wait_for_raw_sequence_occurrences(
            ALTERNATE_SCREEN_ENTER,
            expected_screen_entries,
            WaitTimeout::secs(10, "TUI resumed after editor interrupt"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "draft survives editor interrupt",
            WaitTimeout::secs(10, "rho resumed after editor interrupt"),
        )
        .unwrap();
    harness.settle_input();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "fixture response: draft survives editor interrupt",
            WaitTimeout::secs(20, "preserved draft response"),
        )
        .unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}

#[test]
fn external_editor_works_during_an_active_turn() {
    use std::os::unix::fs::PermissionsExt;

    let home = IsolatedHome::new().unwrap();
    let editor_dir = tempfile::tempdir().unwrap();
    let editor = editor_dir.path().join("running-turn-editor");
    std::fs::write(
        &editor,
        r#"#!/bin/sh
printf 'RUNNING_EDITOR_READY\n'
IFS= read -r replacement
printf '%s\n' "$replacement" > "$1"
"#,
    )
    .unwrap();
    std::fs::set_permissions(&editor, std::fs::Permissions::from_mode(0o700)).unwrap();

    let binary = PathBuf::from(env!("CARGO_BIN_EXE_rho"));
    let plan = RhoLaunchPlan::matrix(
        binary,
        &home,
        PtySize {
            rows: 28,
            cols: 100,
        },
    )
    .with_env("EDITOR", editor.display().to_string());
    let mut harness = PtyHarness::spawn_named(&plan, "external_editor_during_turn").unwrap();
    harness
        .wait_for_text("gpt-5.5", WaitTimeout::secs(20, "startup"))
        .unwrap();
    harness.submit_text("fixture delay").unwrap();
    harness
        .wait_for_text(
            "partial assistant before cancellation",
            WaitTimeout::secs(20, "active turn"),
        )
        .unwrap();

    harness.type_text("draft during turn").unwrap();
    let expected_screen_entries = harness.raw_sequence_occurrences(ALTERNATE_SCREEN_ENTER) + 1;
    harness.inject_key(&Key::Ctrl('g')).unwrap();
    harness
        .wait_for_text(
            "RUNNING_EDITOR_READY",
            WaitTimeout::secs(10, "running editor ready"),
        )
        .unwrap();
    harness.type_text("edited steering prompt").unwrap();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_raw_sequence_occurrences(
            ALTERNATE_SCREEN_ENTER,
            expected_screen_entries,
            WaitTimeout::secs(10, "TUI resumed during active turn"),
        )
        .unwrap();
    harness
        .wait_for_text(
            "edited steering prompt",
            WaitTimeout::secs(10, "running editor return"),
        )
        .unwrap();
    harness.settle_input();
    harness.inject_key(&Key::Enter).unwrap();
    harness
        .wait_for_text(
            "queued steer 1 for after the current assistant turn",
            WaitTimeout::secs(10, "edited steering queued"),
        )
        .unwrap();

    harness.inject_key(&Key::Esc).unwrap();
    harness
        .wait_for_quiet(Duration::from_millis(250), WaitTimeout::secs(10, "cancel"))
        .unwrap();
    harness.inject_key(&Key::Ctrl('c')).unwrap();
    assert_eq!(harness.quit_with_exit_command().unwrap(), 0);
}