rho-coding-agent 1.9.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
pub(super) use crate::config::default_inline_shell as default_shell;

use std::{
    env,
    path::{Path, PathBuf},
    process::Stdio,
};

use tokio::{
    io::{AsyncRead, AsyncReadExt},
    process::Command,
    sync::mpsc,
};

const INLINE_SHELL_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum InlineShellMode {
    IncludeInContext,
    ExcludeFromContext,
}

impl InlineShellMode {
    pub(super) fn parse(input: &str) -> Option<(Self, &str)> {
        if let Some(command) = input.strip_prefix("!!") {
            Some((Self::ExcludeFromContext, command.trim()))
        } else {
            input
                .strip_prefix('!')
                .map(|command| (Self::IncludeInContext, command.trim()))
        }
    }

    pub(super) const fn included_in_context(self) -> bool {
        matches!(self, Self::IncludeInContext)
    }
}

pub(super) fn mode(input: &str) -> Option<InlineShellMode> {
    InlineShellMode::parse(input).map(|(mode, _)| mode)
}

pub(super) fn mode_when_idle(_running: bool, input: &str) -> Option<InlineShellMode> {
    mode(input)
}

pub(super) fn mode_hint_when_idle(
    _running: bool,
    input: &str,
) -> Option<(&'static str, ratatui::style::Style)> {
    mode_hint(input)
}

pub(super) fn mode_hint(input: &str) -> Option<(&'static str, ratatui::style::Style)> {
    match mode(input)? {
        InlineShellMode::IncludeInContext => Some((
            "SHELL - output will be included in model context",
            super::Theme::shell_context(),
        )),
        InlineShellMode::ExcludeFromContext => Some((
            "LOCAL SHELL - output will not be included in model context",
            super::Theme::shell_local(),
        )),
    }
}

pub(super) struct PendingShellTask {
    mode: InlineShellMode,
    max_output_bytes: usize,
    shell: String,
    command: String,
    stdout: String,
    stderr: String,
    updates: mpsc::UnboundedReceiver<ShellStreamUpdate>,
    handle: tokio::task::JoinHandle<std::io::Result<ShellOutput>>,
}

#[derive(Clone, Copy)]
enum ShellStreamKind {
    Stdout,
    Stderr,
}

struct ShellStreamUpdate {
    kind: ShellStreamKind,
    text: String,
}

pub(super) struct DeferredShellContext {
    context: String,
    persisted_display: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct ShellOutput {
    pub(super) shell: String,
    pub(super) command: String,
    pub(super) stdout: String,
    pub(super) stderr: String,
    pub(super) exit_code: String,
    pub(super) ok: bool,
}

pub(super) fn available_shells(selected: &str) -> Vec<String> {
    let candidates: &[&str] = if cfg!(windows) {
        &["powershell", "pwsh", "cmd"]
    } else {
        &["bash", "zsh", "fish", "sh"]
    };
    let mut shells = candidates
        .iter()
        .filter(|shell| executable_exists(shell))
        .map(|shell| (*shell).to_string())
        .collect::<Vec<_>>();
    if !selected.is_empty() && !shells.iter().any(|shell| shell == selected) {
        shells.push(selected.to_string());
    }
    shells
}

#[cfg(test)]
pub(super) async fn execute(
    shell: &str,
    command: &str,
    cwd: &Path,
) -> std::io::Result<ShellOutput> {
    execute_streaming(shell, command, cwd, None).await
}

async fn execute_streaming(
    shell: &str,
    command: &str,
    cwd: &Path,
    updates: Option<mpsc::UnboundedSender<ShellStreamUpdate>>,
) -> std::io::Result<ShellOutput> {
    let mut process = Command::new(shell);
    match executable_name(shell).to_ascii_lowercase().as_str() {
        "powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => {
            process.args(["-NoLogo", "-NoProfile", "-Command", command]);
        }
        "cmd" | "cmd.exe" => {
            process.args(["/C", command]);
        }
        "sh" | "sh.exe" => {
            process.args(["-c", command]);
        }
        _ => {
            process.args(["-lc", command]);
        }
    }
    let mut child = process
        .current_dir(cwd)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()?;
    let stdout = child.stdout.take().expect("stdout configured as piped");
    let stderr = child.stderr.take().expect("stderr configured as piped");
    let stdout_updates = updates.clone();
    let stdout_reader = read_stream(stdout, ShellStreamKind::Stdout, stdout_updates);
    let stderr_reader = read_stream(stderr, ShellStreamKind::Stderr, updates);
    let wait = async {
        match tokio::time::timeout(INLINE_SHELL_TIMEOUT, child.wait()).await {
            Ok(status) => status,
            Err(_) => {
                child.kill().await?;
                let _ = child.wait().await;
                Err(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!(
                        "inline shell command timed out after {} seconds",
                        INLINE_SHELL_TIMEOUT.as_secs()
                    ),
                ))
            }
        }
    };
    let (stdout, stderr, status) = tokio::join!(stdout_reader, stderr_reader, wait);
    let status = status?;
    Ok(ShellOutput {
        shell: shell.to_string(),
        command: command.to_string(),
        stdout: stdout?,
        stderr: stderr?,
        exit_code: status
            .code()
            .map_or_else(|| "signal".into(), |code| code.to_string()),
        ok: status.success(),
    })
}

