agent-commander 0.5.1

A Rust library to control agents enclosed in CLI commands like Anthropic Claude Code CLI
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
552
553
554
//! Execute commands while continuously draining both process pipes.
use std::{
    io,
    process::{ExitStatus, Stdio},
    sync::{Arc, Mutex},
    time::Duration,
};
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
    process::{Child, ChildStdin, Command},
    task::JoinHandle,
    time::Instant,
};

/// Time a process group may keep running after `terminate` before `SIGKILL`.
pub const DEFAULT_KILL_AFTER: Duration = Duration::from_secs(5);
/// Time the pipes may stay open after the wrapper shell exited.
///
/// A grandchild that inherited stdout (`(sleep 600 &)`) otherwise holds
/// `wait_for_exit` open forever (hive-mind#3015).
pub const DEFAULT_PIPE_DRAIN: Duration = Duration::from_secs(5);

/// Command execution result.
#[derive(Debug, Clone, Default)]
pub struct ExecutionResult {
    pub exit_code: i32,
    /// Signal that ended the process (`SIGKILL`), also inferred from 128 + N
    pub signal: Option<String>,
    pub stdout: String,
    pub stderr: String,
    pub command: String,
}

const SIGNALS: &[(i32, &str)] = &[
    (1, "SIGHUP"),
    (2, "SIGINT"),
    (3, "SIGQUIT"),
    (4, "SIGILL"),
    (5, "SIGTRAP"),
    (6, "SIGABRT"),
    (8, "SIGFPE"),
    (9, "SIGKILL"),
    (11, "SIGSEGV"),
    (13, "SIGPIPE"),
    (14, "SIGALRM"),
    (15, "SIGTERM"),
    #[cfg(target_os = "linux")]
    (7, "SIGBUS"),
    #[cfg(target_os = "linux")]
    (10, "SIGUSR1"),
    #[cfg(target_os = "linux")]
    (12, "SIGUSR2"),
];

/// Name of a signal number (`9` is `SIGKILL`).
pub fn signal_name(number: i32) -> Option<&'static str> {
    SIGNALS
        .iter()
        .find(|(value, _)| *value == number)
        .map(|(_, name)| *name)
}

/// Signal named by a shell exit code of 128 + N (`137` is `SIGKILL`).
pub fn signal_from_exit_code(exit_code: i32) -> Option<&'static str> {
    if exit_code > 128 {
        signal_name(exit_code - 128)
    } else {
        None
    }
}

/// Exit code and signal of a status; a signal becomes 128 + N.
pub fn normalize_exit(status: ExitStatus) -> (i32, Option<&'static str>) {
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(number) = status.signal() {
            return (128 + number, signal_name(number));
        }
    }
    let code = status.code().unwrap_or(1);
    (code, signal_from_exit_code(code))
}

type Buffer = Arc<Mutex<Vec<u8>>>;
type Reader = JoinHandle<io::Result<()>>;

/// Join pipe readers until `deadline`; returns whether all finished. A
/// finished reader is taken out, so it is never polled again.
async fn join_readers(
    readers: &mut [Option<Reader>],
    deadline: Option<Instant>,
) -> io::Result<bool> {
    for slot in readers.iter_mut() {
        let Some(reader) = slot.as_mut() else {
            continue;
        };
        let joined = match deadline {
            None => reader.await,
            Some(deadline) => match tokio::time::timeout_at(deadline, reader).await {
                Ok(joined) => joined,
                Err(_) => return Ok(false),
            },
        };
        *slot = None;
        joined.map_err(io::Error::other)??;
    }
    Ok(true)
}

fn read_buffer(buffer: &Buffer) -> String {
    let bytes = buffer.lock().unwrap_or_else(|e| e.into_inner());
    String::from_utf8_lossy(&bytes).into_owned()
}

async fn drain<R: AsyncRead + Unpin>(
    mut pipe: R,
    buffer: Buffer,
    attached: bool,
    stderr: bool,
) -> io::Result<()> {
    let mut chunk = [0; 8192];
    loop {
        let count = pipe.read(&mut chunk).await?;
        if count == 0 {
            break;
        }
        buffer
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .extend_from_slice(&chunk[..count]);
        if attached {
            if stderr {
                tokio::io::stderr().write_all(&chunk[..count]).await?;
            } else {
                tokio::io::stdout().write_all(&chunk[..count]).await?;
            }
        }
    }
    Ok(())
}

