Skip to content

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

refactor(tinybox): move host runtime into the tinybox module

Steven Enamakelc8692fbe8589
diff --git a/crates/openhuman-core/src/agent/session_host/builder/factory.rs b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
index 45e5922a6b..0ccb4bf7a6 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/factory.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
@@ -8,7 +8,7 @@ use crate::agent_registry::host_definition::{
};
use crate::config::Config;
use crate::core::runtime::DispatchCtx;
-use crate::sandbox::host_runtime;
+use crate::modules::tinybox::host_runtime;
use crate::security::SecurityPolicy;
use crate::tools;
use anyhow::Result;
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
index d3960b7aad..f4348ea34f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/failure_policy.rs
@@ -100,7 +100,7 @@ const ALWAYS_RETRYABLE_ON_TIMEOUT: &[&str] = &[
/// The shell agent commands run under on this host, for a nudge that has to
/// say which commands exist.
fn host_shell_description() -> String {
- let shell = match crate::sandbox::host_runtime::host_shell_flavor() {
+ let shell = match crate::modules::tinybox::host_runtime::host_shell_flavor() {
tinybox_bus::ShellFlavor::Cmd => "cmd.exe",
tinybox_bus::ShellFlavor::Posix => "bash or sh",
};
diff --git a/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs b/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
index f5a2bbfb3d..0266f0d716 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/turn_parts.rs
@@ -13,7 +13,7 @@ use crate::channels::system_prompt::{ChannelPromptInputs, ChannelSystemPrompt};
use crate::config::Config;
use crate::core::runtime::DispatchCtx;
use crate::inference::provider;
-use crate::sandbox::host_runtime;
+use crate::modules::tinybox::host_runtime;
use crate::security::SecurityPolicy;
use crate::tools;
use anyhow::Result;
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index bbac80e115..5d989e2558 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -121,7 +121,6 @@ mod storage_import_tinyjuice;
mod storage_import_tinymcp;
#[cfg(all(feature = "modules", feature = "web3"))]
mod storage_import_tinywallet;
-#[cfg(feature = "modules")]
pub mod tinybox;
#[cfg(feature = "modules")]
mod tokenjuice_host;
diff --git a/crates/openhuman-core/src/modules/tinybox.rs b/crates/openhuman-core/src/modules/tinybox.rs
index 547e6c882f..ba6a523f21 100644
--- a/crates/openhuman-core/src/modules/tinybox.rs
+++ b/crates/openhuman-core/src/modules/tinybox.rs
@@ -1,481 +1,14 @@
-//! Host calls to the TinyBox sandbox module.
+//! OpenHuman's host adapter for the TinyBox module.
//!
-//! This adapter owns only OpenHuman's module loading and bus boundary. Request
-//! and response types come from `tinybox-bus`; isolation, process ownership,
-//! file access, forwarding, and output collection stay in TinyBox.
+//! The host runtime policy is available in every build. The bus-facing
+//! operations remain behind the `modules` feature.
-use std::time::Duration;
+#[path = "tinybox/host_runtime.rs"]
+pub mod host_runtime;
-use serde::{de::DeserializeOwned, Serialize};
-pub use tinybox_bus as contract;
+#[cfg(feature = "modules")]
+#[path = "tinybox/operations.rs"]
+mod operations;
-
-use crate::config::Config;
-
-use tinybus::module::{ModuleCallError, ModuleClient};
-
-/// Registry id for the TinyBox module.
-pub const MODULE_ID: &str = "tinybox";
-
-const MODULE_CALL_TIMEOUT: Duration = Duration::from_secs(300);
-
-// `tinybox_bus::names` carries one named constant per bus member; the host
-// never indexes `METHODS` positionally nor keeps a second set of wire strings.
-const DESCRIBE: &str = contract::names::DESCRIBE;
-const CREATE: &str = contract::names::CREATE;
-const EXEC: &str = contract::names::EXEC;
-const INSPECT: &str = contract::names::INSPECT;
-const CLOSE: &str = contract::names::CLOSE;
-const SPAWN: &str = contract::names::SPAWN;
-const IS_RUNNING: &str = contract::names::IS_RUNNING;
-const CANCEL: &str = contract::names::CANCEL;
-const ANALYZE_SHELL: &str = contract::names::ANALYZE_SHELL;
-const RESERVE: &str = contract::names::RESERVE;
-const CAPABILITIES: &str = contract::names::CAPABILITIES;
-const SHUTDOWN: &str = contract::names::SHUTDOWN;
-const FORWARD: &str = contract::names::FORWARD;
-const CLOSE_FORWARD: &str = contract::names::CLOSE_FORWARD;
-const JAIL_STATUS: &str = contract::names::JAIL_STATUS;
-const ANALYZE_COMMAND: &str = contract::names::ANALYZE_COMMAND;
-const BEGIN_FILE_READ: &str = contract::names::BEGIN_FILE_READ;
-const READ_FILE_CHUNK: &str = contract::names::READ_FILE_CHUNK;
-const FINISH_FILE_READ: &str = contract::names::FINISH_FILE_READ;
-const BEGIN_FILE_WRITE: &str = contract::names::BEGIN_FILE_WRITE;
-const WRITE_FILE_CHUNK: &str = contract::names::WRITE_FILE_CHUNK;
-const FINISH_FILE_WRITE: &str = contract::names::FINISH_FILE_WRITE;
-const ABORT_FILE_WRITE: &str = contract::names::ABORT_FILE_WRITE;
-const START_EXEC: &str = contract::names::START_EXEC;
-const READ_OUTPUT: &str = contract::names::READ_OUTPUT;
-const RELEASE_OUTPUT: &str = contract::names::RELEASE_OUTPUT;
-const START_JAILED_EXEC: &str = contract::names::START_JAILED_EXEC;
-const RUN_DOCKER: &str = contract::names::RUN_DOCKER;
-const DOCKER_STATUS: &str = contract::names::DOCKER_STATUS;
-const CLEANUP_DOCKER: &str = contract::names::CLEANUP_DOCKER;
-const RUN_DOCKER_WITHOUT_WORKSPACE: &str = contract::names::RUN_DOCKER_WITHOUT_WORKSPACE;
-const SELECT_SHELL: &str = contract::names::SELECT_SHELL;
-const RESOLVE_LOCAL_GRANTS: &str = contract::names::RESOLVE_LOCAL_GRANTS;
-const START_NATIVE_SHELL_EXEC: &str = contract::names::START_NATIVE_SHELL_EXEC;
-const VALIDATE_DOCKER_POLICY: &str = contract::names::VALIDATE_DOCKER_POLICY;
-const DOCKER_CALL_GRACE: Duration = Duration::from_secs(5);
-
-#[cfg(test)]
-#[path = "tinybox_tests.rs"]
-mod tests;
-
-/// Resolve TinyBox, require the bus contract supported by this host, and
-/// return its proxy.
-///
-/// The capability check is deliberate: the registry artifact may be older
-/// than this compiled host while a release is being rolled out. A package
-/// version alone does not prove which additive bus members it serves.
-///
-/// # Errors
-///
-/// Returns a safe loading, transport, or contract-version error. It does not
-/// include request arguments or filesystem paths.
-fn require_compatible(version: (u32, u32)) -> Result<(), ModuleCallError> {
- if contract::is_compatible(version) {
- return Ok(());
- }
- crate::modules::report::incompatible_contract(MODULE_ID);
- Err(ModuleCallError::IncompatibleContract)
-}
-
-/// Describe the loaded TinyBox module.
-pub async fn describe(config: &Config) -> Result<String, String> {
- call(config, DESCRIBE, (), false).await
-}
-
-/// Read module capabilities after enforcing contract compatibility.
-pub async fn capabilities(config: &Config) -> Result<contract::ModuleCapabilities, String> {
- let client = module_client(config);
- let capabilities: contract::ModuleCapabilities = client
- .call(MODULE_ID, CAPABILITIES, ())
- .await
- .map_err(message)?;
- require_compatible(capabilities.contract_version).map_err(message)?;
- Ok(capabilities)
-}
-
-/// Read the native jail facts; the caller applies its own policy to them.
-pub async fn jail_status(config: &Config) -> Result<contract::JailStatus, String> {
- call(config, JAIL_STATUS, (), false).await
-}
-
-/// Reserve an opaque identifier before native startup.
-pub async fn reserve(
- config: &Config,
- request: contract::ReserveRequest,
-) -> Result<contract::ResourceId, String> {
- call(config, RESERVE, (request,), false).await
-}
-
-/// Create a sandbox using its caller-known reservation.
-pub async fn create(
- config: &Config,
- request: contract::CreateRequest,
-) -> Result<contract::ResourceInfo, String> {
- call(config, CREATE, (request,), true).await
-}
-
-/// Execute a collected command in a resource.
-pub async fn exec(
- config: &Config,
- request: contract::ExecRequest,
-) -> Result<contract::ExecOutput, String> {
- call(config, EXEC, (request,), true).await
-}
-
-/// Inspect a module-owned resource.
-pub async fn inspect(
- config: &Config,
- resource: contract::ResourceId,
-) -> Result<contract::ResourceInfo, String> {
- call(config, INSPECT, (resource,), false).await
-}
-
-/// Close a resource and wait for the module's native cleanup acknowledgement.
-pub async fn close(config: &Config, resource: contract::ResourceId) -> Result<(), String> {
- call(config, CLOSE, (resource,), false).await
-}
-
-/// Shut down the module-owned resources.
-pub async fn shutdown(config: &Config) -> Result<(), String> {
- call(config, SHUTDOWN, (), false).await
-}
-
-/// Start a detached process using a pre-reserved process id.
-pub async fn spawn(
- config: &Config,
- request: contract::SpawnRequest,
-) -> Result<contract::ProcessRef, String> {
- call(config, SPAWN, (request,), true).await
-}
-
-/// Start a supervised process with live output collection.
-pub async fn start_exec(
- config: &Config,
- request: contract::SpawnRequest,
-) -> Result<contract::ProcessRef, String> {
- call(config, START_EXEC, (request,), true).await
-}
-
-/// Start a process in the explicitly requested native directory jail.
-pub async fn start_jailed_exec(
- config: &Config,
- request: contract::SpawnRequest,
- jail: contract::NativeJailConfig,
-) -> Result<contract::ProcessRef, String> {
- call(config, START_JAILED_EXEC, (request, jail), true).await
-}
-
-/// Ask TinyBox which shell it would spawn for a command on its machine.
-///
-/// The reply is discovery only: the host never spawns the plan itself, it
-/// executes through [`start_native_shell_exec`].
-pub async fn select_shell(
- config: &Config,
- request: contract::SelectShellRequest,
-) -> Result<contract::ShellInvocation, String> {
- call(config, SELECT_SHELL, (request,), false).await
-}
-
-/// Start a local shell under module-owned supervision and output replay.
-///
-/// The caller has already authorized the command; the module owns shell
-/// selection, the safe child environment and cleanup.
-pub async fn start_native_shell_exec(
- config: &Config,
- request: contract::ShellExecRequest,
-) -> Result<contract::ProcessRef, String> {
- call(config, START_NATIVE_SHELL_EXEC, (request,), true).await
-}
-
-/// Ask TinyBox whether a Docker run would weaken isolation, without starting
-/// one. This is the same check `RunDocker` enforces before creating a container.
-pub async fn validate_docker_policy(
- config: &Config,
- request: contract::DockerPolicyRequest,
-) -> Result<contract::DockerPolicyReport, String> {
- call(config, VALIDATE_DOCKER_POLICY, (request,), false).await
-}
-
-/// Ask TinyBox to resolve the existing toolchain and configured grants for the
-/// local jail.
-///
-/// The request carries the host's protected-path floor as input; the reply is
-/// a candidate set the host must still pass through its own floor.
-pub async fn resolve_local_grants(
- config: &Config,
- request: contract::ResolveLocalGrantsRequest,
-) -> Result<contract::LocalGrants, String> {
- call(config, RESOLVE_LOCAL_GRANTS, (request,), false).await
-}
-
-/// Run one bounded Docker-isolated command through TinyBox.
-///
-/// The caller applies authorization and Docker policy before sending this
-/// confidential request. The bus deadline includes a short reply grace after
-/// TinyBox's own command deadline so the timeout result can be delivered.
-pub async fn run_docker(
- config: &Config,
- request: contract::DockerRunRequest,
-) -> Result<contract::DockerRunOutput, String> {
- let timeout = Duration::from_millis(request.timeout_ms).saturating_add(DOCKER_CALL_GRACE);
- let client = crate::modules::client(config.clone()).with_timeout(timeout);
- call_run_docker_with_client(&client, request).await
-}
-
-/// Run one bounded Docker-isolated command without mounting a workspace.
-///
-/// This is used by the configured Docker runtime, whose existing behavior
-/// intentionally does not expose host workspace files to the container.
-pub async fn run_docker_without_workspace(
- config: &Config,
- request: contract::DockerRunRequest,
-) -> Result<contract::DockerRunOutput, String> {
- let timeout = Duration::from_millis(request.timeout_ms).saturating_add(DOCKER_CALL_GRACE);
- let client = crate::modules::client(config.clone()).with_timeout(timeout);
- call_run_docker_without_workspace_with_client(&client, request).await
-}
-
-/// Probe the Docker daemon owned by the TinyBox module host.
-pub async fn docker_status(
- config: &Config,
- request: contract::DockerStatusRequest,
-) -> Result<contract::DockerStatus, String> {
- call_docker_status_with_client(&module_client(config), request).await
-}
-
-/// Stop TinyBox-owned containers matching the supplied label.
-pub async fn cleanup_docker(
- config: &Config,
- request: contract::CleanupDockerRequest,
-) -> Result<contract::CleanupDockerOutput, String> {
- call_cleanup_docker_with_client(&module_client(config), request).await
-}
-
-fn docker_control_deadline(timeout_ms: u64, grace: Duration) -> Duration {
- Duration::from_millis(timeout_ms.min(contract::MAX_DOCKER_CONTROL_TIMEOUT_MS))
- .saturating_add(grace)
-}
-
-fn docker_control_client(client: &ModuleClient, timeout_ms: u64, grace: Duration) -> ModuleClient {
- client
- .clone()
- .with_timeout(docker_control_deadline(timeout_ms, grace))
-}
-
-async fn call_docker_status_with_client(
- client: &ModuleClient,
- request: contract::DockerStatusRequest,
-) -> Result<contract::DockerStatus, String> {
- call_docker_status_with_grace(client, request, DOCKER_CALL_GRACE).await
-}
-
-async fn call_docker_status_with_grace(
- client: &ModuleClient,
- request: contract::DockerStatusRequest,
- grace: Duration,
-) -> Result<contract::DockerStatus, String> {
- let client = docker_control_client(client, request.timeout_ms, grace);
- call_with_client(&client, DOCKER_STATUS, (request,), false).await
-}
-
-async fn call_cleanup_docker_with_client(
- client: &ModuleClient,
- request: contract::CleanupDockerRequest,
-) -> Result<contract::CleanupDockerOutput, String> {
- call_cleanup_docker_with_grace(client, request, DOCKER_CALL_GRACE).await
-}
-
-async fn call_cleanup_docker_with_grace(
- client: &ModuleClient,
- request: contract::CleanupDockerRequest,
- grace: Duration,
-) -> Result<contract::CleanupDockerOutput, String> {
- let client = docker_control_client(client, request.timeout_ms, grace);
- call_with_client(&client, CLEANUP_DOCKER, (request,), false).await
-}
-
-async fn call_run_docker_with_client(
- client: &ModuleClient,
- request: contract::DockerRunRequest,
-) -> Result<contract::DockerRunOutput, String> {
- call_with_client(client, RUN_DOCKER, (request,), true).await
-}
-
-async fn call_run_docker_without_workspace_with_client(
- client: &ModuleClient,
- request: contract::DockerRunRequest,
-) -> Result<contract::DockerRunOutput, String> {
- call_with_client(client, RUN_DOCKER_WITHOUT_WORKSPACE, (request,), true).await
-}
-
-/// Read a bounded output batch from a live or completed process.
-pub async fn read_output(
- config: &Config,
- process: contract::ProcessRef,
- cursor: u64,
-) -> Result<contract::OutputBatch, String> {
- call(config, READ_OUTPUT, (process, cursor), false).await
-}
-
-/// Release retained output and process ownership after terminal observation.
-pub async fn release_output(config: &Config, process: contract::ProcessRef) -> Result<(), String> {
- call(config, RELEASE_OUTPUT, (process,), false).await
-}
-
-/// Check whether a detached process remains active.
-pub async fn is_running(config: &Config, process: contract::ProcessRef) -> Result<bool, String> {
- call(config, IS_RUNNING, (process,), false).await
-}
-
-/// Cancel a detached process and wait for acknowledged cleanup.
-pub async fn cancel(config: &Config, process: contract::ProcessRef) -> Result<(), String> {
- call(config, CANCEL, (process,), false).await
-}
-
-/// Open a module-owned port forward.
-pub async fn forward(
- config: &Config,
- request: contract::ForwardRequest,
-) -> Result<contract::ForwardInfo, String> {
- call(config, FORWARD, (request,), false).await
-}
-
-/// Close a module-owned port forward.
-pub async fn close_forward(
- config: &Config,
- request: contract::CloseForwardRequest,
-) -> Result<(), String> {
- call(config, CLOSE_FORWARD, (request,), false).await
-}
-
-/// Ask TinyBox for structural shell facts; this is not an authorization check.
-pub async fn analyze_shell(
- config: &Config,
- command: String,
-) -> Result<contract::ShellAnalysis, String> {
- call(config, ANALYZE_SHELL, (command,), false).await
-}
-
-/// Ask TinyBox for generic command facts; the caller still decides policy.
-pub async fn analyze_command(
- config: &Config,
- command: String,
-) -> Result<contract::CommandAnalysis, String> {
- call(config, ANALYZE_COMMAND, (command,), false).await
-}
-
-/// Open a bounded workspace file reader.
-pub async fn begin_file_read(
- config: &Config,
- request: contract::BeginFileReadRequest,
-) -> Result<contract::FileReadInfo, String> {
- call(config, BEGIN_FILE_READ, (request,), false).await
-}
-
-/// Read one bounded range from an open file reader.
-pub async fn read_file_chunk(
- config: &Config,
- request: contract::ReadFileChunkRequest,
-) -> Result<contract::FileChunk, String> {
- call(config, READ_FILE_CHUNK, (request,), false).await
-}
-
-/// Close a workspace file reader.
-pub async fn finish_file_read(
- config: &Config,
- request: contract::FinishFileReadRequest,
-) -> Result<(), String> {
- call(config, FINISH_FILE_READ, (request,), false).await
-}
-
-/// Open an atomic staged workspace file writer.
-pub async fn begin_file_write(
- config: &Config,
- request: contract::BeginFileWriteRequest,
-) -> Result<contract::FileWriteInfo, String> {
- call(config, BEGIN_FILE_WRITE, (request,), false).await
-}
-
-/// Write one bounded sequential chunk.
-pub async fn write_file_chunk(
- config: &Config,
- request: contract::WriteFileChunkRequest,
-) -> Result<contract::FileWriteProgress, String> {
- call(config, WRITE_FILE_CHUNK, (request,), true).await
-}
-
-/// Atomically publish the staged workspace file.
-pub async fn finish_file_write(
- config: &Config,
- request: contract::FinishFileWriteRequest,
-) -> Result<contract::FileWriteProgress, String> {
- call(config, FINISH_FILE_WRITE, (request,), false).await
-}
-
-/// Abort a staged workspace write without publishing it.
-pub async fn abort_file_write(
- config: &Config,
- request: contract::AbortFileWriteRequest,
-) -> Result<(), String> {
- call(config, ABORT_FILE_WRITE, (request,), false).await
-}
-
-async fn call<Request, Reply>(
- config: &Config,
- member: &'static str,
- request: Request,
- confidential: bool,
-) -> Result<Reply, String>
-where
- Request: Serialize + Send,
- Reply: DeserializeOwned,
-{
- // A registry index from before the storage contract moves into the
- // module's storage before its first call. Once per workspace, best effort.
- super::import_legacy_tinybox(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config.workspace_dir,
- )
- .await;
- call_with_client(&module_client(config), member, request, confidential).await
-}
-
-fn module_client(config: &Config) -> ModuleClient {
- crate::modules::client(config.clone()).with_timeout(MODULE_CALL_TIMEOUT)
-}
-
-fn message(error: ModuleCallError) -> String {
- error.to_string()
-}
-
-async fn call_with_client<Request, Reply>(
- client: &ModuleClient,
- member: &'static str,
- request: Request,
- confidential: bool,
-) -> Result<Reply, String>
-where
- Request: Serialize + Send,
- Reply: DeserializeOwned,
-{
- let capabilities: contract::ModuleCapabilities = client
- .call(MODULE_ID, CAPABILITIES, ())
- .await
- .map_err(message)?;
- require_compatible(capabilities.contract_version).map_err(message)?;
- if confidential {
- client
- .call_confidential(MODULE_ID, member, request)
- .await
- .map_err(message)
- } else {
- client
- .call(MODULE_ID, member, request)
- .await
- .map_err(message)
- }
-}
+#[cfg(feature = "modules")]
+pub use operations::*;
diff --git a/crates/openhuman-core/src/sandbox/host_runtime.rs b/crates/openhuman-core/src/modules/tinybox/host_runtime.rs
similarity index 100%
rename from crates/openhuman-core/src/sandbox/host_runtime.rs
rename to crates/openhuman-core/src/modules/tinybox/host_runtime.rs
diff --git a/crates/openhuman-core/src/sandbox/host_runtime_tests.rs b/crates/openhuman-core/src/modules/tinybox/host_runtime_tests.rs
similarity index 100%
rename from crates/openhuman-core/src/sandbox/host_runtime_tests.rs
rename to crates/openhuman-core/src/modules/tinybox/host_runtime_tests.rs
diff --git a/crates/openhuman-core/src/modules/tinybox/operations.rs b/crates/openhuman-core/src/modules/tinybox/operations.rs
new file mode 100644
index 0000000000..547e6c882f
--- /dev/null
+++ b/crates/openhuman-core/src/modules/tinybox/operations.rs
@@ -0,0 +1,481 @@
+//! Host calls to the TinyBox sandbox module.
+//!
+//! This adapter owns only OpenHuman's module loading and bus boundary. Request
+//! and response types come from `tinybox-bus`; isolation, process ownership,
+//! file access, forwarding, and output collection stay in TinyBox.
+
+use std::time::Duration;
+
+use serde::{de::DeserializeOwned, Serialize};
+pub use tinybox_bus as contract;
+
+
+use crate::config::Config;
+
+use tinybus::module::{ModuleCallError, ModuleClient};
+
+/// Registry id for the TinyBox module.
+pub const MODULE_ID: &str = "tinybox";
+
+const MODULE_CALL_TIMEOUT: Duration = Duration::from_secs(300);
+
+// `tinybox_bus::names` carries one named constant per bus member; the host
+// never indexes `METHODS` positionally nor keeps a second set of wire strings.
+const DESCRIBE: &str = contract::names::DESCRIBE;
+const CREATE: &str = contract::names::CREATE;
+const EXEC: &str = contract::names::EXEC;
+const INSPECT: &str = contract::names::INSPECT;
+const CLOSE: &str = contract::names::CLOSE;
+const SPAWN: &str = contract::names::SPAWN;
+const IS_RUNNING: &str = contract::names::IS_RUNNING;
+const CANCEL: &str = contract::names::CANCEL;
+const ANALYZE_SHELL: &str = contract::names::ANALYZE_SHELL;
+const RESERVE: &str = contract::names::RESERVE;
+const CAPABILITIES: &str = contract::names::CAPABILITIES;
+const SHUTDOWN: &str = contract::names::SHUTDOWN;
+const FORWARD: &str = contract::names::FORWARD;
+const CLOSE_FORWARD: &str = contract::names::CLOSE_FORWARD;
+const JAIL_STATUS: &str = contract::names::JAIL_STATUS;
+const ANALYZE_COMMAND: &str = contract::names::ANALYZE_COMMAND;
+const BEGIN_FILE_READ: &str = contract::names::BEGIN_FILE_READ;
+const READ_FILE_CHUNK: &str = contract::names::READ_FILE_CHUNK;
+const FINISH_FILE_READ: &str = contract::names::FINISH_FILE_READ;
+const BEGIN_FILE_WRITE: &str = contract::names::BEGIN_FILE_WRITE;
+const WRITE_FILE_CHUNK: &str = contract::names::WRITE_FILE_CHUNK;
+const FINISH_FILE_WRITE: &str = contract::names::FINISH_FILE_WRITE;
+const ABORT_FILE_WRITE: &str = contract::names::ABORT_FILE_WRITE;
+const START_EXEC: &str = contract::names::START_EXEC;
+const READ_OUTPUT: &str = contract::names::READ_OUTPUT;
+const RELEASE_OUTPUT: &str = contract::names::RELEASE_OUTPUT;
+const START_JAILED_EXEC: &str = contract::names::START_JAILED_EXEC;
+const RUN_DOCKER: &str = contract::names::RUN_DOCKER;
+const DOCKER_STATUS: &str = contract::names::DOCKER_STATUS;
+const CLEANUP_DOCKER: &str = contract::names::CLEANUP_DOCKER;
+const RUN_DOCKER_WITHOUT_WORKSPACE: &str = contract::names::RUN_DOCKER_WITHOUT_WORKSPACE;
+const SELECT_SHELL: &str = contract::names::SELECT_SHELL;
+const RESOLVE_LOCAL_GRANTS: &str = contract::names::RESOLVE_LOCAL_GRANTS;
+const START_NATIVE_SHELL_EXEC: &str = contract::names::START_NATIVE_SHELL_EXEC;
+const VALIDATE_DOCKER_POLICY: &str = contract::names::VALIDATE_DOCKER_POLICY;
+const DOCKER_CALL_GRACE: Duration = Duration::from_secs(5);
+
+#[cfg(test)]
+#[path = "tinybox_tests.rs"]
+mod tests;
+
+/// Resolve TinyBox, require the bus contract supported by this host, and
+/// return its proxy.
+///
+/// The capability check is deliberate: the registry artifact may be older
+/// than this compiled host while a release is being rolled out. A package
+/// version alone does not prove which additive bus members it serves.
+///
+/// # Errors
+///
+/// Returns a safe loading, transport, or contract-version error. It does not
+/// include request arguments or filesystem paths.
+fn require_compatible(version: (u32, u32)) -> Result<(), ModuleCallError> {
+ if contract::is_compatible(version) {
+ return Ok(());
+ }
+ crate::modules::report::incompatible_contract(MODULE_ID);
+ Err(ModuleCallError::IncompatibleContract)
+}
+
+/// Describe the loaded TinyBox module.
+pub async fn describe(config: &Config) -> Result<String, String> {
+ call(config, DESCRIBE, (), false).await
+}
+
+/// Read module capabilities after enforcing contract compatibility.
+pub async fn capabilities(config: &Config) -> Result<contract::ModuleCapabilities, String> {
+ let client = module_client(config);
+ let capabilities: contract::ModuleCapabilities = client
+ .call(MODULE_ID, CAPABILITIES, ())
+ .await
+ .map_err(message)?;
+ require_compatible(capabilities.contract_version).map_err(message)?;
+ Ok(capabilities)
+}
+
+/// Read the native jail facts; the caller applies its own policy to them.
+pub async fn jail_status(config: &Config) -> Result<contract::JailStatus, String> {
+ call(config, JAIL_STATUS, (), false).await
+}
+
+/// Reserve an opaque identifier before native startup.
+pub async fn reserve(
+ config: &Config,
+ request: contract::ReserveRequest,
+) -> Result<contract::ResourceId, String> {
+ call(config, RESERVE, (request,), false).await
+}
+
+/// Create a sandbox using its caller-known reservation.
+pub async fn create(
+ config: &Config,
+ request: contract::CreateRequest,
+) -> Result<contract::ResourceInfo, String> {
+ call(config, CREATE, (request,), true).await
+}
+
+/// Execute a collected command in a resource.
+pub async fn exec(
+ config: &Config,
+ request: contract::ExecRequest,
+) -> Result<contract::ExecOutput, String> {
+ call(config, EXEC, (request,), true).await
+}
+
+/// Inspect a module-owned resource.
+pub async fn inspect(
+ config: &Config,
+ resource: contract::ResourceId,
+) -> Result<contract::ResourceInfo, String> {
+ call(config, INSPECT, (resource,), false).await
+}
+
+/// Close a resource and wait for the module's native cleanup acknowledgement.
+pub async fn close(config: &Config, resource: contract::ResourceId) -> Result<(), String> {
+ call(config, CLOSE, (resource,), false).await
+}
+
+/// Shut down the module-owned resources.
+pub async fn shutdown(config: &Config) -> Result<(), String> {
+ call(config, SHUTDOWN, (), false).await
+}
+
+/// Start a detached process using a pre-reserved process id.
+pub async fn spawn(
+ config: &Config,
+ request: contract::SpawnRequest,
+) -> Result<contract::ProcessRef, String> {
+ call(config, SPAWN, (request,), true).await
+}
+
+/// Start a supervised process with live output collection.
+pub async fn start_exec(
+ config: &Config,
+ request: contract::SpawnRequest,
+) -> Result<contract::ProcessRef, String> {
+ call(config, START_EXEC, (request,), true).await
+}
+
+/// Start a process in the explicitly requested native directory jail.
+pub async fn start_jailed_exec(
+ config: &Config,
+ request: contract::SpawnRequest,
+ jail: contract::NativeJailConfig,
+) -> Result<contract::ProcessRef, String> {
+ call(config, START_JAILED_EXEC, (request, jail), true).await
+}
+
+/// Ask TinyBox which shell it would spawn for a command on its machine.
+///
+/// The reply is discovery only: the host never spawns the plan itself, it
+/// executes through [`start_native_shell_exec`].
+pub async fn select_shell(
+ config: &Config,
+ request: contract::SelectShellRequest,
+) -> Result<contract::ShellInvocation, String> {
+ call(config, SELECT_SHELL, (request,), false).await
+}
+
+/// Start a local shell under module-owned supervision and output replay.
+///
+/// The caller has already authorized the command; the module owns shell
+/// selection, the safe child environment and cleanup.
+pub async fn start_native_shell_exec(
+ config: &Config,
+ request: contract::ShellExecRequest,
+) -> Result<contract::ProcessRef, String> {
+ call(config, START_NATIVE_SHELL_EXEC, (request,), true).await
+}
+
+/// Ask TinyBox whether a Docker run would weaken isolation, without starting
+/// one. This is the same check `RunDocker` enforces before creating a container.
+pub async fn validate_docker_policy(
+ config: &Config,
+ request: contract::DockerPolicyRequest,
+) -> Result<contract::DockerPolicyReport, String> {
+ call(config, VALIDATE_DOCKER_POLICY, (request,), false).await
+}
+
+/// Ask TinyBox to resolve the existing toolchain and configured grants for the
+/// local jail.
+///
+/// The request carries the host's protected-path floor as input; the reply is
+/// a candidate set the host must still pass through its own floor.
+pub async fn resolve_local_grants(
+ config: &Config,
+ request: contract::ResolveLocalGrantsRequest,
+) -> Result<contract::LocalGrants, String> {
+ call(config, RESOLVE_LOCAL_GRANTS, (request,), false).await
+}
+
+/// Run one bounded Docker-isolated command through TinyBox.
+///
+/// The caller applies authorization and Docker policy before sending this
+/// confidential request. The bus deadline includes a short reply grace after
+/// TinyBox's own command deadline so the timeout result can be delivered.
+pub async fn run_docker(
+ config: &Config,
+ request: contract::DockerRunRequest,
+) -> Result<contract::DockerRunOutput, String> {
+ let timeout = Duration::from_millis(request.timeout_ms).saturating_add(DOCKER_CALL_GRACE);
+ let client = crate::modules::client(config.clone()).with_timeout(timeout);
+ call_run_docker_with_client(&client, request).await
+}
+
+/// Run one bounded Docker-isolated command without mounting a workspace.
+///
+/// This is used by the configured Docker runtime, whose existing behavior
+/// intentionally does not expose host workspace files to the container.
+pub async fn run_docker_without_workspace(
+ config: &Config,
+ request: contract::DockerRunRequest,
+) -> Result<contract::DockerRunOutput, String> {
+ let timeout = Duration::from_millis(request.timeout_ms).saturating_add(DOCKER_CALL_GRACE);
+ let client = crate::modules::client(config.clone()).with_timeout(timeout);
+ call_run_docker_without_workspace_with_client(&client, request).await
+}
+
+/// Probe the Docker daemon owned by the TinyBox module host.
+pub async fn docker_status(
+ config: &Config,
+ request: contract::DockerStatusRequest,
+) -> Result<contract::DockerStatus, String> {
+ call_docker_status_with_client(&module_client(config), request).await
+}
+
+/// Stop TinyBox-owned containers matching the supplied label.
+pub async fn cleanup_docker(
+ config: &Config,
+ request: contract::CleanupDockerRequest,
+) -> Result<contract::CleanupDockerOutput, String> {
+ call_cleanup_docker_with_client(&module_client(config), request).await
+}
+
+fn docker_control_deadline(timeout_ms: u64, grace: Duration) -> Duration {
+ Duration::from_millis(timeout_ms.min(contract::MAX_DOCKER_CONTROL_TIMEOUT_MS))
+ .saturating_add(grace)
+}
+
+fn docker_control_client(client: &ModuleClient, timeout_ms: u64, grace: Duration) -> ModuleClient {
+ client
+ .clone()
+ .with_timeout(docker_control_deadline(timeout_ms, grace))
+}
+
+async fn call_docker_status_with_client(
+ client: &ModuleClient,
+ request: contract::DockerStatusRequest,
+) -> Result<contract::DockerStatus, String> {
+ call_docker_status_with_grace(client, request, DOCKER_CALL_GRACE).await
+}
+
+async fn call_docker_status_with_grace(
+ client: &ModuleClient,
+ request: contract::DockerStatusRequest,
+ grace: Duration,
+) -> Result<contract::DockerStatus, String> {
+ let client = docker_control_client(client, request.timeout_ms, grace);
+ call_with_client(&client, DOCKER_STATUS, (request,), false).await
+}
+
+async fn call_cleanup_docker_with_client(
+ client: &ModuleClient,
+ request: contract::CleanupDockerRequest,
+) -> Result<contract::CleanupDockerOutput, String> {
+ call_cleanup_docker_with_grace(client, request, DOCKER_CALL_GRACE).await
+}
+
+async fn call_cleanup_docker_with_grace(
+ client: &ModuleClient,
+ request: contract::CleanupDockerRequest,
+ grace: Duration,
+) -> Result<contract::CleanupDockerOutput, String> {
+ let client = docker_control_client(client, request.timeout_ms, grace);
+ call_with_client(&client, CLEANUP_DOCKER, (request,), false).await
+}
+
+async fn call_run_docker_with_client(
+ client: &ModuleClient,
+ request: contract::DockerRunRequest,
+) -> Result<contract::DockerRunOutput, String> {
+ call_with_client(client, RUN_DOCKER, (request,), true).await
+}
+
+async fn call_run_docker_without_workspace_with_client(
+ client: &ModuleClient,
+ request: contract::DockerRunRequest,
+) -> Result<contract::DockerRunOutput, String> {
+ call_with_client(client, RUN_DOCKER_WITHOUT_WORKSPACE, (request,), true).await
+}
+
+/// Read a bounded output batch from a live or completed process.
+pub async fn read_output(
+ config: &Config,
+ process: contract::ProcessRef,
+ cursor: u64,
+) -> Result<contract::OutputBatch, String> {
+ call(config, READ_OUTPUT, (process, cursor), false).await
+}
+
+/// Release retained output and process ownership after terminal observation.
+pub async fn release_output(config: &Config, process: contract::ProcessRef) -> Result<(), String> {
+ call(config, RELEASE_OUTPUT, (process,), false).await
+}
+
+/// Check whether a detached process remains active.
+pub async fn is_running(config: &Config, process: contract::ProcessRef) -> Result<bool, String> {
+ call(config, IS_RUNNING, (process,), false).await
+}
+
+/// Cancel a detached process and wait for acknowledged cleanup.
+pub async fn cancel(config: &Config, process: contract::ProcessRef) -> Result<(), String> {
+ call(config, CANCEL, (process,), false).await
+}
+
+/// Open a module-owned port forward.
+pub async fn forward(
+ config: &Config,
+ request: contract::ForwardRequest,
+) -> Result<contract::ForwardInfo, String> {
+ call(config, FORWARD, (request,), false).await
+}
+
+/// Close a module-owned port forward.
+pub async fn close_forward(
+ config: &Config,
+ request: contract::CloseForwardRequest,
+) -> Result<(), String> {
+ call(config, CLOSE_FORWARD, (request,), false).await
+}
+
+/// Ask TinyBox for structural shell facts; this is not an authorization check.
+pub async fn analyze_shell(
+ config: &Config,
+ command: String,
+) -> Result<contract::ShellAnalysis, String> {
+ call(config, ANALYZE_SHELL, (command,), false).await
+}
+
+/// Ask TinyBox for generic command facts; the caller still decides policy.
+pub async fn analyze_command(
+ config: &Config,
+ command: String,
+) -> Result<contract::CommandAnalysis, String> {
+ call(config, ANALYZE_COMMAND, (command,), false).await
+}
+
+/// Open a bounded workspace file reader.
+pub async fn begin_file_read(
+ config: &Config,
+ request: contract::BeginFileReadRequest,
+) -> Result<contract::FileReadInfo, String> {
+ call(config, BEGIN_FILE_READ, (request,), false).await
+}
+
+/// Read one bounded range from an open file reader.
+pub async fn read_file_chunk(
+ config: &Config,
+ request: contract::ReadFileChunkRequest,
+) -> Result<contract::FileChunk, String> {
+ call(config, READ_FILE_CHUNK, (request,), false).await
+}
+
+/// Close a workspace file reader.
+pub async fn finish_file_read(
+ config: &Config,
+ request: contract::FinishFileReadRequest,
+) -> Result<(), String> {
+ call(config, FINISH_FILE_READ, (request,), false).await
+}
+
+/// Open an atomic staged workspace file writer.
+pub async fn begin_file_write(
+ config: &Config,
+ request: contract::BeginFileWriteRequest,
+) -> Result<contract::FileWriteInfo, String> {
+ call(config, BEGIN_FILE_WRITE, (request,), false).await
+}
+
+/// Write one bounded sequential chunk.
+pub async fn write_file_chunk(
+ config: &Config,
+ request: contract::WriteFileChunkRequest,
+) -> Result<contract::FileWriteProgress, String> {
+ call(config, WRITE_FILE_CHUNK, (request,), true).await
+}
+
+/// Atomically publish the staged workspace file.
+pub async fn finish_file_write(
+ config: &Config,
+ request: contract::FinishFileWriteRequest,
+) -> Result<contract::FileWriteProgress, String> {
+ call(config, FINISH_FILE_WRITE, (request,), false).await
+}
+
+/// Abort a staged workspace write without publishing it.
+pub async fn abort_file_write(
+ config: &Config,
+ request: contract::AbortFileWriteRequest,
+) -> Result<(), String> {
+ call(config, ABORT_FILE_WRITE, (request,), false).await
+}
+
+async fn call<Request, Reply>(
+ config: &Config,
+ member: &'static str,
+ request: Request,
+ confidential: bool,
+) -> Result<Reply, String>
+where
+ Request: Serialize + Send,
+ Reply: DeserializeOwned,
+{
+ // A registry index from before the storage contract moves into the
+ // module's storage before its first call. Once per workspace, best effort.
+ super::import_legacy_tinybox(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config.workspace_dir,
+ )
+ .await;
+ call_with_client(&module_client(config), member, request, confidential).await
+}
+
+fn module_client(config: &Config) -> ModuleClient {
+ crate::modules::client(config.clone()).with_timeout(MODULE_CALL_TIMEOUT)
+}
+
+fn message(error: ModuleCallError) -> String {
+ error.to_string()
+}
+
+async fn call_with_client<Request, Reply>(
+ client: &ModuleClient,
+ member: &'static str,
+ request: Request,
+ confidential: bool,
+) -> Result<Reply, String>
+where
+ Request: Serialize + Send,
+ Reply: DeserializeOwned,
+{
+ let capabilities: contract::ModuleCapabilities = client
+ .call(MODULE_ID, CAPABILITIES, ())
+ .await
+ .map_err(message)?;
+ require_compatible(capabilities.contract_version).map_err(message)?;
+ if confidential {
+ client
+ .call_confidential(MODULE_ID, member, request)
+ .await
+ .map_err(message)
+ } else {
+ client
+ .call(MODULE_ID, member, request)
+ .await
+ .map_err(message)
+ }
+}
diff --git a/crates/openhuman-core/src/sandbox/README.md b/crates/openhuman-core/src/sandbox/README.md
index 80e5fd3aea..493af22cc6 100644
--- a/crates/openhuman-core/src/sandbox/README.md
+++ b/crates/openhuman-core/src/sandbox/README.md
@@ -233,7 +233,7 @@ The settings surface (`get_sandbox_settings`, `[security.sandbox]` and
default install the sandbox and the `is_always_forbidden` floor are what
bound an agent.
- Shell selection and the child environment for each platform are TinyBox's
- (`SelectShell` / `StartNativeShellExec`); `sandbox::host_runtime::host_shell_flavor`
+ (`SelectShell` / `StartNativeShellExec`); `modules::tinybox::host_runtime::host_shell_flavor`
only tells the model which family that is.
- The agent's `SandboxMode` is declared in its definition
(`tinyagents_definition::SandboxMode`) and read per call through
diff --git a/crates/openhuman-core/src/sandbox/mod.rs b/crates/openhuman-core/src/sandbox/mod.rs
index 3d091c0624..f6b817bcec 100644
--- a/crates/openhuman-core/src/sandbox/mod.rs
+++ b/crates/openhuman-core/src/sandbox/mod.rs
@@ -11,7 +11,6 @@
//! explicit elevated escape paths.
pub mod docker;
-pub mod host_runtime;
pub(crate) mod local_grants;
pub(crate) mod native_jail;
pub mod ops;
diff --git a/crates/openhuman-core/src/tools/impl/system/shell.rs b/crates/openhuman-core/src/tools/impl/system/shell.rs
index aac7e8faa6..aa9e7ef389 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell.rs
@@ -5,7 +5,7 @@ use super::shell_support::{command_failure, exec_result, python_utf8_env, tool_t
use crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool;
use crate::config::Config;
use crate::core::runtime::DispatchCtx;
-use crate::sandbox::host_runtime::RuntimeAdapter;
+use crate::modules::tinybox::host_runtime::RuntimeAdapter;
use crate::security::{AuditLogger, CommandExecutionLog, SecurityPolicy};
use async_trait::async_trait;
use serde_json::json;
diff --git a/crates/openhuman-core/src/tools/impl/system/shell_description.rs b/crates/openhuman-core/src/tools/impl/system/shell_description.rs
index e7ad17c9bb..b9ca9a54b0 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell_description.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell_description.rs
@@ -5,7 +5,7 @@
//! a pure function of the contract's [`ShellFlavor`] so both flavors are testable
//! on any host.
-use crate::sandbox::host_runtime::ShellFlavor;
+use crate::modules::tinybox::host_runtime::ShellFlavor;
/// The `shell` tool description for the shell this host spawns. Models default
/// to POSIX syntax, so on Windows the description says outright that commands
diff --git a/crates/openhuman-core/src/tools/impl/system/shell_tests.rs b/crates/openhuman-core/src/tools/impl/system/shell_tests.rs
index 51c28dd3f3..a712052e83 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell_tests.rs
@@ -1,5 +1,5 @@
use super::*;
-use crate::sandbox::host_runtime::{NativeRuntime, RuntimeAdapter};
+use crate::modules::tinybox::host_runtime::{NativeRuntime, RuntimeAdapter};
use crate::security::{AutonomyLevel, SecurityPolicy};
use tinybox_bus::CommandClass;
diff --git a/crates/openhuman-core/src/tools/impl/system/shell_tests_platform_tests.rs b/crates/openhuman-core/src/tools/impl/system/shell_tests_platform_tests.rs
index cc31e57ace..e12ad99f62 100644
--- a/crates/openhuman-core/src/tools/impl/system/shell_tests_platform_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/system/shell_tests_platform_tests.rs
@@ -5,7 +5,7 @@
//! pinned on every CI host, not only on a Windows runner.
use super::*;
-use crate::sandbox::host_runtime::{host_shell_flavor, ShellFlavor};
+use crate::modules::tinybox::host_runtime::{host_shell_flavor, ShellFlavor};
#[cfg(feature = "modules")]
use crate::tools::system::shell_support::PYTHON_UTF8_DEFAULTS;
use std::collections::HashMap;
@@ -185,7 +185,7 @@ fn the_tool_describes_the_shell_its_runtime_spawns() {
#[test]
fn the_docker_runtime_always_runs_posix_sh() {
- let docker = crate::sandbox::host_runtime::create_runtime(
+ let docker = crate::modules::tinybox::host_runtime::create_runtime(
&crate::config::RuntimeConfig {
kind: "docker".into(),
..crate::config::RuntimeConfig::default()
diff --git a/crates/openhuman-core/src/tools/native_ops.rs b/crates/openhuman-core/src/tools/native_ops.rs
index e3089f9088..5fed8340e7 100644
--- a/crates/openhuman-core/src/tools/native_ops.rs
+++ b/crates/openhuman-core/src/tools/native_ops.rs
@@ -4,7 +4,7 @@ use std::time::Instant;
use crate::config::Config;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
-use crate::sandbox::host_runtime::{NativeRuntime, RuntimeAdapter};
+use crate::modules::tinybox::host_runtime::{NativeRuntime, RuntimeAdapter};
use crate::security::SecurityPolicy;
use crate::tools;
use crate::tools::native_ops_types::{ExecuteToolOutcome, RuntimeToolSummary};
diff --git a/crates/openhuman-core/src/tools/ops.rs b/crates/openhuman-core/src/tools/ops.rs
index f2bda3b270..42fe1a3b87 100644
--- a/crates/openhuman-core/src/tools/ops.rs
+++ b/crates/openhuman-core/src/tools/ops.rs
@@ -2,7 +2,7 @@
use super::implementations::network::{McpListServersTool, McpListToolsTool};
use super::*;
use crate::config::{Config, DelegateAgentConfig};
-use crate::sandbox::host_runtime::{NativeRuntime, RuntimeAdapter};
+use crate::modules::tinybox::host_runtime::{NativeRuntime, RuntimeAdapter};
use crate::security::{AuditLogger, SecurityPolicy};
use crate::tools::delegate::DelegateTool;
use std::collections::HashMap;