Skip to content

Execution Sandbox Plane (openhusky-sandbox) ​

openhusky-sandbox implements OpenHusky's Execution Sandbox Plane, decoupling the Control Plane (openhusky-core, openhusky-skills) from filesystem and process execution across five isolation tiers (L0–L4). It relies on standard JDK 21 APIs (ProcessBuilder, Virtual Threads, java.nio.file) with zero docker-java or Netty dependencies.

xml
<dependency>
    <groupId>dev.openhusky</groupId>
    <artifactId>openhusky-sandbox</artifactId>
    <version>0.3.0</version>
</dependency>
kotlin
implementation("dev.openhusky:openhusky-sandbox:0.3.0")

Security Isolation Tiers (L0–L4) ​

TierImplementationIsolation MechanismsupportsExec()Typical Use Case
L0InMemorySandboxConcurrentHashMap<String, byte[]> virtual workspacefalseFast unit tests, stateless text/artifact generation without disk I/O
L1LocalHostSandboxHost directory with toRealPath() jail, atomic temp+move writes, JDK 21 ProcessBuildertrueLocal developer tools and trusted CI runners
L2LocalOsSandboxLinux bubblewrap (bwrap with --unshare-net, --ro-bind / /, --die-with-parent) or macOS sandbox-exec (Seatbelt SBPL), FAIL_CLOSED by defaulttrueCoding agents and untrusted workspace inspection on Linux/macOS hosts
L3DockerSandboxEphemeral container via JDK 21 docker CLI (--cap-drop ALL, --security-opt no-new-privileges, --network none default, memory/CPU/PID limits)trueMulti-tenant code execution and untrusted build/test workloads
L4Custom AgentSandboxImplement dev.openhusky.sandbox.AgentSandbox SPIImplementation-definedCloud micro-VMs & remote sandboxes (Kubernetes Pods, Firecracker, E2B, Modal, Daytona)

The 6 SandboxToolkit Tools ​

SandboxToolkit (dev.openhusky.sandbox.SandboxToolkit) exposes up to six AgentTool primitives backed by any AgentSandbox:

Tool NameexecutionMode()readOnly()Key ArgumentsSafety Defaults & Behavior
read_filePARALLEL_SAFEtruepath, offset / start_line, limitStreaming UTF-8 reader rejecting binary files; 1-based line numbers; 2000 lines / 50 KiB truncation; maxReadBytes = 131_072 (128 KiB).
glob_filesPARALLEL_SAFEtruepattern, path (default .), limit / max_resultsSkips .git, node_modules, target, build, .idea; sorted by lastModified descending; default limit = 1000.
grep_filesPARALLEL_SAFEtruepattern, path, glob, ignore_case, literal, context, limit / max_matchesStreaming regex/literal search over UTF-8 files (skips binaries); default limit = 100.
write_fileSEQUENTIALfalsepath, contentAtomic write inside sandbox jail; auto-creates parent directories; maxWriteBytes = 1_048_576 (1 MiB).
edit_fileSEQUENTIALfalsepath, edits[{old_text, new_text}] or old_text + new_text, replace_allSurgical replacement with BOM/CRLF preservation, fuzzy whitespace fallback, uniqueness check, and unified diff output.
shell_execSEQUENTIALfalsecommand, description, cwd, timeout_secondsOnly exposed when sandbox.supportsExec() and enableExec are true; streams throttled progress; defaultExecTimeout = 60s, maxExecTimeout = 300s, tail truncation (50 KiB).

Use SandboxToolkit.readOnlyTools(sandbox) to expose only read_file, glob_files, and grep_files (ideal for SubagentTool research agents).


Configuration Examples (L1, L2, L3) ​

Tier L1: Host Directory Sandbox ​

java
try (LocalHostSandbox sandbox = LocalHostSandbox.builder(Path.of("/workspace/project"))
        .deleteOnClose(false)
        .defaultEnv(Map.of("CI", "true"))
        .build()) {

    List<AgentTool> tools = SandboxToolkit.builder(sandbox)
            .defaultExecTimeout(Duration.ofSeconds(30))
            .maxExecTimeout(Duration.ofSeconds(120))
            .build();
}

Tier L2: OS-Level Sandbox (bwrap / Seatbelt, Fail-Closed) ​

LocalOsSandbox probes bwrap (on Linux) or /usr/bin/sandbox-exec (on macOS) at construction time. Under the default FallbackPolicy.FAIL_CLOSED, calling shell_exec throws SandboxException if OS sandboxing is unavailable rather than silently running unsandboxed on the host:

java
try (LocalOsSandbox sandbox = LocalOsSandbox.builder(Path.of("/workspace/project"))
        .networkAccess(false)                                     // --unshare-net / (deny network*)
        .fallbackPolicy(LocalOsSandbox.FallbackPolicy.FAIL_CLOSED)
        .extraReadOnlyPaths(List.of(Path.of("/opt/toolchains")))
        .build()) {

    // Optional: mount read-only skill bundles inside the sandbox workspace
    sandbox.mountBundles(".openhusky/skills", skillRegistry.exportAllFiles());

    AgentRuntime runtime = AgentRuntime.builder()
            .provider(provider)
            .model(model)
            .tools(SandboxToolkit.tools(sandbox))
            .permissionPolicy(PermissionPolicy.failClosed()
                    .allowReadOnlyTools()
                    .requireApproval("write_file", "edit_file", "shell_exec")
                    .build())
            .approvalHandler(approvalHandler)
            .build();
}

Tier L3: Ephemeral Container Sandbox (DockerSandbox) ​

DockerSandbox starts an ephemeral container lazily on first operation (sleep infinity with --rm --init --cap-drop ALL --security-opt no-new-privileges) and cleans it up via docker rm -f on close():

java
try (DockerSandbox sandbox = DockerSandbox.builder("eclipse-temurin:21-jdk")
        .workspaceDir("/workspace")
        .networkAccess(false)
        .memoryLimit("512m")
        .cpuLimit("1")
        .pidsLimit(128)
        .readOnlyRootFs(false)
        .startupTimeout(Duration.ofSeconds(30))
        .build()) {

    AgentRuntime runtime = AgentRuntime.builder()
            .provider(provider)
            .model(model)
            .tools(SandboxToolkit.tools(sandbox))
            .permissionPolicy(PermissionPolicy.requireApprovalForWrites())
            .approvalHandler(approvalHandler)
            .build();
}

Security Threat Model & Defense-in-Depth ​

  1. Path Traversal & Symlink Escape: SandboxPaths and LocalHostSandbox resolve toRealPath() on reads, writes, directory listings, and cwd resolution, rejecting .. escapes, null bytes, and symlinks pointing outside workspaceRoot.
  2. Fail-Closed OS Probe: LocalOsSandbox defaults to FallbackPolicy.FAIL_CLOSED and networkAccess = false, ensuring missing bwrap or sandbox-exec binaries never degrade silently unless FallbackPolicy.ALLOW_HOST_FALLBACK is explicitly opted into.
  3. Control-Plane Gating: Combining LocalOsSandbox or DockerSandbox with PermissionPolicy.failClosed() and ApprovalDecision.Handler gives defense-in-depth—writes and shell commands are gated both by human approval in the Control Plane and by kernel/container isolation in the Execution Plane.

Released under the MIT License. Built with Java 21 Virtual Threads.