async fn read_stream(
    mut stream: impl AsyncRead + Unpin,
    kind: ShellStreamKind,
    updates: Option<mpsc::UnboundedSender<ShellStreamUpdate>>,
) -> std::io::Result<String> {
    let mut output = Vec::new();
    let mut buffer = [0; 4096];
    loop {
        let read = stream.read(&mut buffer).await?;
        if read == 0 {
            break;
        }
        output.extend_from_slice(&buffer[..read]);
        if let Some(updates) = &updates {
            let _ = updates.send(ShellStreamUpdate {
                kind,
                text: String::from_utf8_lossy(&buffer[..read]).into_owned(),
            });
        }
    }
    Ok(String::from_utf8_lossy(&output).into_owned())
}

pub(super) fn context_text(output: &ShellOutput) -> String {
    format!(
        "Inline shell command executed with {}:\n```shell\n{}\n```\nstdout:\n```text\n{}\n```\nstderr:\n```text\n{}\n```\nexit code: {}",
        output.shell,
        output.command,
        output.stdout,
        output.stderr,
        output.exit_code
    )
}

pub(super) fn display_text(output: &ShellOutput, included_in_context: bool) -> String {
    display_lines(output, included_in_context).join("\n")
}

pub(super) fn display_lines(output: &ShellOutput, _included_in_context: bool) -> Vec<String> {
    let prompt = match output.shell.to_ascii_lowercase().as_str() {
        "powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => "PS",
        _ => "$",
    };
    let mut lines = vec![format!("{prompt} {}", output.command)];
    if !output.stdout.is_empty() {
        lines.push(String::new());
        lines.push(output.stdout.trim_end().to_string());
    }
    lines
}

fn executable_exists(executable: &str) -> bool {
    let path = Path::new(executable);
    if path.components().count() > 1 {
        return path.is_file();
    }
    env::var_os("PATH").is_some_and(|paths| {
        env::split_paths(&paths)
            .any(|directory| executable_paths(&directory, executable).any(|path| path.is_file()))
    })
}

fn executable_paths(directory: &Path, executable: &str) -> impl Iterator<Item = PathBuf> {
    let mut paths = vec![directory.join(executable)];
    if cfg!(windows) && Path::new(executable).extension().is_none() {
        paths.push(directory.join(format!("{executable}.exe")));
        paths.push(directory.join(format!("{executable}.cmd")));
        paths.push(directory.join(format!("{executable}.bat")));
    }
    paths.into_iter()
}

fn executable_name(shell: &str) -> &str {
    Path::new(shell)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or(shell)
}

impl super::App {
    pub(super) fn start_inline_shell(
        &mut self,
        mode: InlineShellMode,
        command: String,
    ) -> anyhow::Result<()> {
        if command.is_empty() {
            self.status = "enter a shell command after ! or !!".into();
            return Ok(());
        }
        let config = self.info.services.config_repository.load()?;
        let shell = if config.inline_shell.trim().is_empty() {
            default_shell()
        } else {
            config.inline_shell
        };
        self.push_input_history(&format!(
            "{}{}",
            if mode.included_in_context() {
                "!"
            } else {
                "!!"
            },
            command
        ));
        let cwd = self.info.runtime.cwd.clone();
        let task_shell = shell.clone();
        let task_command = command.clone();
        let (updates_tx, updates_rx) = mpsc::unbounded_channel();
        self.pending_inline_shells.push(PendingShellTask {
            mode,
            max_output_bytes: config.max_output_bytes,
            shell: shell.clone(),
            command: command.clone(),
            stdout: String::new(),
            stderr: String::new(),
            updates: updates_rx,
            handle: tokio::spawn(async move {
                execute_streaming(&task_shell, &task_command, &cwd, Some(updates_tx)).await
            }),
        });
        self.status = format!("running {shell}");
        Ok(())
    }

    pub(super) fn cancel_inline_shells(&mut self) -> bool {
        if self.pending_inline_shells.is_empty() {
            return false;
        }
        for mut task in std::mem::take(&mut self.pending_inline_shells) {
            task.drain_updates();
            task.handle.abort();
            let output = ShellOutput {
                shell: task.shell,
                command: task.command,
                stdout: task.stdout,
                stderr: task.stderr,
                exit_code: "cancelled".into(),
                ok: false,
            };
            self.insert_entry(&super::Entry::Tool(super::ToolEntry {
                state: super::ToolEntryState::Finished {
                    ok: false,
                    display_style: rho_tools::tool::ToolDisplayStyle::file_or_command(),
                },
                display_lines: display_lines(&output, task.mode.included_in_context()),
                expanded: true,
                image: None,
            }));
        }
        self.status = "inline shell cancelled".into();
        true
    }

