Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(shell): a command killed at its deadline keeps what it printed

sanil-2392c69d50aebd
diff --git a/crates/openhuman-core/src/sandbox/shell_exec.rs b/crates/openhuman-core/src/sandbox/shell_exec.rs
index 5803d0ce1b..b2c95abf49 100644
--- a/crates/openhuman-core/src/sandbox/shell_exec.rs
+++ b/crates/openhuman-core/src/sandbox/shell_exec.rs
@@ -295,14 +295,14 @@ async fn collect_process(
tinybox::release_output(config, process)
.await
.map_err(|_| anyhow::anyhow!("TinyBox could not release command output"))?;
+ // What the command printed before it was stopped, both streams: a build
+ // or test killed at its deadline has usually written its progress and
+ // first errors by then. `timed_out` says why it stopped; the shell tool
+ // writes the timeout line.
Ok(SandboxExecResult {
exit_code: -1,
stdout: String::from_utf8_lossy(&stdout).into_owned(),
- stderr: if timed_out {
- format!("Command timed out after {}s", timeout.as_secs())
- } else {
- String::from_utf8_lossy(&stderr).into_owned()
- },
+ stderr: String::from_utf8_lossy(&stderr).into_owned(),
timed_out,
})
}
diff --git a/crates/openhuman-core/src/sandbox/shell_exec_fixture.rs b/crates/openhuman-core/src/sandbox/shell_exec_fixture.rs
index 3b53d2600e..6c498619fc 100644
--- a/crates/openhuman-core/src/sandbox/shell_exec_fixture.rs
+++ b/crates/openhuman-core/src/sandbox/shell_exec_fixture.rs
@@ -1,9 +1,14 @@
//! Test-only stand-in for the module's native shell: unit tests do not load a
//! native TinyBox artifact, so unjailed runs execute here. The module's own
//! behaviour (shell selection, environment, scratch) is covered by its crate and
-//! by the released-artifact test; this fixture only has to run a command.
+//! by the released-artifact test; this fixture only has to run a command, and
+//! like the module it keeps what a command printed before its deadline stopped
+//! it.
use std::process::Stdio;
+use std::time::Duration;
+
+use tokio::io::{AsyncRead, AsyncReadExt};
use super::{NativeShellRun, SandboxExecResult};
@@ -60,27 +65,68 @@ pub(crate) async fn run(run: NativeShellRun) -> anyhow::Result<SandboxExecResult
.stderr(Stdio::piped())
.kill_on_drop(true);
tinytools_std::timeout::own_process_group(command.as_std_mut());
- match tinytools_std::timeout::output_or_kill(&mut command, run.timeout).await {
- Ok(Ok(output)) => {
- // The module streams output into the caller's meter as it reads it;
- // the fixture folds the finished output in the same way.
- if let Some(meter) = &run.meter {
- meter.record(&output.stdout);
- meter.record(&output.stderr);
+ let mut child = command
+ .spawn()
+ .map_err(|error| anyhow::anyhow!("Failed to execute command: {error}"))?;
+ let pid = child.id();
+ // Read both streams as the command runs, like the module does, so a
+ // command stopped at its deadline keeps what it printed before.
+ let (mut stdout_pipe, mut stderr_pipe) = (child.stdout.take(), child.stderr.take());
+ let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
+ let finished = tokio::time::timeout(run.timeout, async {
+ let (status, (), ()) = tokio::join!(
+ child.wait(),
+ drain(&mut stdout_pipe, &mut stdout),
+ drain(&mut stderr_pipe, &mut stderr),
+ );
+ status
+ })
+ .await;
+ let (exit_code, timed_out) = match finished {
+ Ok(status) => (status?.code().unwrap_or(-1), false),
+ Err(_) => {
+ // Stop the command and everything it started, then give the
+ // readers a moment to drain what is left in the pipes.
+ if let Some(pid) = pid {
+ tinytools_std::timeout::kill_process_group(pid);
}
- Ok(SandboxExecResult {
- exit_code: output.status.code().unwrap_or(-1),
- stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
- stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
- timed_out: false,
+ let _ = child.start_kill();
+ let _ = tokio::time::timeout(Duration::from_secs(2), async {
+ tokio::join!(
+ drain(&mut stdout_pipe, &mut stdout),
+ drain(&mut stderr_pipe, &mut stderr),
+ )
})
+ .await;
+ (-1, true)
+ }
+ };
+ // The module streams output into the caller's meter as it reads it; the
+ // fixture folds the collected output in the same way.
+ if let Some(meter) = &run.meter {
+ meter.record(&stdout);
+ meter.record(&stderr);
+ }
+ Ok(SandboxExecResult {
+ exit_code,
+ stdout: String::from_utf8_lossy(&stdout).into_owned(),
+ stderr: String::from_utf8_lossy(&stderr).into_owned(),
+ timed_out,
+ })
+}
+
+/// Appends what `pipe` delivers to `into` until it closes. Reads are
+/// cancel-safe, so a drain cut short by the deadline keeps every chunk it read,
+/// and a later call picks up where it stopped.
+async fn drain(pipe: &mut Option<impl AsyncRead + Unpin>, into: &mut Vec<u8>) {
+ let Some(pipe) = pipe else {
+ return;
+ };
+ let mut chunk = [0u8; 8192];
+ while let Ok(read) = pipe.read(&mut chunk).await {
+ if read == 0 {
+ break;
}
- Ok(Err(error)) => anyhow::bail!("Failed to execute command: {error}"),
- Err(_) => Ok(SandboxExecResult {
- exit_code: -1,
- stdout: String::new(),
- stderr: format!("Command timed out after {}s", run.timeout.as_secs()),
- timed_out: true,
- }),
+ into.extend_from_slice(&chunk[..read]);
}
}
diff --git a/crates/openhuman-core/src/tools/impl/system/shell_support.rs b/crates/openhuman-core/src/tools/impl/system/shell_support.rs
index c402b9b9d6..71903ba766 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell_support.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell_support.rs
@@ -10,6 +10,8 @@ pub(super) const COMMAND_TIMEOUT_EXIT_CODE: i32 = 124;
pub(super) const COMMAND_TIMEOUT_NOTE: &str = "[exit 124: the command's own `timeout` limit expired; the shell tool's timeout_secs did not fire]";
/// The result when the tool's own `timeout_secs` deadline killed the command.
+/// It stays the first line of the result: TinyAgents' shell turn-budget note
+/// recognises a fired deadline by this prefix.
pub(super) fn tool_timeout_message(secs: u64) -> String {
format!(
"Command timed out after {secs}s and was killed: the shell tool's timeout_secs limit fired."
@@ -38,7 +40,7 @@ pub(super) fn exec_result(
) -> ToolResult {
match result {
Ok(result) if result.timed_out => {
- ToolResult::error(tool_timeout_message(effective.as_secs()))
+ ToolResult::error(timed_out_output(effective.as_secs(), &result))
}
Ok(result) if result.success() => {
if result.stderr.is_empty() {
@@ -56,6 +58,42 @@ pub(super) fn exec_result(
}
}
+/// The timeout line, then what the command printed before it was killed.
+///
+/// A test or build that runs past its deadline has usually already printed
+/// what the model needs: its first failures, how far it got, the error that
+/// stalled it. Dropping that leaves only "timed out", and the model reruns
+/// blind. When nothing came back, the result says why output can be missing
+/// (a pipeline such as `| tail`, or a program that buffers its output, holds
+/// it until the command exits), so the model does not read it as a hang.
+fn timed_out_output(secs: u64, result: &crate::sandbox::SandboxExecResult) -> String {
+ let mut out = tool_timeout_message(secs);
+ let stdout = result.stdout.trim_end();
+ let stderr = result.stderr.trim_end();
+ if stdout.is_empty() && stderr.is_empty() {
+ out.push_str(
+ " No output reached the tool before the kill; a pipeline such as `| tail` \
+ or a program that buffers its output holds it until the command exits.",
+ );
+ return out;
+ }
+ tracing::debug!(
+ stdout_bytes = stdout.len(),
+ stderr_bytes = stderr.len(),
+ "[shell] timed out: returning the output printed before the kill"
+ );
+ out.push_str(" Output before the kill:");
+ if !stdout.is_empty() {
+ out.push_str("\n[stdout]\n");
+ out.push_str(stdout);
+ }
+ if !stderr.is_empty() {
+ out.push_str("\n[stderr]\n");
+ out.push_str(stderr);
+ }
+ out
+}
+
/// Python's text-encoding defaults for a child, unless the parent sets its own.
/// Under `cmd.exe` the console code page is a legacy one (cp1252, cp437), so a
/// script printing anything outside it dies with `UnicodeEncodeError`. Both are
diff --git a/crates/openhuman-core/src/tools/impl/system/shell_tests_timeout_tests.rs b/crates/openhuman-core/src/tools/impl/system/shell_tests_timeout_tests.rs
index 4bb89217d3..787c76a0b2 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell_tests_timeout_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell_tests_timeout_tests.rs
@@ -129,3 +129,72 @@ async fn the_tools_deadline_kills_the_commands_grandchildren_too() {
"grandchild {grandchild} survived the shell tool's deadline"
);
}
+
+/// A command killed at its deadline still shows what it printed on both
+/// streams: a test that reported its first failures, or a build that wrote
+/// its progress to stderr, must not come back as only "timed out".
+#[cfg(not(windows))]
+#[tokio::test]
+async fn the_tools_deadline_keeps_what_the_command_printed() {
+ let tool = ShellTool::new(
+ test_security(AutonomyLevel::Full),
+ test_runtime(),
+ test_audit(),
+ );
+ let result = tool
+ .execute(json!({
+ "command": "echo 'FAIL: case 3'; echo 'warning: slow' >&2; sleep 5",
+ "timeout_secs": 1
+ }))
+ .await
+ .unwrap();
+ assert!(result.is_error);
+ let out = result.output();
+ assert!(
+ out.starts_with("Command timed out after 1s"),
+ "the timeout line stays first: {out}"
+ );
+ assert!(out.contains("[stdout]\nFAIL: case 3"), "{out}");
+ assert!(out.contains("[stderr]\nwarning: slow"), "{out}");
+}
+
+#[test]
+fn a_timed_out_result_carries_both_streams_after_the_timeout_line() {
+ let result = exec_result(
+ Ok(crate::sandbox::SandboxExecResult {
+ exit_code: -1,
+ stdout: "tested 40 positions\nFAIL: e1d2\n".to_owned(),
+ stderr: "warning: slow\n".to_owned(),
+ timed_out: true,
+ }),
+ Duration::from_secs(1331),
+ );
+ assert!(result.is_error);
+ assert_eq!(
+ result.output(),
+ "Command timed out after 1331s and was killed: the shell tool's timeout_secs limit fired. \
+ Output before the kill:\n[stdout]\ntested 40 positions\nFAIL: e1d2\n[stderr]\nwarning: slow"
+ );
+}
+
+#[test]
+fn a_kill_with_no_output_says_why_output_can_be_missing() {
+ let silent = exec_result(
+ Ok(crate::sandbox::SandboxExecResult {
+ exit_code: -1,
+ stdout: String::new(),
+ stderr: "\n".to_owned(),
+ timed_out: true,
+ }),
+ Duration::from_secs(5),
+ );
+ let out = silent.output();
+ assert!(
+ out.starts_with("Command timed out after 5s and was killed"),
+ "{out}"
+ );
+ assert!(
+ out.contains("No output reached the tool before the kill") && out.contains("`| tail`"),
+ "{out}"
+ );
+}