Skip to content

The sandbox: isolating the agent from your machine ​

Objectives of this module

  • Know what a coding agent can do on your machine
  • Compare the disposable clone, the container and the virtual micro-machine: what each one protects, and what it costs
  • Run Pi in Docker Sandboxes with a versioned kit, ai4dev-pi-kit
  • Leave with a sandbox in which the manipulations of the following modules run unattended

The following modules launch Pi twenty times on the same task without human intervention, give it sub-agents that have a shell, then chain sub-agents into pipelines. Pi has no mechanism for asking your consent before executing a command, and its security documentation says it clearly: the tools read, write and run commands "with the permissions of the pi process", and "Pi does not include a built-in sandbox". Anything you can do from your terminal, the agent can do too: read ~/.ssh, read ~/.pi/agent/auth.json where your API keys are stored, run git push --force, or send the content of a file to any domain with curl.

The natural reaction is to write an instruction, "only modify game/neon.js", "read nothing outside the repository". An instruction is text, and using an LLM is never deterministic, which means you will never have a 100% guarantee that it will be followed. In the module on skills, you will see that a temporary-file cleanup instruction placed in a SKILL.md is followed less than one time in three. So before the first unsupervised run, you need a limit that does not depend on the model's obedience. The sandbox (appelé sandbox) is a deterministic way to ensure that the LLM is in a closed environment whose boundaries are set by you and that the model cannot overstep.

Understanding ​

What does the agent have access to? ​

A code agent running on your machine has access to your files, that is, the repository it works on and, with the same rights, your home directory, where the SSH keys, the model provider tokens and the .env files of your other projects live. The network lets it install any package, run a curl | sh found in a README, or broadcast what it has just read. Finally, it launches processes with your identity, which covers the Docker daemon, the rm command and write access to the remote repository. If you also have sudo privileges on your machine, nothing stops it.

These actions don't even require the model to be wrong. A repository file can contain instructions written for the agent, and that's the role of NÉON's SUPPORT.md, whose text mimics a support procedure but asks you to read the .env and send its contents to an external address: the agent that opens this file to answer a question treats the instruction as if it came from you, and the module on permissions will work on this case. An extension installed from the community registry runs, as the module on Pi reminded, with all your rights. In both cases, the flaw is in the harness, and a guardrail written inside AGENTS.md will not protect you.

Pi's documentation concludes: "For untrusted repositories, generated code you do not intend to monitor closely, or unattended automation, run pi in a contained environment. Use a container, VM, micro-VM, remote sandbox, or policy-controlled sandbox with only the files and credentials required for the task." Our twenty runs on issue #1 are exactly unattended automation. And eventually, we want autonomous agents that can work for hours without us having to monitor them.

Three levels of isolation ​

The cheapest of the three is the disposable clone. The measurement tool in the next module clones NÉON at a tag, into a temporary directory, on each run, which protects the repository's history and working tree at almost no cost. The process still runs under your identity, with your home directory and your network, so a disposable clone only protects the repository. And even then, nothing stops the model from pushing to your remote repository if it has the rights.

One step further, the container runs Pi in a Docker image where only the repository is mounted, putting your home directory out of reach. It shares the host kernel, its network is open by default, and above all the model provider's API key must be added to it so that Pi can call the model, which the Pi page on containerization notes in one sentence: "Provider API keys enter the container". So everything the agent runs has access to this key.

In the same way, you can add a hook to Pi that checks whether each requested bash command is allowed. Several Pi extensions offer this kind of configuration. That is the case, for example, with pi-permission-system, which lets you declare, from a configuration file, which commands are allowed and which are not. It hooks into the tool_call event of the Pi extension API, a hook that intercepts every tool call, every bash command, every MCP call, and every invoked skill before it runs, and compares the request against allow / deny / ask rules written in JSON. It still requires you to configure the permissions yourself; otherwise the read tool can still read your configuration files. You will see how to use it later in this module.

The third level is the policy micro-VM with Docker Sandboxes. Each sandbox has its own kernel behind a hypervisor, all outgoing TCP traffic goes through a proxy on the host that only accepts domains from an allow-list, and the API keys are injected into the HTTP headers by that proxy, so that, to quote the security page, "Credential values never enter the VM". The working directory is mounted in the VM at the same absolute path as on the host. The cost is a six-hundred-and-fifty-megabyte image to download, a daemon to run, an allow-list of domains to maintain. This may seem complicated, but your favorite AI can help you set up this infrastructure easily.