    pub(super) async fn finish_completed_inline_shells(&mut self) -> anyhow::Result<bool> {
        let mut changed = false;
        for task in &mut self.pending_inline_shells {
            changed |= task.drain_updates();
        }
        while self
            .pending_inline_shells
            .first()
            .is_some_and(|task| task.handle.is_finished())
        {
            let mut task = self.pending_inline_shells.remove(0);
            task.drain_updates();
            self.finish_inline_shell_task(task).await?;
            changed = true;
        }
        Ok(changed)
    }

    pub(super) async fn finish_all_inline_shells(&mut self) -> anyhow::Result<()> {
        while !self.pending_inline_shells.is_empty() {
            let task = self.pending_inline_shells.remove(0);
            self.finish_inline_shell_task(task).await?;
        }
        Ok(())
    }

    async fn finish_inline_shell_task(&mut self, task: PendingShellTask) -> anyhow::Result<()> {
        let output = match task.handle.await? {
            Ok(output) => output,
            Err(error) => {
                self.insert_entry(&super::Entry::Error(format!(
                    "could not run inline shell: {error}"
                )));
                self.status = "inline shell failed".into();
                return Ok(());
            }
        };
        if task.mode.included_in_context() {
            self.deferred_inline_shell_context
                .push(DeferredShellContext {
                    context: rho_tools::tool::truncate(
                        context_text(&output),
                        task.max_output_bytes,
                    ),
                    persisted_display: rho_tools::tool::truncate(
                        format!(
                            "!{}\n\n{}",
                            output.command,
                            display_text(&output, /*included_in_context*/ true)
                        ),
                        task.max_output_bytes,
                    ),
                });
        }
        let display_text = rho_tools::tool::truncate(
            display_text(&output, task.mode.included_in_context()),
            task.max_output_bytes,
        );
        self.finish_streams();
        self.insert_entry(&super::Entry::Tool(super::ToolEntry {
            state: super::ToolEntryState::Finished {
                ok: output.ok,
                display_style: rho_tools::tool::ToolDisplayStyle::file_or_command(),
            },
            display_lines: display_text.lines().map(str::to_string).collect(),
            expanded: true,
            image: None,
        }));
        self.statusline.refresh_git_branch();
        self.status = if output.ok {
            if task.mode.included_in_context() {
                "shell output pending context insertion".into()
            } else {
                "shell output excluded from context".into()
            }
        } else {
            format!("shell exited with {}", output.exit_code)
        };
        Ok(())
    }

    pub(super) fn running_inline_shell_entries(
        &self,
    ) -> impl Iterator<Item = super::ToolEntry> + '_ {
        self.pending_inline_shells
            .iter()
            .map(PendingShellTask::tool_entry)
    }

    pub(super) fn insert_deferred_inline_shell_context(
        &mut self,
        agent: &mut super::InteractiveRuntime,
    ) -> anyhow::Result<()> {
        let inserted = !self.deferred_inline_shell_context.is_empty();
        for deferred in std::mem::take(&mut self.deferred_inline_shell_context) {
            agent.append_user_context_with_display(deferred.context, deferred.persisted_display)?;
        }
        if inserted && !self.running {
            self.status = "shell output included in context".into();
        }
        Ok(())
    }

    pub(super) fn block_pasted_inline_shell(&mut self) -> anyhow::Result<()> {
        self.insert_entry(&super::Entry::Error(
            "inline shell commands cannot start from collapsed pasted content".into(),
        ));
        self.status = "inline shell paste blocked".into();
        Ok(())
    }

    pub(super) fn clear_submitted_input(&mut self) {
        self.input.clear();
        self.paste_segments.clear();
        self.input_cursor = 0;
        self.clamp_command_selection();
    }
}

impl PendingShellTask {
    fn drain_updates(&mut self) -> bool {
        let mut changed = false;
        while let Ok(update) = self.updates.try_recv() {
            match update.kind {
                ShellStreamKind::Stdout => self.stdout.push_str(&update.text),
                ShellStreamKind::Stderr => self.stderr.push_str(&update.text),
            }
            changed = true;
        }
        changed
    }

    fn tool_entry(&self) -> super::ToolEntry {
        let output = ShellOutput {
            shell: self.shell.clone(),
            command: self.command.clone(),
            stdout: self.stdout.clone(),
            stderr: self.stderr.clone(),
            exit_code: "running".into(),
            ok: true,
        };
        super::ToolEntry {
            state: super::ToolEntryState::Running,
            display_lines: display_lines(&output, self.mode.included_in_context()),
            expanded: true,
            image: None,
        }
    }
}

#[cfg(test)]
#[path = "inline_shell_tests.rs"]
mod tests;