/// Run `kill -<signal> -- -<pgid>`; `0` checks whether the group exists.
#[cfg(unix)]
async fn kill_group(pgid: u32, signal: &str) -> io::Result<std::process::Output> {
    // bash's kill builtin is available wherever our executor is supported.
    let signal = signal.trim_start_matches("SIG");
    Command::new("bash")
        .arg("-c")
        .arg(format!("kill -{signal} -- -{pgid}"))
        .stdin(Stdio::null())
        .output()
        .await
}

/// Whether any process in the group led by `pgid` is alive.
///
/// The wrapper shell can exit while the agent it started still runs in the
/// same group (hive-mind#3015). A group the caller may not signal is alive.
pub async fn is_process_group_alive(pgid: u32) -> bool {
    #[cfg(unix)]
    {
        match kill_group(pgid, "0").await {
            Ok(output) if output.status.success() => true,
            Ok(output) => String::from_utf8_lossy(&output.stderr)
                .to_lowercase()
                .contains("not permitted"),
            Err(_) => false,
        }
    }
    #[cfg(not(unix))]
    {
        let _ = pgid;
        false
    }
}

/// Execute a command, or return the command without spawning in dry-run mode.
pub async fn execute_command(
    command: &str,
    dry_run: bool,
    attached: bool,
) -> io::Result<ExecutionResult> {
    if dry_run {
        println!("Dry run - command that would be executed:\n{}", command);
        return Ok(ExecutionResult {
            command: command.into(),
            ..Default::default()
        });
    }
    let mut handle = start_command(command, attached).await?;
    let exit_code = handle.wait_for_exit().await?;
    let (stdout, stderr, _) = handle.get_output();
    Ok(ExecutionResult {
        exit_code,
        signal: handle.signal().map(String::from),
        stdout: stdout.into(),
        stderr: stderr.into(),
        command: command.into(),
    })
}

/// Non-blocking process handle. Pipe readers run immediately after spawning.
pub struct ProcessHandle {
    pub command: String,
    child: Child,
    pid: Option<u32>,
    readers: Option<(Reader, Reader)>,
    stdout_buffer: Buffer,
    stderr_buffer: Buffer,
    pipe_drain: Option<Duration>,
    stdout: String,
    stderr: String,
    exit_code: Option<i32>,
    signal: Option<&'static str>,
    stdin: Option<ChildStdin>,
}
impl ProcessHandle {
    /// Bound the wait for open pipes after the wrapper exits; `None` waits
    /// until every holder closes them.
    #[must_use]
    pub const fn with_pipe_drain(mut self, pipe_drain: Option<Duration>) -> Self {
        self.pipe_drain = pipe_drain;
        self
    }

    /// Process id of the wrapper shell, which also leads the process group.
    pub const fn pid(&self) -> Option<u32> {
        self.pid
    }

    /// Wait for process completion and all output, preserving exact bytes as UTF-8.
    ///
    /// Ends a piped stdin first: nothing can write while this borrows the
    /// handle, and a tool reading stdin would otherwise never exit.
    pub async fn wait_for_exit(&mut self) -> io::Result<i32> {
        if let Some(code) = self.exit_code {
            return Ok(code);
        }
        self.end_stdin();
        let status = self.child.wait().await?;
        if let Some((stdout, stderr)) = self.readers.take() {
            let mut readers = [Some(stdout), Some(stderr)];
            let deadline = self.pipe_drain.map(|limit| Instant::now() + limit);
            if !join_readers(&mut readers, deadline).await? {
                // A grandchild still holds the pipes: stop the group, then
                // keep what was read.
                #[cfg(unix)]
                if let Some(pid) = self.pid {
                    if is_process_group_alive(pid).await {
                        let _ = kill_group(pid, "KILL").await;
                    }
                }
                let grace = Some(Instant::now() + Duration::from_millis(500));
                if !join_readers(&mut readers, grace).await? {
                    for reader in readers.iter().flatten() {
                        reader.abort();
                    }
                }
            }
        }
        self.stdout = read_buffer(&self.stdout_buffer);
        self.stderr = read_buffer(&self.stderr_buffer);
        let (code, signal) = normalize_exit(status);
        self.exit_code = Some(code);
        self.signal = signal;
        Ok(code)
    }

    /// Terminate the command and its Unix process group with `SIGTERM`,
    /// escalating to `SIGKILL` after [`DEFAULT_KILL_AFTER`].
    pub async fn terminate(&mut self) -> io::Result<()> {
        self.terminate_with("SIGTERM", Some(DEFAULT_KILL_AFTER))
            .await
            .map(|_| ())
    }

