From 1e7992900a278844359a7f1d578f2a335d33fc6b Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Thu, 5 Nov 2026 21:30:36 +0530 Subject: [PATCH 1/4] test(sandbox): planned Docker spawns map their CPU and memory bounds A Docker plan with the shipped [runtime.docker] bounds, a memory bound alone, or a CPU bound alone must map to a BoxPlan; a zero bound is still refused and a native CPU bound still is. On main every bounded Docker plan is refused (TS-E2E-002). --- .../openhuman-core/src/sandbox/plan_tests.rs | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/crates/openhuman-core/src/sandbox/plan_tests.rs b/crates/openhuman-core/src/sandbox/plan_tests.rs index 54e71133af7..f01fa373281 100644 --- a/crates/openhuman-core/src/sandbox/plan_tests.rs +++ b/crates/openhuman-core/src/sandbox/plan_tests.rs @@ -65,22 +65,55 @@ fn an_unbounded_plan_does_not_synthesize_native_resource_limits() { } #[test] -fn unsupported_planned_bounds_are_refused_instead_of_being_ignored() { - let mut planned = artifact(SandboxBackend::Docker); - planned.limits = ResourceLimits2 { - cpu_time_ms: None, - cpu_millicores: Some(500), - memory_bytes: Some(64 << 20), - pids_max: None, - disk_bytes: None, - }; - planned.constraints.push(SandboxConstraint2::Subprocesses); +fn docker_cpu_and_memory_bounds_are_accepted() { + // TS-E2E-002: PlannedSpawn::apply carries both into the Docker overrides and + // RunDocker enforces them, so refusing them refused every bounded container. + for limits in [ + ResourceLimits2 { + cpu_millicores: Some(1000), + memory_bytes: Some(512 << 20), + ..ResourceLimits2::default() + }, + ResourceLimits2 { + memory_bytes: Some(512 << 20), + ..ResourceLimits2::default() + }, + ResourceLimits2 { + cpu_millicores: Some(500), + ..ResourceLimits2::default() + }, + ] { + let mut planned = artifact(SandboxBackend::Docker); + planned.limits = limits; + planned.constraints.push(SandboxConstraint2::Subprocesses); + assert!( + BoxPlan::from_artifact(&planned, None).is_ok(), + "a Docker plan with {:?} must map", + planned.limits + ); + } +} + +#[test] +fn native_cpu_bounds_are_refused_instead_of_being_ignored() { + let mut planned = artifact(SandboxBackend::Jail); + planned.limits.cpu_millicores = Some(500); assert_eq!( BoxPlan::from_artifact(&planned, None), Err(PlanMappingError::CpuLimitUnenforceable) ); } +#[test] +fn a_zero_docker_bound_is_refused() { + let mut planned = artifact(SandboxBackend::Docker); + planned.limits.cpu_millicores = Some(0); + assert_eq!( + BoxPlan::from_artifact(&planned, None), + Err(PlanMappingError::ZeroLimit) + ); +} + #[test] fn native_memory_limits_are_refused_instead_of_being_dropped() { let mut planned = artifact(SandboxBackend::Jail); From 3aa1ac215243dcfa06188865d09327f9e85db7fc Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Thu, 5 Nov 2026 21:30:36 +0530 Subject: [PATCH 2/4] fix(sandbox): accept CPU and memory bounds for planned Docker spawns validate_resources refused any CPU bound for every executor and a memory bound for Docker, so every planned Docker spawn carrying the [runtime.docker] defaults (1.0 CPU, 512 MB) was refused: local backend = "docker" and SaaS host_shell never ran a planned command. The container enforces both: PlannedSpawn::apply writes them into the Docker overrides and TinyBox's RunDocker passes them as --cpus and -m. Docker now accepts CPU and memory bounds. It still refuses a PID limit (RunDocker has none), and every bound stays refused for the host and the native jail. A zero bound is checked first, for every executor. Fixes TS-E2E-002. --- crates/openhuman-core/src/sandbox/plan.rs | 43 +++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/crates/openhuman-core/src/sandbox/plan.rs b/crates/openhuman-core/src/sandbox/plan.rs index 31b9bd3364c..4129f1f04ec 100644 --- a/crates/openhuman-core/src/sandbox/plan.rs +++ b/crates/openhuman-core/src/sandbox/plan.rs @@ -250,33 +250,40 @@ fn validate_resources( executor: PlannedExecutor, ) -> Result<(), PlanMappingError> { let limits = &artifact.limits; + if limits.cpu_millicores == Some(0) + || limits.memory_bytes == Some(0) + || limits.pids_max == Some(0) + { + return Err(PlanMappingError::ZeroLimit); + } if limits.cpu_time_ms.is_some() { return Err(PlanMappingError::CpuTimeUnenforceable); } - if limits.cpu_millicores.is_some() { - return Err(if executor == PlannedExecutor::Host { - PlanMappingError::HostResourceLimitUnenforceable - } else { - PlanMappingError::CpuLimitUnenforceable - }); - } if limits.disk_bytes.is_some() { return Err(PlanMappingError::DiskLimitUnenforceable); } - if limits.cpu_millicores == Some(0) - || limits.memory_bytes == Some(0) - || limits.pids_max == Some(0) + if executor == PlannedExecutor::Docker { + // A container enforces CPU and memory bounds: `PlannedSpawn::apply` + // writes them into the Docker overrides, and TinyBox's RunDocker passes + // them as `--cpus` and `-m`. That request has no PID limit. + return if limits.pids_max.is_some() { + Err(PlanMappingError::PidsLimitUnenforceable) + } else { + Ok(()) + }; + } + if executor == PlannedExecutor::Host + && (limits.cpu_millicores.is_some() + || limits.memory_bytes.is_some() + || limits.pids_max.is_some()) { - return Err(PlanMappingError::ZeroLimit); + return Err(PlanMappingError::HostResourceLimitUnenforceable); + } + if limits.cpu_millicores.is_some() { + return Err(PlanMappingError::CpuLimitUnenforceable); } if limits.memory_bytes.is_some() || limits.pids_max.is_some() { - return Err(match executor { - PlannedExecutor::Host => PlanMappingError::HostResourceLimitUnenforceable, - PlannedExecutor::Docker if limits.pids_max.is_some() => { - PlanMappingError::PidsLimitUnenforceable - } - _ => PlanMappingError::NativeResourceLimitsUnenforceable, - }); + return Err(PlanMappingError::NativeResourceLimitsUnenforceable); } Ok(()) } From e066217087d8f0ed99255ced5632d3c5271da023 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 6 Nov 2026 11:48:10 +0530 Subject: [PATCH 3/4] test(sandbox): a fractional Docker CPU bound must not widen A CPU bound is a ceiling: 0.1004 cores must plan as 100 millicores, not 101 (which PlannedSpawn::apply writes back as 0.101), and a bound below one millicore plans as 0, which the plan refuses. --- .../openhuman-core/src/sandbox/spawn_plan/mod_tests.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs b/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs index 22061175c55..6c03cb2eef5 100644 --- a/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs +++ b/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs @@ -37,10 +37,16 @@ fn bounds_come_from_the_docker_overrides_and_never_loosen() { assert_eq!(bounded.cpu_millicores, Some(1500)); assert_eq!(bounded.memory_bytes, Some(512 * 1024 * 1024)); assert_eq!(bounded.cpu_time_ms, None); - // A fraction rounds up to the next millicore rather than down to a looser bound. + // A fraction rounds down: a CPU bound is a ceiling, so rounding up would + // widen it (0.1004 cores must not run as 0.101). + assert_eq!( + limits(&with_overrides(Some(0.1004), None)).cpu_millicores, + Some(100) + ); + // Below one millicore the bound is 0, which the plan refuses, not widens. assert_eq!( limits(&with_overrides(Some(0.0004), None)).cpu_millicores, - Some(1) + Some(0) ); // Absent, zero and non-finite bounds are not bounds. for cpu in [ From 14dcb8ca0e32bc2029b6b0b3e53514db1bef9adf Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 6 Nov 2026 11:48:10 +0530 Subject: [PATCH 4/4] fix(sandbox): round a fractional Docker CPU bound down, not up limits() rounded cpu_limit up to the next millicore on the belief that this tightened the bound. For a CPU ceiling it widens it, and since #8571 accepts Docker CPU bounds the widened value would run. Rounding down never exceeds the operator's ceiling; below one millicore the bound is 0 and the plan refuses it (ZeroLimit). --- crates/openhuman-core/src/sandbox/spawn_plan/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs b/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs index 04b86b2fd99..fa49456e3cb 100644 --- a/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs +++ b/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs @@ -164,12 +164,14 @@ fn limits(policy: &SandboxPolicy) -> ResourceLimits2 { }; ResourceLimits2 { cpu_time_ms: None, - // Fractional CPUs become thousandths of a core, rounded up so a bound is - // never silently loosened; absent bounds stay absent. + // Fractional CPUs become thousandths of a core, rounded down: a CPU bound + // is a ceiling, so rounding up would let the container use more than the + // operator allowed. Below one millicore this is 0, which the plan refuses + // (`ZeroLimit`) rather than widening. Absent bounds stay absent. cpu_millicores: overrides .cpu_limit .filter(|cpus| cpus.is_finite() && *cpus > 0.0) - .map(|cpus| (cpus * 1000.0).ceil().min(f64::from(u32::MAX)) as u32), + .map(|cpus| (cpus * 1000.0).floor().min(f64::from(u32::MAX)) as u32), memory_bytes: overrides .memory_limit_mb .filter(|mb| *mb > 0)