Skip to content

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

refactor(sandbox): thread dispatch context into sandbox policy resolution

Steven Enamakel72e20092bcb8
diff --git a/crates/openhuman-core/src/sandbox/ops.rs b/crates/openhuman-core/src/sandbox/ops.rs
index 8a86cb58af..7f8285c1e6 100644
--- a/crates/openhuman-core/src/sandbox/ops.rs
+++ b/crates/openhuman-core/src/sandbox/ops.rs
@@ -55,7 +55,7 @@ pub async fn command_requires_sandbox(
// language tools whose agent definition does not request a sandbox.
return Ok(true);
}
- if sandbox_disabled_by_host() {
+ if sandbox_disabled_by_host(ctx) {
return Ok(false);
}
if matches!(sandbox_mode, Some(SandboxMode::Sandboxed)) {
@@ -90,13 +90,14 @@ pub(crate) async fn resolve_command_policy(
.await
.map_err(|error| format!("Cannot read sandbox configuration: {error}"))?;
let mut policy = resolve_sandbox_policy(
+ ctx,
SandboxMode::Sandboxed,
action_dir,
state_dir,
&config.runtime,
false,
);
- if !config_disables_sandbox(&config) && !sandbox_disabled_by_host() {
+ if !config_disables_sandbox(&config) && !sandbox_disabled_by_host(ctx) {
apply_requested_backend(&mut policy, &config.sandbox, &config.runtime, &config)
.await
.map_err(|error| error.to_string())?;
@@ -104,15 +105,14 @@ pub(crate) async fn resolve_command_policy(
Ok(policy)
}
-fn sandbox_disabled_by_host() -> bool {
- sandbox_off_value(std::env::var(SANDBOX_OFF_ENV).ok().as_deref()) || sandbox_component_off()
+fn sandbox_disabled_by_host(ctx: &DispatchCtx) -> bool {
+ sandbox_off_value(std::env::var(SANDBOX_OFF_ENV).ok().as_deref()) || sandbox_component_off(ctx)
}
-/// `[components] sandbox = false` in the ambient core context. With no context
-/// the component is on, since the set only narrows.
-pub(crate) fn sandbox_component_off() -> bool {
- let off = crate::core::runtime::context::CoreContext::current()
- .is_some_and(|ctx| !ctx.components().sandbox);
+/// `[components] sandbox = false` in the context `ctx` runs under. With no
+/// context the component is on, since the set only narrows.
+pub(crate) fn sandbox_component_off(ctx: &DispatchCtx) -> bool {
+ let off = !ctx.components().sandbox;
if off {
tracing::debug!("[sandbox] component 'sandbox' disabled, using the unsandboxed fallback");
}
@@ -136,6 +136,7 @@ fn config_disables_sandbox(config: &crate::config::Config) -> bool {
/// `state_dir` is the core's internal `workspace_dir`; host-side scratch such
/// as output capture goes there instead of into `action_dir`.
pub fn resolve_sandbox_policy(
+ ctx: &DispatchCtx,
mode: SandboxMode,
action_dir: &Path,
state_dir: &Path,
@@ -145,7 +146,7 @@ pub fn resolve_sandbox_policy(
let backend = match mode {
SandboxMode::None => SandboxBackendKind::None,
SandboxMode::ReadOnly => SandboxBackendKind::None,
- SandboxMode::Sandboxed if sandbox_disabled_by_host() => {
+ SandboxMode::Sandboxed if sandbox_disabled_by_host(ctx) => {
tracing::debug!(
env = SANDBOX_OFF_ENV,
"[sandbox] host disabled the sandbox; running sandboxed mode unconfined"
@@ -324,11 +325,11 @@ pub(crate) async fn execute_in_sandbox_metered(
// must be checked at the spawn boundary, including callers that resolved
// their policy from an older or default RuntimeConfig.
let mut effective_policy = policy.clone();
- if !crate::core::runtime::is_saas() && sandbox_disabled_by_host() {
+ if !crate::core::runtime::is_saas() && sandbox_disabled_by_host(ctx) {
effective_policy.backend = SandboxBackendKind::None;
}
let mut module_config = None;
- let explicitly_requested = if crate::core::runtime::is_saas() || sandbox_disabled_by_host() {
+ let explicitly_requested = if crate::core::runtime::is_saas() || sandbox_disabled_by_host(ctx) {
false
} else {
let config = ctx
diff --git a/crates/openhuman-core/src/sandbox/ops_tests.rs b/crates/openhuman-core/src/sandbox/ops_tests.rs
index 6fc9410542..43e45076d3 100644
--- a/crates/openhuman-core/src/sandbox/ops_tests.rs
+++ b/crates/openhuman-core/src/sandbox/ops_tests.rs
@@ -5,6 +5,7 @@ use crate::core::runtime::DispatchCtx;
#[test]
fn resolve_sandbox_policy_none_mode() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::None,
Path::new("/tmp/action"),
Path::new("/tmp/state"),
@@ -17,6 +18,7 @@ fn resolve_sandbox_policy_none_mode() {
#[test]
fn resolve_sandbox_policy_read_only_mode() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::ReadOnly,
Path::new("/tmp/action"),
Path::new("/tmp/state"),
@@ -29,6 +31,7 @@ fn resolve_sandbox_policy_read_only_mode() {
#[test]
fn resolve_sandbox_policy_sandboxed_local() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::Sandboxed,
Path::new("/tmp/action"),
Path::new("/tmp/state"),
@@ -42,6 +45,7 @@ fn resolve_sandbox_policy_sandboxed_local() {
#[test]
fn resolve_sandbox_policy_sandboxed_remote_uses_docker() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::Sandboxed,
Path::new("/tmp/action"),
Path::new("/tmp/state"),
@@ -60,6 +64,7 @@ fn resolve_sandbox_policy_docker_runtime_forces_docker() {
..RuntimeConfig::default()
};
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::Sandboxed,
Path::new("/tmp/action"),
Path::new("/tmp/state"),
@@ -89,6 +94,7 @@ fn build_elevated_op_creates_record() {
#[tokio::test]
async fn create_sandbox_backend_none() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::None,
Path::new("/tmp"),
Path::new("/tmp/state"),
@@ -103,6 +109,7 @@ async fn create_sandbox_backend_none() {
/// A `None`-backend policy whose state dir is a throwaway directory.
fn unsandboxed_policy(workspace: &Path, state: &Path) -> SandboxPolicy {
resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::None,
workspace,
state,
@@ -201,6 +208,7 @@ async fn execute_unsandboxed_echo_runs_on_every_os() {
#[tokio::test]
async fn execute_in_sandbox_none_backend() {
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::None,
Path::new("/tmp"),
Path::new("/tmp/state"),
@@ -277,6 +285,7 @@ async fn execute_unsandboxed_applies_environment_overrides() {
async fn execute_in_sandbox_none_backend_runs_on_every_os() {
let tempdir = tempfile::tempdir().unwrap();
let policy = resolve_sandbox_policy(
+ &DispatchCtx::unbound(),
SandboxMode::None,
tempdir.path(),
Path::new("/tmp/state"),
diff --git a/crates/openhuman-core/src/sandbox/schemas.rs b/crates/openhuman-core/src/sandbox/schemas.rs
index 4a62f20daa..b19d9ad850 100644
--- a/crates/openhuman-core/src/sandbox/schemas.rs
+++ b/crates/openhuman-core/src/sandbox/schemas.rs
@@ -9,9 +9,9 @@ use crate::core::Outcome;
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
/// Refuse with the typed `COMPONENT_DISABLED:sandbox` error while `[components]
-/// sandbox` is off. Read from the ambient core context; with none, it is on.
-fn gate() -> Result<(), String> {
- if super::ops::sandbox_component_off() {
+/// sandbox` is off for the context the call runs under; with none, it is on.
+fn gate(ctx: &DispatchCtx) -> Result<(), String> {
+ if super::ops::sandbox_component_off(ctx) {
return Err(crate::core::runtime::components::component_disabled("sandbox").encode());
}
Ok(())
@@ -170,7 +170,7 @@ pub fn schemas(function: &str) -> ControllerSchema {
/// backend instead was the conflation fixed in #6081.
fn handle_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
- gate()?;
+ gate(&ctx)?;
let backend_str = params
.get("backend")
.and_then(|v| v.as_str())
@@ -204,6 +204,7 @@ fn handle_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFutu
};
let mut policy = super::ops::resolve_sandbox_policy(
+ &ctx,
mode,
&config.workspace_dir,
&config.workspace_dir,
@@ -227,7 +228,7 @@ fn handle_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFutu
fn handle_resolve_policy(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
- gate()?;
+ gate(&ctx)?;
let mode_str = params
.get("sandbox_mode")
.and_then(|v| v.as_str())
@@ -264,6 +265,7 @@ fn handle_resolve_policy(ctx: DispatchCtx, params: Map<String, Value>) -> Contro
// set directly. Re-deriving it here from `action_dir_override` alone
// would silently ignore an embedder's programmatic `action_dir` (#6081).
let mut policy = super::ops::resolve_sandbox_policy(
+ &ctx,
mode,
&config.action_dir,
&config.workspace_dir,
@@ -286,7 +288,7 @@ fn handle_resolve_policy(ctx: DispatchCtx, params: Map<String, Value>) -> Contro
fn handle_cleanup_orphans(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
- gate()?;
+ gate(&ctx)?;
let config = match ctx.load_config().await {
Ok(config) => config,
Err(error) => return Err(error),
@@ -303,7 +305,7 @@ fn handle_cleanup_orphans(ctx: DispatchCtx, _params: Map<String, Value>) -> Cont
fn handle_validate_policy(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
- gate()?;
+ gate(&ctx)?;
let policy_val = params.get("policy").cloned().unwrap_or(Value::Null);
let policy: super::types::SandboxPolicy = match serde_json::from_value(policy_val) {
Ok(p) => p,
diff --git a/crates/openhuman-core/src/tools/impl/system/shell.rs b/crates/openhuman-core/src/tools/impl/system/shell.rs
index 84ceea7025..0ca2065db5 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell.rs
@@ -642,6 +642,7 @@ impl ShellTool {
) -> (bool, ToolResult) {
let config = crate::config::RuntimeConfig::default();
let policy = crate::sandbox::resolve_sandbox_policy(
+ ctx,
tinyagents_definition::SandboxMode::Sandboxed,
action_dir,
&self.security.workspace_dir,