    /// Send `signal` to the process group while any member is alive, even
    /// after the wrapper shell exited. Unless the signal is `SIGKILL`,
    /// `kill_after` sends `SIGKILL` to a group that is still alive then.
    ///
    /// Returns whether a signal was sent.
    pub async fn terminate_with(
        &mut self,
        signal: &str,
        kill_after: Option<Duration>,
    ) -> io::Result<bool> {
        #[cfg(unix)]
        {
            let Some(pid) = self.pid else {
                return Ok(false);
            };
            if !is_process_group_alive(pid).await {
                return Ok(false);
            }
            let output = kill_group(pid, signal).await?;
            if std::env::var_os("AGENT_COMMANDER_DEBUG").is_some() {
                eprintln!(
                    "Cancel process group {pid}: {} {}",
                    output.status,
                    String::from_utf8_lossy(&output.stderr)
                );
            }
            if !output.status.success() && is_process_group_alive(pid).await {
                return Err(io::Error::other(
                    String::from_utf8_lossy(&output.stderr).into_owned(),
                ));
            }
            let is_kill = matches!(signal.trim_start_matches("SIG"), "KILL" | "9");
            if let (Some(delay), false) = (kill_after, is_kill) {
                tokio::spawn(async move {
                    tokio::time::sleep(delay).await;
                    if is_process_group_alive(pid).await {
                        let _ = kill_group(pid, "KILL").await;
                    }
                });
            }
            Ok(true)
        }
        #[cfg(not(unix))]
        {
            let _ = (signal, kill_after);
            if self.exit_code.is_some() || self.child.try_wait()?.is_some() {
                return Ok(false);
            }
            self.child.start_kill()?;
            Ok(true)
        }
    }

    /// Get collected output after waiting for completion.
    pub fn get_output(&self) -> (&str, &str, Option<i32>) {
        (&self.stdout, &self.stderr, self.exit_code)
    }
    /// Stdout read so far, also while the process runs.
    pub fn peek_stdout(&self) -> String {
        read_buffer(&self.stdout_buffer)
    }
    /// Signal that ended the process, after waiting for completion.
    pub const fn signal(&self) -> Option<&'static str> {
        self.signal
    }
    /// Whether the wrapper process has exited, without waiting for it.
    pub fn try_wait(&mut self) -> io::Result<bool> {
        Ok(self.exit_code.is_some() || self.child.try_wait()?.is_some())
    }
    /// Check whether completion has been collected.
    pub const fn has_exited(&self) -> bool {
        self.exit_code.is_some()
    }
    /// Whether stdin is piped and still open (see [`start_command_with_stdin`]).
    pub const fn has_stdin(&self) -> bool {
        self.stdin.is_some()
    }
    /// Write to the piped stdin and flush.
    ///
    /// Fails when stdin was not piped, was ended, or the process exited
    /// (`BrokenPipe`).
    pub async fn write_stdin(&mut self, data: &str) -> io::Result<()> {
        let stdin = self
            .stdin
            .as_mut()
            .ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "stdin is not open"))?;
        stdin.write_all(data.as_bytes()).await?;
        stdin.flush().await
    }
    /// Close the piped stdin (end of input). Returns whether it was open.
    pub fn end_stdin(&mut self) -> bool {
        self.stdin.take().is_some()
    }
}

/// Start a command and drain stdout/stderr concurrently, with optional console output.
pub async fn start_command(command: &str, attached: bool) -> io::Result<ProcessHandle> {
    spawn_command(command, attached, false, None)
}

/// Like [`start_command`], with stdin piped for
/// [`ProcessHandle::write_stdin`] (live input, stream-json frames).
pub async fn start_command_with_stdin(command: &str, attached: bool) -> io::Result<ProcessHandle> {
    spawn_command(command, attached, true, None)
}

/// Like [`start_command`], with an explicit child environment that replaces
/// the inherited one (issue #60: values stay off the command line).
pub async fn start_command_with_env(
    command: &str,
    attached: bool,
    pipe_stdin: bool,
    env: &[(String, String)],
) -> io::Result<ProcessHandle> {
    spawn_command(command, attached, pipe_stdin, Some(env))
}

