Skip to content

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

fix(sandbox): preserve shell output when the native jail exits early

Steven Enamakel4d4c6e23373e
diff --git a/crates/openhuman-core/src/cron/scheduler/shell_job.rs b/crates/openhuman-core/src/cron/scheduler/shell_job.rs
index 58a67c5ec2..592adb6870 100644
--- a/crates/openhuman-core/src/cron/scheduler/shell_job.rs
+++ b/crates/openhuman-core/src/cron/scheduler/shell_job.rs
@@ -174,6 +174,7 @@ pub(super) async fn run_job_command_with_timeout(
std::collections::BTreeMap::new(),
timeout,
true,
+ None,
)
.await
{
diff --git a/crates/openhuman-core/src/sandbox/native_jail.rs b/crates/openhuman-core/src/sandbox/native_jail.rs
index c50dae568e..5bf430f13b 100644
--- a/crates/openhuman-core/src/sandbox/native_jail.rs
+++ b/crates/openhuman-core/src/sandbox/native_jail.rs
@@ -52,6 +52,7 @@ pub(crate) async fn execute(
extra_env: &HashMap<OsString, OsString>,
timeout: Duration,
explicitly_requested: bool,
+ meter: Option<super::shell_progress::ShellOutputMeter>,
) -> anyhow::Result<SandboxExecResult> {
#[cfg(feature = "modules")]
{
@@ -70,6 +71,7 @@ pub(crate) async fn execute(
&extra_env,
timeout,
explicitly_requested,
+ meter,
)
.await
})
@@ -86,6 +88,7 @@ pub(crate) async fn execute(
extra_env,
timeout,
explicitly_requested,
+ meter,
);
anyhow::bail!("TinyBox module support is disabled; native jail execution is unavailable")
}
@@ -100,6 +103,7 @@ async fn execute_owned(
extra_env: &HashMap<OsString, OsString>,
timeout: Duration,
explicitly_requested: bool,
+ meter: Option<super::shell_progress::ShellOutputMeter>,
) -> anyhow::Result<SandboxExecResult> {
let status = tinybox::jail_status(config)
.await
@@ -116,6 +120,7 @@ async fn execute_owned(
run.workspace = policy.workspace_root.clone();
run.env = approved_environment(extra_env)?;
run.isolate_network = true;
+ run.meter = meter;
// TinyBox adds its own per-call scratch directory to the writable grants and
// points TMPDIR, TMP and TEMP at it unless the caller overrides them.
run.jail = Some(jail_request(policy)?);
diff --git a/crates/openhuman-core/src/sandbox/native_jail_tests.rs b/crates/openhuman-core/src/sandbox/native_jail_tests.rs
index 7de2ba4a02..56d4771c4d 100644
--- a/crates/openhuman-core/src/sandbox/native_jail_tests.rs
+++ b/crates/openhuman-core/src/sandbox/native_jail_tests.rs
@@ -243,6 +243,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
BTreeMap::new(),
Duration::from_secs(10),
true,
+ None,
)
.await?;
assert!(output.success());
@@ -257,6 +258,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
BTreeMap::new(),
Duration::from_secs(10),
true,
+ None,
)
.await?;
assert!(output.success(), "cron preserves default pipeline status");
@@ -269,6 +271,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
BTreeMap::new(),
Duration::from_millis(50),
true,
+ None,
)
.await?;
assert!(output.timed_out, "cron timeout is acknowledged by TinyBox");
diff --git a/crates/openhuman-core/src/sandbox/shell_exec.rs b/crates/openhuman-core/src/sandbox/shell_exec.rs
index e2d27c37c4..f83b792ff8 100644
--- a/crates/openhuman-core/src/sandbox/shell_exec.rs
+++ b/crates/openhuman-core/src/sandbox/shell_exec.rs
@@ -42,6 +42,8 @@ pub(crate) struct NativeShellRun {
pub(crate) hide_window: bool,
/// Whether the resource keeps default network isolation; `false` is open.
pub(crate) isolate_network: bool,
+ /// Receives the command's output as it streams, for progress reporting.
+ pub(crate) meter: Option<super::shell_progress::ShellOutputMeter>,
/// Native confinement; `None` is ordinary local execution.
#[cfg(feature = "modules")]
pub(crate) jail: Option<contract::NativeJailConfig>,
@@ -66,6 +68,7 @@ impl NativeShellRun {
python_utf8: false,
hide_window: false,
isolate_network: false,
+ meter: None,
#[cfg(feature = "modules")]
jail: None,
}
@@ -94,7 +97,7 @@ pub(crate) static USE_FIXTURE: std::sync::atomic::AtomicBool =
/// Run `run` through the module and collect its output.
///
/// A worker owns cancellation and resource cleanup if the caller drops its wait.
-pub(crate) async fn run(config: &Config, run: NativeShellRun) -> anyhow::Result<SandboxExecResult> {
+pub(crate) async fn run(config: &Config, mut run: NativeShellRun) -> anyhow::Result<SandboxExecResult> {
#[cfg(test)]
if !run.is_jailed() && USE_FIXTURE.load(std::sync::atomic::Ordering::SeqCst) {
let _ = config;
@@ -103,8 +106,8 @@ pub(crate) async fn run(config: &Config, run: NativeShellRun) -> anyhow::Result<
#[cfg(feature = "modules")]
{
let config = config.clone();
- // Read before the spawn: the caller's progress meter is task-local.
- let meter = super::shell_progress::current_meter();
+ // Taken before the spawn: the worker task owns the meter.
+ let meter = run.meter.take();
tracing::debug!(
jailed = run.jail.is_some(),
timeout_ms = run.timeout.as_millis() as u64,
@@ -321,8 +324,10 @@ pub(crate) async fn run_host(
env: BTreeMap<String, String>,
timeout: Duration,
preserve_pipeline_status: bool,
+ meter: Option<super::shell_progress::ShellOutputMeter>,
) -> anyhow::Result<SandboxExecResult> {
let mut shell = NativeShellRun::new(command, cwd, state_dir, timeout);
+ shell.meter = meter;
shell.env = env;
shell.preserve_pipeline_status = preserve_pipeline_status;
run(config, shell).await
diff --git a/crates/openhuman-core/src/sandbox/shell_progress.rs b/crates/openhuman-core/src/sandbox/shell_progress.rs
index 63aed175a2..31108541ec 100644
--- a/crates/openhuman-core/src/sandbox/shell_progress.rs
+++ b/crates/openhuman-core/src/sandbox/shell_progress.rs
@@ -77,17 +77,6 @@ impl ShellOutputMeter {
}
}
-tokio::task_local! {
- static CURRENT_METER: ShellOutputMeter;
-}
-
-/// The meter of the shell call running on this task, if it reports progress.
-/// Read it before spawning a worker task: a task-local does not follow
-/// `tokio::spawn`.
-pub(crate) fn current_meter() -> Option<ShellOutputMeter> {
- CURRENT_METER.try_with(Clone::clone).ok()
-}
-
/// The status line for a call `elapsed` into its run.
pub(crate) fn progress_message(elapsed: Duration, bytes: u64, last_line: Option<&str>) -> String {
let secs = elapsed.as_secs();
@@ -112,22 +101,29 @@ pub(crate) fn progress_message(elapsed: Duration, bytes: u64, last_line: Option<
}
}
-/// Run the shell call `fut`, reporting its progress through `context` every
-/// `interval` until it finishes. Without a context there is nowhere to report,
-/// so `fut` runs as is.
-pub(crate) async fn with_shell_progress<F: std::future::Future>(
+/// Run the shell call built by `build`, reporting its progress through
+/// `context` every `interval` until it finishes. `build` receives the call's
+/// [`ShellOutputMeter`] and hands it down to the process runner, which feeds it;
+/// without a context there is nowhere to report, so it receives `None` and the
+/// call runs as is.
+pub(crate) async fn with_shell_progress<B, F>(
context: Option<&dyn ToolRunContext>,
interval: Duration,
- fut: F,
-) -> F::Output {
+ build: B,
+) -> F::Output
+where
+ B: FnOnce(Option<ShellOutputMeter>) -> F,
+ F: std::future::Future,
+{
let Some(context) = context else {
- return fut.await;
+ return build(None).await;
};
let meter = ShellOutputMeter::default();
+ let fut = build(Some(meter.clone()));
let started = tokio::time::Instant::now();
// Boxed: the shell call's future is large, and this wrapper would
// otherwise hold it inline a second time on the tool's stack.
- let mut work = Box::pin(CURRENT_METER.scope(meter.clone(), fut));
+ let mut work = Box::pin(fut);
let mut ticker = tokio::time::interval_at(tokio::time::Instant::now() + interval, interval);
loop {
tokio::select! {