# Sandbox mode

In sandbox mode the host yokai starts a Linux VM and runs a second yokai inside it. The agent and the sensors run in the VM, and your repository is mounted at the same path.

## Why a sandbox

Unattended runs default to `approve-all`: the agent may run any command. A sandbox limits what that can reach and gives every run the same tools, defined in a file your repository owns. The next person who clones the repository can build the same environment.

Sandbox mode is optional. Without it, Yokai runs on your machine with whatever is installed there.

## How it fits together

Two `yokai` processes take part. The one you start on the host is the launcher. The one inside the VM is the real supervisor.

<figure class="diagram" aria-labelledby="dg-sandbox-caption">
<figcaption id="dg-sandbox-caption">The supervisor moves into the VM</figcaption>
<div class="diagram-frame" tabindex="0" role="region" aria-labelledby="dg-sandbox-caption">
<svg viewBox="0 0 560 320" width="560" height="320" role="img" aria-labelledby="dg-sandbox-title dg-sandbox-desc">
<title id="dg-sandbox-title">Host launcher, Linux VM and the shared repository</title>
<desc id="dg-sandbox-desc">The yokai process on the host reads settings from the repository and starts the inner yokai inside a Linux VM run by sbx or container. Inside the VM, the inner yokai talks to the coding agent over ACP and runs the sensors. The agent edits files and the sensors read the tree in the repository, which is mounted into the VM at the same path as on the host.</desc>
<defs><marker id="dg-sandbox-head" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="8" markerHeight="8" markerUnits="userSpaceOnUse" orient="auto-start-reverse"><path class="dg-head" d="M0 0 8 4 0 8z"/></marker></defs>
<rect class="dg-box" x="0" y="56" width="160" height="48" rx="4"/>
<text class="dg-label" x="80" y="80">yokai on host</text>
<path class="dg-line" d="M164 80H212" marker-end="url(#dg-sandbox-head)"/>
<text class="dg-note" x="188" y="64">exec</text>
<rect class="dg-box is-group" x="200" y="8" width="360" height="232" rx="4"/>
<text class="dg-note is-start" x="224" y="32">Linux VM · sbx or container</text>
<rect class="dg-box" x="216" y="56" width="144" height="48" rx="4"/>
<text class="dg-label" x="288" y="80">Inner yokai</text>
<path class="dg-line" d="M364 80H388" marker-start="url(#dg-sandbox-head)" marker-end="url(#dg-sandbox-head)"/>
<rect class="dg-box" x="392" y="56" width="160" height="48" rx="4"/>
<text class="dg-label" x="472" y="80">Coding agent</text>
<path class="dg-line" d="M288 108V148" marker-end="url(#dg-sandbox-head)"/>
<rect class="dg-box" x="216" y="152" width="144" height="48" rx="4"/>
<text class="dg-label" x="288" y="176">Sensors</text>
<path class="dg-line" d="M80 108V260" marker-end="url(#dg-sandbox-head)"/>
<text class="dg-note is-start" x="96" y="184">reads settings</text>
<path class="dg-line" d="M472 108V260" marker-end="url(#dg-sandbox-head)"/>
<text class="dg-note is-end" x="456" y="184">edits</text>
<path class="dg-line" d="M288 204V260" marker-end="url(#dg-sandbox-head)"/>
<rect class="dg-box is-solid" x="0" y="264" width="560" height="48" rx="4"/>
<text class="dg-label is-reverse" x="280" y="288">Repository, mounted at the same path</text>
</svg>
</div>
</figure>

When `sandbox.enabled` is true and you did not pass `--host`, the launcher:

1. Creates the named sandbox from your image if it does not exist. The name defaults to `yokai-<repository folder>`.
2. Copies a Linux build of `yokai` into the VM: `/home/agent/.local/bin/yokai` with Docker Sandboxes, `/usr/local/bin/yokai` with Apple Container.
3. Runs `.koi/sandbox-setup.sh` once, if it exists, and records that it ran.
4. Starts the inner `yokai` with `YOKAI_SANDBOX_INNER=1`, forwarding your launch flags. That variable stops the inner process from starting another sandbox or opening setup.

The repository is mounted read-write at the same absolute path as on the host. Changes the agent makes inside the VM land in your checkout directly. The VM can reach the network.

## Backends

| Backend | Setting | Host tool | Notes |
| --- | --- | --- | --- |
| Docker Sandboxes | `backend: docker` (default) | `sbx`, with Docker Desktop running | Credentials can come from the host through `sbx secret` |
| Apple Container | `backend: apple` | `container`, macOS 26 on Apple silicon | Defaults to 8 GB of memory and 4 CPUs; you sign the agent in inside the VM |

## The image

The image is built from `.koi/Dockerfile.sandbox`, a file in your repository. It must install the coding agent you configured and every tool your sensors call. Yokai does not install agents for you. Setup can propose a Dockerfile; Approve and build saves it, builds the image and replaces the named sandbox.

```yaml
# .koi/yokai.yml
sandbox:
  enabled: true
  backend: docker
  image: daylist-sandbox
```

`.koi/sandbox-setup.sh` is an escape hatch for setup the image cannot do. Prefer the Dockerfile.

## Linux binaries

The VM needs a Linux `yokai` of the same version as the host binary. The launcher finds one in this order:

1. The path in `YOKAI_LINUX_BINARY`, if set.
2. A cached copy for this version at `${KOI_DATA_DIR:-~/.local/share/koi}/bin/yokai-linux-<arch>-v<version>`.
3. A download of `yokai-<arch>-unknown-linux-musl.tar.xz` from the GitHub release `v<version>`, checked against its `.sha256` and then cached.
4. An older local build at `${KOI_DATA_DIR:-~/.local/share/koi}/bin/yokai-linux-<arch>`, only if the download fails.

`<arch>` is the host's architecture, `aarch64` or `x86_64`, because the VM runs the host's. Pinning the version keeps the supervisor inside the VM identical to the one on the host. `yokai doctor` reports which copy the next launch will use. [Sandboxes](/en/docs/yokai/sandbox#linux-binaries) covers the dev-build case.

## Gate review and the environment

Sensors are reviewed in the environment they will run in. The review fingerprint in `.koi/yokai.yml` covers the backend, the image, the Dockerfile and the setup hook. Switching from host to sandbox, or rebuilding the image, needs a new smoke run of the same scripts before Yokai runs a phase.

## What can go wrong

- `opencode not found` inside the VM. The image does not install the agent. Add it to the Dockerfile, rebuild and recreate the sandbox.
- The image changed but the tools did not. An existing sandbox keeps its old filesystem. Recreate it through setup, or remove it with `sbx rm` (Docker) or `container stop` and `container rm` (Apple).
- `no release asset at …` or `could not download …`. The launcher could not fetch the Linux `yokai`. Check the network, or set `YOKAI_LINUX_BINARY` to a Linux build.
- The host login works and the VM login fails. With Apple Container, sign in inside the VM, for example `container exec -it yokai-daylist opencode` and then `/connect`.

To run one launch on the host anyway, use `yokai --host`. [Sandboxes](/en/docs/yokai/sandbox) is the step-by-step guide.
