aider icon indicating copy to clipboard operation
aider copied to clipboard

Feature: Sandboxed test execution via exec-sandbox (QEMU microVMs) for safe test-before-commit workflows

Open clemlesne opened this issue 9 months ago • 3 comments

Context

Aider's /test and /run commands execute shell commands directly on the host via subprocess. When --auto-test is enabled, every AI edit triggers the configured --test-cmd on the host filesystem with no isolation boundary. This is fine when the user is watching, but becomes a concern as aider moves toward more autonomous agent workflows (see #3009, #648 -- both now closed) where the AI generates code and runs it with less human oversight.

Issue #4679 discusses sandboxing via bubblewrap/seatbelt (syscall filtering). A community contributor has also prototyped bubblewrap-based sandboxing (see the bubbleproc discussion in #4679). This proposal is complementary -- it focuses specifically on running tests in a disposable VM before committing, so that untrusted generated code never touches the host. VM-level isolation and syscall filtering serve different points on the security/performance tradeoff -- both could be useful.

Proposed workflow: generate, sandbox-test, then commit

1. LLM generates code edits (as today)
2. Edited files are copied into a fresh QEMU microVM
3. Test suite runs inside the VM (~1-2ms warm pool allocation)
4. If tests pass --> apply edits to the host repo and commit
5. If tests fail --> feed errors back to the LLM for another attempt
   (the host repo was never modified)

This is a meaningful improvement over the current flow where edits are applied to the host before tests run, requiring /undo to revert on failure. It does introduce new complexity (QEMU dependency, file sync overhead), but allows tests to run in isolation before modifying the host repo.

What is exec-sandbox

exec-sandbox is an open-source (Apache-2.0) Python library that runs code in dedicated QEMU microVMs with hardware virtualization (KVM on Linux, HVF on macOS).

  • Hardware VM isolation -- each execution gets its own kernel, not a shared-kernel container
  • 1-2ms warm start from pre-booted VM pool, ~100ms from snapshot
  • Python, JavaScript, and Shell execution with stateful sessions
  • File I/O -- upload project files into the VM, download artifacts back
  • macOS + Linux -- develops locally on Mac (HVF), deploys on Linux (KVM)
  • Self-hosted -- no cloud account, no API key, no data leaving your machine
  • pip install exec-sandbox -- pure Python host side, same ecosystem as aider

Integration sketch

Since both projects are Python, integration is direct. A minimal proof of concept for the --test-cmd path:

from exec_sandbox import Scheduler

async def run_tests_in_sandbox(changed_files: dict[str, bytes], test_cmd: str) -> tuple[bool, str]:
    """Run test_cmd in a sandbox with the proposed file changes."""
    async with Scheduler() as scheduler:
        async with await scheduler.session(language="raw") as session:
            # Upload changed files into the VM
            for path, content in changed_files.items():
                await session.write_file(path, content)

            # Run the test command
            result = await session.exec(test_cmd)

            return result.exit_code == 0, result.stdout + result.stderr

This could be exposed as:

  • A new --sandbox-test flag that wraps the existing --test-cmd in VM isolation
  • An opt-in mode where edits are staged in the sandbox first and only applied to the host on test success

Why this matters

  1. Safety for autonomous loops. As aider gains more autonomous capabilities (--yes, auto-test, agent mode), the blast radius of a bad code generation grows. A sandbox bounds that risk.
  2. Faster iteration. Failed tests never touch the host repo, so there is no /undo step and no dirty-file commit dance.
  3. No infrastructure. Unlike Docker or cloud sandboxes, exec-sandbox is a single pip install and uses the same QEMU that ships with Homebrew/apt. No daemon, no account.

Links

  • exec-sandbox GitHub
  • exec-sandbox on PyPI
  • Related: #4679 (bubblewrap/seatbelt sandboxing -- complementary, different isolation model)
  • Related: #3009 (automatic command execution, closed), #648 (autonomous command execution, closed)

clemlesne avatar Mar 04 '26 15:03 clemlesne

This seems like a very good approach. I'm not sure that I really need the extra isolation of hardware VMs but it is definitely a nice touch. One issue would be ensuring that the all of the tools needed to execute and run a project would be available. But it would be nice extra safety for projects that can easily support it.

On the other hand I absolutely love the only pulling the patch once it is finished. Giving access to the host project directory including .git, built artifacts, dependencies and everything else to the agent is a big attack surface. For example just editing .git/config is possible remote code execution on the next Git command you run outside of the sandbox. It definitely seems ideal to me that the agent runs in a sandbox, and only a patch is sent back to the host. Obviously the patch can still contain exploits but it is going to show up in git diff (unlike edits to .git/config, node_modules or other untracked or ignored files) so you can fully review before running anything untrusted.

kevincox avatar Mar 22 '26 14:03 kevincox

For cases where full QEMU microVM isolation is more than needed, sandlock offers a lighter-weight alternative using Landlock, seccomp-bpf, and user namespaces.

exec-sandbox (QEMU) sandlock
Isolation Hardware (KVM/HVF) OS (kernel LSM + seccomp)
Startup ~200ms cold ~20ms
Dependencies QEMU + VM images Zero (kernel features)
Platform Linux + macOS Linux only
Install Separate service pip install sandlock

For Aider's typical use case (sandboxing /run, /test, /lint commands within a developer's local repo), OS-level isolation via Landlock filesystem whitelisting + seccomp syscall filtering provides strong defense-in-depth without the overhead of booting a VM per command execution.

Both approaches could coexist: sandlock as the lightweight default on Linux, exec-sandbox for higher-assurance scenarios.

Disclosure: I'm the author of sandlock.

congwang-mk avatar Mar 27 '26 23:03 congwang-mk