Skip to content

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

10.0 KB
Raw Blame
use super::*;
use crate::core::all::assert_schema_controller_parity;
use crate::core::runtime::context::CoreContext;
use crate::core::runtime::DispatchCtx;
use crate::core::runtime::DomainSet;

/// Scope a `Config` onto the embedder-config seam that `load_config_with_timeout()`
/// prefers, so a handler reads it instead of doing live disk I/O against
/// `~/.openhuman` — which is slow and racy across parallel unit tests (#6081).
fn scoped_ctx(config: crate::config::Config) -> std::sync::Arc<CoreContext> {
    CoreContext::for_test_with_config(DomainSet::full(), config)
}

#[test]
fn sandbox_schema_catalog_matches_registered_controllers() {
    crate::core::all::expect_schema_namespace(&all_controller_schemas(), "sandbox");
    assert_schema_controller_parity(
        &all_controller_schemas(),
        &all_registered_controllers(),
        "status",
    );
}

#[tokio::test]
async fn handle_status_returns_json() {
    let ctx = scoped_ctx(crate::config::Config::default());
    let result = CoreContext::scope(ctx.clone(), async {
        handle_status(DispatchCtx::new(ctx.clone()), Map::new()).await
    })
    .await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn handle_resolve_policy_none() {
    let mut params = Map::new();
    params.insert("sandbox_mode".into(), Value::String("none".into()));
    let ctx = scoped_ctx(crate::config::Config::default());
    let result = CoreContext::scope(ctx.clone(), async {
        handle_resolve_policy(DispatchCtx::new(ctx.clone()), params).await
    })
    .await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn handle_resolve_policy_sandboxed_remote() {
    let mut params = Map::new();
    params.insert("sandbox_mode".into(), Value::String("sandboxed".into()));
    params.insert("is_remote".into(), Value::Bool(true));
    let ctx = scoped_ctx(crate::config::Config::default());
    let result = CoreContext::scope(ctx.clone(), async {
        handle_resolve_policy(DispatchCtx::new(ctx.clone()), params).await
    })
    .await;
    assert!(result.is_ok());
    let val = result.unwrap();
    let backend = val.get("backend").and_then(|b| b.as_str());
    assert_eq!(backend, Some("docker"));
}

/// Build a `Config` carrying a Docker runtime with a NON-default image, so a
/// test can prove the RPC handlers read the loaded runtime rather than
/// `RuntimeConfig::default()` (#6081).
fn docker_runtime_config() -> crate::config::Config {
    let mut config = crate::config::Config::default();
    config.workspace_dir = std::path::PathBuf::from("/tmp/openhuman-6081-ws");
    config.runtime.kind = "docker".into();
    config.runtime.docker.image = "ghcr.io/example/custom-sandbox:6081".into();
    config
}

/// #6081 regression — `sandbox_resolve_policy` must honor the loaded
/// `[runtime] kind = "docker"` config. Before the fix the handler answered from
/// `RuntimeConfig::default()` (`kind = "native"`), so a Docker-configured user
/// was told `backend = "local"`. The config is injected through the
/// embedder-config seam that `load_config_with_timeout()` reads, scoped for the
/// duration of the dispatch.
#[tokio::test]
async fn handle_resolve_policy_honors_loaded_docker_runtime() {
    let ctx = scoped_ctx(docker_runtime_config());

    let mut params = Map::new();
    params.insert("sandbox_mode".into(), Value::String("sandboxed".into()));
    params.insert("is_remote".into(), Value::Bool(false));

    let val = CoreContext::scope(ctx.clone(), async {
        handle_resolve_policy(DispatchCtx::new(ctx.clone()), params).await
    })
    .await
    .expect("resolve_policy should succeed under the scoped config");

    // Consequence #1: the backend follows `[runtime] kind`, not the default.
    assert_eq!(
        val.get("backend").and_then(|b| b.as_str()),
        Some("docker"),
        "docker runtime must resolve to the docker backend; got {val:?}"
    );

    // Consequence #2: docker overrides reflect the user's `[runtime.docker]`,
    // not the compiled-in default image.
    let image = val
        .get("docker_overrides")
        .and_then(|o| o.get("image"))
        .and_then(|i| i.as_str());
    assert_eq!(
        image,
        Some("ghcr.io/example/custom-sandbox:6081"),
        "docker_overrides.image must reflect the loaded [runtime.docker].image; got {val:?}"
    );
}

/// #6081 regression — `sandbox_status` must likewise read the loaded runtime.
/// With `[runtime] kind = "docker"` and `is_remote = false`, a sandboxed status
/// probe resolves the docker backend; before the fix it answered "local" from
/// `RuntimeConfig::default()`. Uses `backend = "local"` in the request to prove
/// the resolution comes from the loaded config's `kind`, not the caller's
/// backend name.
#[tokio::test]
async fn handle_status_honors_loaded_docker_runtime() {
    let ctx = scoped_ctx(docker_runtime_config());

    let mut params = Map::new();
    params.insert("backend".into(), Value::String("local".into()));
    params.insert("is_remote".into(), Value::Bool(false));

    let val = CoreContext::scope(ctx.clone(), async {
        handle_status(DispatchCtx::new(ctx.clone()), params).await
    })
    .await
    .expect("status should succeed under the scoped config");

    assert_eq!(
        val.get("kind").and_then(|k| k.as_str()),
        Some("docker"),
        "docker runtime must make status report the docker backend; got {val:?}"
    );
}

/// #6081 (F2) — `sandbox_resolve_policy` must use the loaded config's
/// already-resolved `action_dir`, not recompute it from `action_dir_override`
/// alone. An embedder sets `Config.action_dir` directly via
/// `CoreBuilder::action_dir(..)` and leaves `action_dir_override` at `None`;
/// `resolve_action_dir(&None)` would have ignored that and returned the default
/// projects dir, so the resolved policy would have pointed the sandbox at the
/// wrong root. This pins that the handler honors the embedder's programmatic
/// `action_dir`.
#[tokio::test]
async fn handle_resolve_policy_uses_loaded_action_dir() {
    let embedder_action_dir = std::path::PathBuf::from("/tmp/openhuman-6081-action-dir");
    let mut config = crate::config::Config::default();
    // The embedder-shaped case: `action_dir` set directly, override untouched.
    config.action_dir = embedder_action_dir.clone();
    config.action_dir_override = None;
    let ctx = scoped_ctx(config);

    let mut params = Map::new();
    params.insert("sandbox_mode".into(), Value::String("none".into()));

    let val = CoreContext::scope(ctx.clone(), async {
        handle_resolve_policy(DispatchCtx::new(ctx.clone()), params).await
    })
    .await
    .expect("resolve_policy should succeed under the scoped config");

    assert_eq!(
        val.get("workspace_root").and_then(|w| w.as_str()),
        Some(embedder_action_dir.to_str().unwrap()),
        "policy workspace_root must use the loaded config.action_dir, not a \
         recomputed default; got {val:?}"
    );
}

fn docker_policy_params(workspace: &str) -> Map<String, Value> {
    let policy = super::super::types::SandboxPolicy {
        backend: super::super::types::SandboxBackendKind::Docker,
        workspace_root: std::path::PathBuf::from(workspace),
        state_dir: std::path::PathBuf::from("/tmp/state"),
        read_only_mounts: vec![],
        read_write_mounts: vec![],
        allow_network: false,
        env_passthrough: vec![],
        docker_overrides: None,
    };
    let mut params = Map::new();
    params.insert("policy".into(), serde_json::to_value(&policy).unwrap());
    params
}

#[tokio::test]
async fn handle_validate_policy_rejects_a_malformed_policy() {
    let mut params = Map::new();
    params.insert("policy".into(), Value::String("not a policy".into()));
    let err = handle_validate_policy(DispatchCtx::process_default(), params)
        .await
        .unwrap_err();
    assert!(err.contains("Invalid policy"), "{err}");
}

/// The verdict belongs to the TinyBox module. With none available the RPC must
/// say so; answering `valid: true` for an unchecked policy would fail open.
#[cfg(feature = "modules")]
#[tokio::test]
async fn handle_validate_policy_errors_instead_of_passing_when_tinybox_is_unavailable() {
    let mut config = crate::config::Config::default();
    config.modules.enabled = false;
    let ctx = scoped_ctx(config);
    let result = CoreContext::scope(ctx.clone(), async {
        handle_validate_policy(DispatchCtx::new(ctx.clone()), docker_policy_params("/")).await
    })
    .await;
    let err = result.unwrap_err();
    assert!(err.contains("Policy validation is unavailable"), "{err}");
}

// ── [components] sandbox = false ──────────────────────────────────────

#[tokio::test]
async fn every_sandbox_method_answers_component_disabled_when_sandbox_is_off() {
    type ControllerHandler = fn(DispatchCtx, Map<String, Value>) -> ControllerFuture;
    let mut set = crate::core::runtime::ComponentSet::all();
    set.sandbox = false;
    let ctx = crate::core::runtime::context::CoreContext::for_test_with_components(set);
    let handlers: [ControllerHandler; 4] = [
        handle_status,
        handle_resolve_policy,
        handle_cleanup_orphans,
        handle_validate_policy,
    ];
    let dispatch = DispatchCtx::new(ctx.clone());
    crate::core::runtime::context::CoreContext::scope(ctx, async {
        for handler in handlers {
            let err = handler(dispatch.clone(), Map::new()).await.unwrap_err();
            let decoded = crate::core::StructuredRpcError::decode(&err).expect("typed error");
            assert_eq!(decoded.message, "COMPONENT_DISABLED:sandbox");
        }
    })
    .await;
}

#[tokio::test]
async fn sandbox_methods_are_not_gated_when_sandbox_is_on() {
    let ctx = crate::core::runtime::context::CoreContext::for_test_with_components(
        crate::core::runtime::ComponentSet::all(),
    );
    crate::core::runtime::context::CoreContext::scope(ctx.clone(), async {
        let err = handle_validate_policy(DispatchCtx::new(ctx.clone()), Map::new())
            .await
            .unwrap_err();
        assert!(!err.contains("COMPONENT_DISABLED"), "{err}");
    })
    .await;
}