bubblewrap (the bwrap command) on Linux and sandbox-exec on macOS confine a process without an image or a daemon, but they sit at the container level and inherit its two limits: the provider key stays in Pi's environment, where anything the agent runs can read it, and the network is either cut off entirely or left wide open, since filtering by domain requires a proxy on the host, which sandbox-runtime from Anthropic adds.

what is protecteddisposable clonecontainerDocker Sandboxes
your home directorynoyes, if only the repo is mountedyes
outbound networknono by defaultyes, deny by default and allowlist
your API keysnono, they go into the imageyes, only the host proxy sees them

What the sandbox doesn't protect ​

In direct mode, the default one, the agent edits your working tree in place, and the Docker Sandboxes documentation reminds us that it can therefore modify a git hook, a Makefile or a continuous integration configuration, which will run later on the host when you launch them yourself. The sandbox protects the machine while the agent works, and it does not spare you from reviewing the diff.

The balanced network policy, the one that sbx policy init recommends, allows domains with broad wildcards like *.googleapis.com, which cover far more than model APIs. We start from deny-all and then only open the domains that appear in the denial log.

Finally, inside the VM, the agent is an administrator, with passwordless sudo and its own Docker daemon, which we accept, since nothing that happens there ever leaves it and the VM itself is disposable.

Rebuilding ​

