Bubblewrapped Workspace
Run your agent’s commands in a Linux bubblewrap (bwrap) wrapper on the host they already run on, over SSH or locally.
While Pydantic AI Harness is on 0.x releases, the API may change between minor releases; when it does, deprecation warnings and release-note migration guidance tell you (or your agent) exactly how to upgrade. See the version policy.
flowchart TB
accTitle: Bubblewrapped Workspace: confine your agent's commands on the host they run on
accDescr: On your machine, the model calls Coder, Shell or FileSystem tools such as run_command. Their ctx.workspace is BubblewrappedWorkspace, which wraps SSHWorkspace and runs commands over ssh -T in a bwrap wrapper on dev@build-box with private /tmp and empty /run. In it, working_dir /srv/app is writable, the host's other files are read-only, and seccomp blocks the network, every socket.
subgraph machine["Your machine"]
model(["model"])
tools["Coder, Shell or<br/>FileSystem tools,<br/>such as run_command"]
bwrap["BubblewrappedWorkspace()<br/>wraps SSHWorkspace"]:::capability
end
sandbox["bwrap wrapper<br/>on dev@build-box<br/>private /tmp, empty /run"]:::external
workdir["working_dir<br/>/srv/app"]:::external
files["the host's<br/>other files"]:::external
sockets["network:<br/>every socket"]:::blocked
model <-->|"tool call, result"| tools
tools -->|"ctx.workspace"| bwrap
bwrap <-->|"commands over ssh -T"| sandbox
sandbox -->|"writable"| workdir
sandbox -->|"read-only"| files
sandbox -.-x|"seccomp blocks"| sockets
classDef capability stroke:#E520E9,stroke-width:2px
classDef external stroke:#0E9C86,stroke-width:2px
classDef blocked stroke:#E5533D,stroke-width:2px,stroke-dasharray:4 3
pip install "pydantic-ai-harness[coder,anthropic]"
uv add "pydantic-ai-harness[coder,anthropic]"
The host the commands run on must run Linux, with the bubblewrap package installed and user namespaces allowed. Otherwise commands raise WorkspaceUnavailableError. The examples also use Coder and an Anthropic model, hence the coder and anthropic extras; swap them for your own tools and model provider.
BubblewrappedWorkspace wraps another workspace capability and runs its commands in bwrap on that workspace’s host. Wrap SSHWorkspace to confine commands on a remote host, or LocalWorkspace to confine them on this one:
from pydantic_ai import Agent
from pydantic_ai_harness import BubblewrappedWorkspace, Coder, SSHWorkspace
agent = Agent(
'anthropic:claude-opus-5-5',
capabilities=[BubblewrappedWorkspace(SSHWorkspace('dev@build-box', working_dir='/srv/app')), Coder()],
)
result = agent.run_sync('Run the test suite and fix the first failure.')
- Commands can write only to the working directory. They see the host’s other files read-only, a private
/tmpand an empty/run. - The file tools of
FileSystemandCoderrun in the wrapper too, so they can’t write outside the working directory either, even through a swapped-in symlink. - There is no network, not even loopback or Unix sockets. A test suite that starts a local server needs
network=True. See Network. - Background jobs keep running after the call and the run that started them, until something stops them. Commands share the host’s process list.
- The run’s ref and
backendare the wrapped workspace’s, so continuing a run works as it does for that workspace.
bwrap gives the wrapper its own user namespace and no Linux capabilities, the kernel’s per-process privileges. /run is empty because host daemons such as Docker listen on sockets there, and a read-only mount doesn’t stop a connection.
When the working directory contains the host account’s home directory, as an SSHWorkspace without a working_dir does, the files the host runs or reads when the next SSH connection logs in are mounted read-only:
~/.sshand~/.pam_environment.- The startup files a login shell runs or commonly sources for a command:
~/.bashrc,~/.bashrc.d,~/.cargo/env,~/.local/bin/env,~/.zshenv(and$ZDOTDIR/.zshenvwhenZDOTDIRpoints elsewhere),~/.cshrc,~/.tcshrcand~/.config/fish.
Missing ones are created empty. If one of them is a symlink, or the home directory isn’t absolute, the wrapper doesn’t start.
Add bwrap arguments with bwrap_args=. They come after the defaults, including the mounts above:
['--bind', path, path]makes another directory writable.['--tmpfs', path]hides one, such as~/.ssh.
By default, a seccomp filter, a kernel-level list of blocked system calls, stops commands from:
- Connecting to, serving or accepting on any socket, including Unix sockets elsewhere on disk and the wrapper’s own loopback.
- Using
ptraceorio_uring.
Stream socket pairs still work, which language runtimes use between their own processes.
Python’s multiprocessing listens on a socket with its forkserver start method, the default on Linux since Python 3.14. Use multiprocessing.get_context('spawn') or 'fork', or pass network=True.
network=True shares the host’s network: commands can reach anything the host can, including services on its loopback, and listen on its ports. There is no seccomp filter then.
The wrapper keeps an agent from accidentally changing the host outside its working directory. It is not a boundary against an agent trying to harm the host user:
-
Commands can read every file the host user can, such as SSH keys and cloud credentials, and show them to the model. Hide those directories with
bwrap_args=['--tmpfs', path]. -
Commands can see the host’s processes and their command lines, including a call’s
envvalues. They can also signal, and so stop, the host user’s other processes. -
Anything a command writes in the working directory, such as Git hooks, a
Makefileor an.envrc, runs outside the wrapper if you later run it there. -
When the working directory contains the home directory, everything in it except the paths listed above stays writable. That includes:
- files only an interactive login runs, such as
~/.profileand~/.bash_profile; - other per-user configuration, including fish configuration when
$XDG_CONFIG_HOMEis set to a non-default location; - files the read-only startup files source, such as a
~/.bashrc.local. Which ones depends on each host, so the wrapper can’t know them all.
This isn’t specific to SSH: with a
LocalWorkspace, the same files run the next time you open a shell. Give the wrapper a project directory as itsworking_dirinstead. - files only an interactive login runs, such as
-
The wrapper trusts the wrapped workspace’s own settings,
bwrap_args, and the host’sbwrapandbase64, taken from aPATHdirectory outside the working directory.
For an agent you don’t trust, give it its own user on the host, or use a VM-backed sandbox such as Smol or BoxLite.
The capability builds a BubblewrapWorkspace around the wrapped workspace. You can also use it directly, around any workspace. It is a WrapperWorkspace:
from pydantic_ai.workspaces import CommandResult, Workspace
from pydantic_ai_harness import BubblewrapWorkspace, SSHWorkspaceBackend
async def run_tests() -> CommandResult:
workspace = BubblewrapWorkspace(Workspace(SSHWorkspaceBackend('dev@build-box', working_dir='/srv/app')))
# `bwrap` runs `make test` on build-box
return await workspace.run(['make', 'test'])
The wrapped workspace must run commands without a controlling terminal. Otherwise, on kernels that permit TIOCSTI, wrapped commands could type input into the host terminal. LocalWorkspaceBackend, SSHWorkspaceBackend, E2BSandboxBackend and ModalSandboxBackend all run commands without one. A custom backend must too.
- On a timeout or cancellation, what stops is up to the wrapped backend:
- Local and SSH stop the command and its foreground descendants. SSH cleanup can fail if the host stalls.
- E2B and Modal do so only when the image has
setsid. - Jobs detached into their own session keep running everywhere.
- The seccomp filter covers x86_64 and aarch64 hosts. A 32-bit program in the wrapper is stopped when it makes a system call.
- A read-only wrapped workspace runs no commands, so its files are read from the host directly.
BubblewrappedWorkspace emits no spans of its own. The run’s workspace is the wrapped one’s, so core’s instrumentation records its pydantic_ai.workspace.provider and pydantic_ai.workspace.id on the agent run span, and each wrapped command runs inside its tool call’s span.
Bases: WrapperCapability[AgentDepsT]
Runs the commands of the wrapped capability’s workspace in a bubblewrap wrapper, on that workspace’s host.
See BubblewrapWorkspace for what the wrapper allows.
from pydantic_ai import Agent
from pydantic_ai_harness import BubblewrappedWorkspace, SSHWorkspace
agent = Agent(
'anthropic:claude-opus-5-5',
capabilities=[BubblewrappedWorkspace(SSHWorkspace('dev@build-box', working_dir='/srv/app'))],
)
Whether commands share the host’s network; without it, a seccomp filter also blocks every socket connection.
Type: bool Default: False
Extra bwrap arguments, placed after the defaults so they can override them.
Type: Sequence[str] Default: ()
Bases: WrapperWorkspace
A Workspace that runs commands in a bubblewrap (bwrap) sandbox.
The wrapped workspace runs bwrap, so the sandbox is on its host: wrap an
SSHWorkspaceBackend to sandbox commands on the
remote host. bwrap must be installed there (Linux only).
The wrapped workspace must run commands without a controlling terminal. This wrapper omits
bwrap --new-session so the backend can stop foreground descendants on timeout or cancellation;
with a controlling terminal, kernels permitting TIOCSTI could let sandboxed commands inject input
into the host terminal. LocalWorkspaceBackend starts a new session and SSHWorkspaceBackend uses
ssh -T. E2BSandboxBackend and ModalSandboxBackend run commands through their providers’ non-PTY
command APIs, so no terminal is attached, with or without setsid in the image. All four satisfy this
requirement; custom backends must do so too.
Commands see the host read-only, with an empty /run, a private /tmp, and no network, and can only
write to the working directory. Without the network, a seccomp filter also stops them from connecting to
or serving any socket, including the host’s Unix sockets. They share the host’s processes, so a detached
command keeps running after the call that started it, and they can signal the host user’s other processes.
bwrap and, without the network, base64 are taken from a PATH directory outside the working
directory, so a command cannot replace the next launch by writing there. When the working directory
contains files the next SSH login runs on the host, a fixed list of them, such as ~/.ssh/rc and
~/.bashrc, is bind-mounted read-only (and created if missing). The rest of the home directory stays
writable, including files they source, so give the wrapper a project working directory rather than one
containing $HOME.
File methods run in the sandbox too, as shell commands, so they see what commands see and can’t be tricked into writing outside it through a symlink. Only when the wrapped workspace is read-only (and so runs no commands) do its file methods read the host directly.
The workspace whose host runs the sandbox.
network : bool Default: False
Whether commands share the host’s network; without it, a seccomp filter also blocks every socket connection.
Extra bwrap arguments, placed after the defaults so they can override them,
such as ['--bind', path, path] for another writable directory or ['--tmpfs', secrets_dir]
to hide one.
def durable_policy() -> tuple[object, ...]
(network, bwrap_args), so a durable unit cannot rebuild this sandbox with another.