CodeBuild a WorkspaceSandbox Runtime

Sandbox Runtime

Your custom tools run inside a per-workspace-version MicroVM provisioned by Bahulam. This page covers what that means for your code: resource limits, network policy, dispatch modes, and dev-mode behavior.


What Bahulam Provides

For every unique (workspace_id, version):

  1. Base image bake — golden Python 3.11 image + any extra_packages you declare + your custom_tools/ tree, materialized to an Azure Blob cache the first time your workspace-version is loaded.
  2. Warm MicroVM per user session — sub-second boot from snapshot when a user starts a workspace turn. Reused across tool calls in that session. Reaped when idle.
  3. Network policy — outbound HTTP restricted to the hosts you declared in custom_tools.network.egress. Everything else blackholed.
  4. Resource budget enforcement — cgroups + Firecracker limits cap CPU, memory, wall time per tool call.
  5. Secret injection — connection credentials (Power BI, Slack, GitHub OAuth) resolved by the Bahulam broker and passed to your tool at call time via ctx. Never hardcode credentials in your code.

Resource Limits

Declare in workspace.yaml:

custom_tools:
  limits:
    cpu: "0.5"                # ≤ 2.0
    memory_mb: 512            # ≤ 2048
    wall_seconds: 30          # ≤ 300

Ceilings enforced at admission:

FieldCeilingRationale
cpu2.0 coresTool calls are per-turn; long compute belongs in a job.
memory_mb2048Fits within a MicroVM’s default snapshot size.
wall_seconds300LLM turns time out anyway; long tools block the loop.

Exceeding any is a fatal admission error. If your tool genuinely needs more, split it — kick off async work through a service tool and poll for results across turns.


Network Egress

Empty egress = no outbound calls at all.

custom_tools:
  network:
    egress: []                # locked-down default

Declare explicitly per host:

custom_tools:
  network:
    egress:
      - api.example.com
      - hooks.slack.com

Wildcards are not supported — enumerate the exact hosts. Real enforcement is at the MicroVM network policy layer (Firecracker + iptables). Your tool’s httpx.get("https://blocked.com") will fail with a connection error at runtime.

The admission validator also does a static best-effort check: if it sees import httpx / requests / urllib in your code but egress is empty, it emits a warning (not fatal).


Two Executor Modes

SANDBOX_EXECUTOR env var (Bahulam-side) picks the dispatch strategy:

ValueBehaviorWhen
mockIn-process, returns a marker dict. Your code is NOT called.Dev, tests, CI validation.
microvmFull MicroVM dispatch. Your code runs in isolation.Production.
noopAlias for mock.Explicit test isolation.

As a workspace author, this is transparent. Your SandboxTool subclass is identical either way. The executor decision is Bahulam-operator business.

What Mock Looks Like

Set SANDBOX_EXECUTOR=mock (or unset — mock is default in dev):

result = await MyTool().execute({"person": "alice"})
# Returns:
{
    "sandbox_executor": "mock",
    "workspace_id": "community/example/hello-world",
    "custom_tool_path": "tools/hello_tool.py",
    "tool_name": "bahulam.community.example.hello_greet",
    "args": {"person": "alice"},
    "message": "MockSandboxExecutor did not execute the tool...",
}

The pipeline (registry → dispatch → executor → return) works end-to-end. Only the actual tool body is not invoked. Useful for verifying:

  • Registration fires.
  • YAML admission passes.
  • The agent picks up the tool in its available list.
  • Downstream code that consumes tool results handles the shape.

What MicroVM Looks Like

Set SANDBOX_EXECUTOR=microvm (production). Same call shape:

result = await MyTool().execute({"person": "alice"})
# Returns:
{"greeting": "Hello, alice!"}

Your _inside_sandbox(person) body actually ran, in a fresh Firecracker VM booted from your workspace-version’s cached image. Bahulam meters the call for community credits (PRD-086 §7.4).


Approval Policy Gate

Every SandboxTool.execute() runs ApprovalPolicy.check(tool_name, args) BEFORE dispatch. Declare per-tool policy in your workspace.yaml:

approvals:
  default: never
  overrides:
    - tool: bahulam.community.<handle>.destructive_action
      policy: always

Result codes returned when denied:

{
    "error": "denied_by_policy",
    "tool": "bahulam.community.<handle>.destructive_action",
    "message": "ApprovalPolicy denied dispatch..."
}

For an unattended channel (batch job, cron), always fails closed. For an attended channel (CLI, CDE with HITL), the channel prompts the user before the tool runs.


Connections Available at Dispatch

If your workspace declares connections_required, the broker resolves credentials Bahulam-side and makes them available to your tool at run time. Details in Connections & Auth.

Your tool never handles the OAuth handshake or credential storage. That’s the moat: your code is portable, but running it usefully requires Bahulam’s brokered access.


What You Should NOT Do In Your Tool

  • Store credentials in environment variables, config files, or code. Bahulam injects them per-call.
  • Persist state to the sandbox filesystem across calls. Assume the VM is ephemeral and can be reaped between tool invocations. If you need persistence, write to the workspace’s declared Postgres/Blob connections.
  • subprocess.run shell commands that expect specific tools installed. Only the base image + your extra_packages are available.
  • Long-running processes. Tools return per-call; you can’t leave a daemon running.
  • Access the internet outside your declared egress list. It won’t work.
  • Try to escape the sandbox. Detection triggers automatic workspace suspension.

Debugging in Prod

MicroVM tool calls emit structured logs Bahulam-side:

  • Tool name, args, session_id, workspace_id, workspace_version
  • Duration, exit status, bytes returned
  • Egress attempted vs allowed (for policy audits)
  • Resource usage vs ceilings

If your tool fails in prod but works locally with the mock executor, first suspects:

  1. Undeclared egress — check declared hosts vs your code.
  2. Missing package in extra_packages.
  3. Resource ceiling — memory OOM or wall-time timeout.
  4. Connection not attached — user hasn’t OAuth’d to the required service.

The MicroVM’s stderr comes back as a sandbox_stderr field on the error result so you can debug remotely.