We propose two approaches below: using an extension in Pi that adds a hook (which we'll ask you to rebuild in another module) and using Docker Sandboxes. The first solution doesn't require any particular installation on your system and can therefore be used easily for the in-person training. But keep in mind that it has its limits and that it is clearly not sufficient for daily work with agents. For the more adventurous, you can choose the second solution based on Docker Sandboxes.

Installing and configuring pi-permission-system ​

The extension installs with one command, like any package from Pi's directory:

bash
pi install npm:@gotgenes/pi-permission-system

The rules live in a JSON file, read at three scopes: global (~/.pi/agent/extensions/pi-permission-system/config.json), project (.pi/extensions/pi-permission-system/config.json, ignored if the project is not approved), and per-agent, in the YAML header of an agent file, which takes precedence over the first two. For NÉON, a project configuration is enough to stop the most dangerous instruction in SUPPORT.md, since reading a .env is refused by construction:

json
{
  "permission": {
    "*": "allow",
    "path": {
      "*": "allow",
      "*.env": "deny",
      "*.env.*": "deny"
    },
    "bash": {
      "*": "ask",
      "rm -rf *": "deny",
      "sudo *": "ask"
    },
    "external_directory": "ask"
  }
}

The most specific rule wins: bash.* requires confirmation by default, rm -rf * refuses without asking, and a path outside the repository remains subject to confirmation even when path.* allows everything else. A command that the extension's bash parser cannot classify is refused rather than let through, and a path that traverses a symbolic link is resolved before comparison.

Exercise (in class)

Work in a throwaway clone of NÉON, since two of the requests below are destructive. Install the extension, drop the configuration above into .pi/extensions/pi-permission-system/config.json, check that the .env shipped with NÉON is at the root (it contains only fake values), then start Pi and ask it for three things: to read the contents of that .env, to delete the game/ folder with rm -rf, and to run the test suite. The first two requests are refused without Pi consulting you; the third opens a confirmation that you answer yourself.

Next, resubmit the request to read the .env three times in a row, rephrasing it, then explaining to Pi that you are the owner of the file and that you authorize it: the verdict does not change, because it comes from a rule evaluated before the tool call and not from model arbitration.

Finally, remove the path block from the configuration and replace it with the instruction "never read a .env file" in the repository's AGENTS.md, then repeat the same request five times in five different sessions. Count the refusals you get: you then hold your own number on what a text instruction is worth against a guardrail in code. All of this obviously depends on the quality of the model, and the one you use may follow your written rule. Your context, however, is empty here, and the result can change when it is already well filled.

Installing and configuring sbx ​

sbx is the command for using Docker Sandboxes. sbx knows a list of agents it can launch as-is (claude, codex, copilot, cursor, gemini, opencode and a few others), and Pi is not one of them. You therefore have to create a kit: a directory described by a spec.yaml whose kind: sandbox variant defines an agent from scratch, with the image, the startup command, the instructions added to the context file, the keys to inject and the network permissions. Ours is versioned in AI-for-dev/ai4dev-pi-kit and contains only four files.

ai4dev-pi-kit
├── spec.yaml
└── files/home/.pi/agent
    ├── extensions/pi-permission-system/config.json
    ├── models.json
    └── settings.json

The versions cited below are those with which this kit was verified at the time of writing the document: sbx 0.45.1, Docker Engine 29.7.2, Pi 1.1.0.

Install sbx ​

The command-line tool is called sbx. To install it on your OS, simply go to the following page

https://docs.docker.com/ai/sandboxes/install/

Choosing the image ​

Docker publishes docker.io/sbx/pi-image, an image that adds Pi and fd (the file search tool that Pi calls) to the shell-docker template of the sandboxes, which already provides Node 22.22.1, git, rg, python3 and uv. It is rebuilt every night from the latest version of Pi published on npm, so the kit has no Dockerfile to maintain and no image to build, and sbx downloads it from Docker Hub on first launch.

An image that changes every night

The latest tag follows Pi: two sandboxes created a week apart may be running two different versions. For a measurement campaign, where a difference in result must come from the variable being studied and not from an update to the harness, you pin the image by its digest, which docker buildx imagetools inspect docker.io/sbx/pi-image:latest gives, by writing for example image: docker.io/sbx/pi-image@sha256:ae4a64715d2b8ba22f02eccf0d40cc484772661425e57801f242bea9eecb509a in the spec.yaml.

Declare the kit ​

yaml
schemaVersion: "2"
kind: sandbox
name: pi
version: "0.1.0"
displayName: Pi
description: Pi coding agent (pi.dev) in a Docker sandbox.
sourceURL: https://github.com/earendil-works/pi

sandbox:
  image: docker.io/sbx/pi-image:latest
  entrypoint: [pi, -a]

agentInstructions:
  filename: AGENTS.md
  content: |
    ## Sandbox environment

    Tu tournes dans une microVM Docker Sandbox. `sudo` est sans mot de passe,
    Docker est disponible a l'interieur de la VM. Le reseau sortant est filtre
    par une allowlist: un domaine non autorise echoue, ce n'est pas une panne
    reseau. La cle du provider n'est pas dans la VM, seule une sentinelle l'est.

environment:
  variables:
    PI_SKIP_VERSION_CHECK: "1"
    PI_TELEMETRY: "0"
    NODE_OPTIONS: "--disable-warning=UNDICI-EHPA"

credentials:
  - service: ilaas
    description: ILAAS API KEY (llm.ilaas.fr)
    required: true
    apiKey:
      name: ILAAS_API_KEY
      proxyManaged: true
      inject:
        - domain: llm.ilaas.fr
          scheme: bearer

permissions:
  network:
    allow:
      - llm.ilaas.fr
      - github.com
      - raw.githubusercontent.com
      - pypi.org
      - files.pythonhosted.org
      - registry.npmjs.org
      - pi.dev

The sandbox block names the image chosen in the previous step and runs pi -a. The -a option declares the project files as trusted for this run, which answers the question that trust.json raised in the module on Pi: inside the VM, a skill or extension found in the repository can only touch what the VM contains.

agentInstructions adds a few lines to the AGENTS.md the model reads: a denied domain is not a network failure, which keeps it from retrying ten times, and the provider key is not in the VM.

The credentials block declares a key managed by the proxy (proxyManaged: true). Pi finds in ILAAS_API_KEY a sentinel, a dummy value, and the host's proxy replaces it with the real key in the Authorization header of requests to llm.ilaas.fr, and nowhere else.

Under permissions.network, the kit opens over the global policy the model provider, GitHub for cloning NÉON, PyPI for measurement tools and the npm registry for extensions installed with pi install. llm.ilaas.fr must appear in this list in addition to the credentials block, because sbx does not implicitly allow the domains where it injects a key: without this line, the deny-all policy would refuse every model call. The PI_SKIP_VERSION_CHECK and PI_TELEMETRY variables cut some of Pi's startup network operations.

The file files/home/.pi/agent/settings.json, which the kit drops into the agent's home directory, sets the provider, the default model, the reasoning level and the extensions to install. It replaces your host's ~/.pi/agent/settings.json, which is not mounted in the VM. It is fairly simple here and looks like this

json
{
  "defaultProvider": "ilaas",
  "defaultModel": "gemma-4-31b",
  "defaultThinkingLevel": "high",
  "packages": ["npm:@gotgenes/pi-permission-system@40.1.1"]
}

Likewise, the files/home/.pi/agent/models.json file lists the models available in the sandbox.

json
{
    "providers": {
        "ilaas": {
            "baseUrl": "https://llm.ilaas.fr/v1",
            "api": "openai-completions",
            "apiKey": "$ILAAS_API_KEY",
            "models": [
                {
                    "id": "gemma-4-31b",
                    "contextWindow": 128000,
                    "reasoning": true
                },
                {
                    "id": "qwen-3.8-27b",
                    "contextWindow": 256000
                }
            ]
        }
    }
}

Pi installs the packages listed under packages at startup, from registry.npmjs.org, so every sandbox created with this kit has pi-permission-system available without any manual step. The version is pinned, because an extension runs with all of Pi's privileges and a silent package update would change what runs in the VM. To add another extension to the kit, add its source npm:<package>@<version> to that list and recreate the sandbox.

The file files/home/.pi/agent/extensions/pi-permission-system/config.json gives the extension's global policy, which denies reading .env files and rm -rf and allows everything else:

json
{
  "permission": {
    "*": "allow",
    "path": {
      "*": "allow",
      "*.env": "deny",
      "*.env.*": "deny"
    },
    "bash": {
      "*": "allow",
      "rm -rf *": "deny"
    },
    "external_directory": "allow"
  }
}

No ask rule in the kit's policy

An ask rule waits for a response in Pi's interface. In non-interactive mode (pi -p, for example launched with sbx exec), the extension has no one to ask and denies the call with the message "requires approval, but no interactive UI is available". With the classroom exercise's configuration, where bash.* is ask, every bash command would therefore be denied. The kit's policy sticks to allow and deny, and a project that wants confirmations adds them in its own .pi/extensions/pi-permission-system/config.json, which takes precedence over the global policy.

Registering the key ​

ilaas authenticates with an API key. You entrust it to sbx under the name of the service declared by the kit:

bash
sbx secret set ilaas

You must then provide your key. You can then verify that it is properly registered with the command

bash
sbx secret ls

On first launch, sbx asks you to approve the credential binding, the authorization given to a third-party kit to use this secret on the domains it declares. The answer is recorded in ~/.config/sbx/credentials.yaml.

Setting the network policy ​

This setting is global, sbx requires it before the first sandbox, and it is done once and for all:

bash
sbx policy init deny-all

The kit's permissions.network.allow rules apply on top, for its sandboxes only.

Launching ​

bash
git clone https://github.com/AI-for-dev/ai4dev-pi-kit
cd ai4dev-pi-kit
sbx kit validate .
cd /chemin/vers/neon
sbx run /chemin/vers/ai4dev-pi-kit

Exercise (on your own)

Walk through the previous five steps on your machine, from installing sbx to the first sbx run. In the Pi session that opens, ask for the value of the ILAAS_API_KEY variable: you will see the sentinel, not your key. Then run a curl https://example.com: the request fails, because the domain is not on any list. Finally, have a NÉON file modified: the change appears on the host side as soon as Pi has written.

Go back to the host and read sbx policy log, where each denial is recorded with the requested domain. Finish the exercise on pi-permission-system, this time inside the sandbox, where the kit has already installed the extension: ask Pi to read the .env, then to delete game/ with rm -rf, two denials given by the kit's global policy, then drop the classroom exercise configuration into .pi/extensions/pi-permission-system/config.json and check that the test suite now opens a confirmation. This file is stored in the mounted repository and therefore stays on your host. The two guardrails overlap without interfering with each other, and the rm -rf denial keeps all its usefulness, since in direct mode the repository Pi would delete is your host's.

Generalizing ​

A usage boundary that does not depend on obedience. A permission written in text, in an AGENTS.md or a SKILL.md, is a suggestion the model may or may not follow. The permissions module will build guards in code inside the harness, which refuse a tool call before it executes. The sandbox is the outer layer, the one that holds when the harness itself is at fault, because a malicious extension or a booby-trapped file can only damage the VM.

The key stays on the host. The agent does not need to read the key, only that its requests to a specific domain be authenticated, and keeping the key on the host, to place it in the header as the request goes through, removes it from everything the agent can read, execute, or send. The principle holds for any harness, regardless of the tool that implements it.

Deny by default, then open up from the log. A kit's allowlist is not written in advance: you start from denial, work a session, and only add the domains whose refusal actually blocked something, just as the rest of the act decides on measurement rather than intuition.

What you learned

You can tell what an agent has access to on your machine and what each level of isolation protects. You can start Pi in a sandbox and check that it does not see your key, that it cannot reach an unauthorised domain, and that its changes do land in your repository. Finally, you can read the denial log to open only the domains you need.

Going further ​

The threat model ​

Documented incidents ​

Isolation practices and mechanisms ​

Tools ​

© 2026 Loic Gouarin, Max Beligné. This content is published under the CC BY-SA 4.0 license.