Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
82
crates/luxe-core/src/program/bounded.rs
Normal file
82
crates/luxe-core/src/program/bounded.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
//! Bounded runs — a program held to a deadline, its whole tree killed when it passes.
|
||||
|
||||
/// Why a bounded run produced no output.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum BoundedRunError {
|
||||
/// The program could not be started.
|
||||
#[error("could not start the program")]
|
||||
Spawn(#[source] std::io::Error),
|
||||
/// The deadline passed; the whole process tree has been killed.
|
||||
#[error("timed out after {}s", after.as_secs())]
|
||||
Timeout {
|
||||
/// The deadline that expired
|
||||
after: std::time::Duration,
|
||||
},
|
||||
/// The program started but waiting on it failed.
|
||||
#[error("lost the program while waiting for it")]
|
||||
Wait(#[source] std::io::Error),
|
||||
}
|
||||
|
||||
/// Run `command` to completion under `deadline`, killing its whole tree if it expires or the
|
||||
/// caller stops waiting.
|
||||
///
|
||||
/// Meant for a command built by [`super::captured_command`]: the tree kill reaches descendants only because
|
||||
/// the child leads its own process group. A caller owns the wording of its failures — the `git` tool
|
||||
/// names the usual culprit for a timeout, GitHub turns one into a typed error — so this returns
|
||||
/// the variant and leaves the sentence to them.
|
||||
pub async fn run_bounded(
|
||||
command: tokio::process::Command,
|
||||
deadline: std::time::Duration,
|
||||
) -> Result<std::process::Output, BoundedRunError> {
|
||||
run_bounded_with_input(command, &[], deadline).await
|
||||
}
|
||||
|
||||
/// [`run_bounded`] with `input` written to the program's stdin, the write held to the same
|
||||
/// deadline as the run.
|
||||
///
|
||||
/// Feeding a pipe is part of the run, not a step before it: a patch routinely outgrows the pipe's
|
||||
/// buffer, and the write then waits for the program to drain it — forever, if the program has
|
||||
/// stalled. A program that exits without reading all of `input` is not an error here; its status
|
||||
/// says what happened. Empty `input` leaves stdin as `command` had it.
|
||||
pub async fn run_bounded_with_input(
|
||||
mut command: tokio::process::Command,
|
||||
input: &[u8],
|
||||
deadline: std::time::Duration,
|
||||
) -> Result<std::process::Output, BoundedRunError> {
|
||||
if !input.is_empty() {
|
||||
command.stdin(std::process::Stdio::piped());
|
||||
}
|
||||
let mut child = command.spawn().map_err(BoundedRunError::Spawn)?;
|
||||
let mut reaper = TreeReaper(child.id());
|
||||
let stdin = child.stdin.take().filter(|_| !input.is_empty());
|
||||
let feed = async {
|
||||
if let Some(mut stdin) = stdin {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
// a broken pipe is the program having heard enough
|
||||
let _ = stdin.write_all(input).await;
|
||||
let _ = stdin.shutdown().await;
|
||||
}
|
||||
};
|
||||
let run = async { tokio::join!(feed, child.wait_with_output()).1 };
|
||||
match tokio::time::timeout(deadline, run).await {
|
||||
Ok(result) => {
|
||||
reaper.0 = None;
|
||||
result.map_err(BoundedRunError::Wait)
|
||||
}
|
||||
// the reaper takes the tree on the way out
|
||||
Err(_) => Err(BoundedRunError::Timeout { after: deadline }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Kills a process tree when dropped armed: on a timeout, or when the future waiting on the run is
|
||||
/// dropped, as a cancelled turn or an abandoned capture drops it. `kill_on_drop` alone reaches the
|
||||
/// child and nothing it started.
|
||||
struct TreeReaper(Option<u32>);
|
||||
|
||||
impl Drop for TreeReaper {
|
||||
fn drop(&mut self) {
|
||||
if let Some(pid) = self.0.take() {
|
||||
super::kill_tree(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user