fn apply_env(command_builder: &mut Command, env: Option<&[(String, String)]>) {
    if let Some(env) = env {
        command_builder.env_clear();
        command_builder.envs(env.iter().map(|(k, v)| (k.as_str(), v.as_str())));
    }
}

fn spawn_command(
    command: &str,
    attached: bool,
    pipe_stdin: bool,
    env: Option<&[(String, String)]>,
) -> io::Result<ProcessHandle> {
    let mut command_builder = Command::new("bash");
    apply_env(&mut command_builder, env);
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        command_builder.as_std_mut().process_group(0);
    }
    let mut child = command_builder
        .arg("-c")
        .arg(command)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(if pipe_stdin {
            Stdio::piped()
        } else {
            Stdio::null()
        })
        .kill_on_drop(true)
        .spawn()?;
    let stdin = child.stdin.take();
    let stdout = child.stdout.take().expect("piped stdout");
    let stderr = child.stderr.take().expect("piped stderr");
    let stdout_buffer = Buffer::default();
    let stderr_buffer = Buffer::default();
    Ok(ProcessHandle {
        command: command.into(),
        pid: child.id(),
        child,
        readers: Some((
            tokio::spawn(drain(stdout, Arc::clone(&stdout_buffer), attached, false)),
            tokio::spawn(drain(stderr, Arc::clone(&stderr_buffer), attached, true)),
        )),
        stdout_buffer,
        stderr_buffer,
        pipe_drain: Some(DEFAULT_PIPE_DRAIN),
        stdout: String::new(),
        stderr: String::new(),
        exit_code: None,
        signal: None,
        stdin,
    })
}

/// Start a detached command with all standard streams disconnected, in its
/// own process group (JS `spawn(..., { detached: true })`).
pub async fn execute_detached(command: &str) -> io::Result<Option<u32>> {
    execute_detached_with_env(command, None).await
}

/// Like [`execute_detached`], with an optional explicit child environment.
pub async fn execute_detached_with_env(
    command: &str,
    env: Option<&[(String, String)]>,
) -> io::Result<Option<u32>> {
    let mut command_builder = Command::new("bash");
    apply_env(&mut command_builder, env);
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        command_builder.as_std_mut().process_group(0);
    }
    let child = command_builder
        .arg("-c")
        .arg(command)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .stdin(Stdio::null())
        .spawn()?;
    Ok(child.id())
}
/// Signal cleanup callback.
pub type CleanupFn = Box<dyn Fn() + Send + Sync>;

/// Register a Ctrl+C cleanup callback in the current Tokio runtime.
/// Calling the returned function unregisters it without running cleanup.
pub fn setup_signal_handler<F>(cleanup_fn: F) -> impl Fn()
where
    F: Fn() + Send + Sync + 'static,
{
    let task = tokio::runtime::Handle::try_current().ok().map(|runtime| {
        runtime.spawn(async move {
            if tokio::signal::ctrl_c().await.is_ok() {
                cleanup_fn();
            }
        })
    });
    move || {
        if let Some(task) = &task {
            task.abort();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_execute_command_dry_run() {
        let result = execute_command("echo hello", true, false).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.command, "echo hello");
    }

    #[tokio::test]
    #[cfg(not(target_os = "windows"))]
    async fn test_execute_command_real() {
        let result = execute_command("echo hello", false, false).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("hello"));
    }

    #[tokio::test]
    #[cfg(not(target_os = "windows"))]
    async fn test_start_command_and_wait() {
        let mut handle = start_command("echo hello", false).await.unwrap();
        let exit_code = handle.wait_for_exit().await.unwrap();
        assert_eq!(exit_code, 0);

        let (stdout, _, _) = handle.get_output();
        assert!(stdout.contains("hello"));
    }

    #[tokio::test]
    #[cfg(not(target_os = "windows"))]
    async fn test_execute_detached() {
        let pid = execute_detached("sleep 0.1").await.unwrap();
        assert!(pid.is_some());
    }

    #[tokio::test]
    #[cfg(unix)]
    async fn test_execute_detached_leads_its_own_process_group() {
        // Like JS `spawn(..., { detached: true })`: Ctrl+C to the caller's
        // group must not reach the background agent.
        let pid = execute_detached("sleep 1").await.unwrap().unwrap();
        let output = tokio::process::Command::new("ps")
            .args(["-o", "pgid=", "-p", &pid.to_string()])
            .output()
            .await
            .unwrap();
        assert_eq!(
            String::from_utf8_lossy(&output.stdout).trim(),
            pid.to_string()
        );
    }
}