Least-privilege sandboxing for a single agent session, at the Python
process level: declare the paths, commands, and hosts a session is allowed
to touch, then run arbitrary code inside with Cage(scope): — anything
outside that scope raises CageViolation instead of silently happening.
Cage patches three standard-library entry points for the lifetime of the
with block and restores them unconditionally on exit:
PathGuard— patchesbuiltins.open; blocks any path not matching a grantedPATHpattern.CommandGuard— patchessubprocess.Popen.__init__(whichrun,call, andcheck_callall go through); blocks any command whose basename isn't a grantedCOMMANDpattern.NetworkGuard— patchessocket.socket.connect; blocks any outbound connection to a host that isn't a grantedHOSTpattern.
This is process-level, monkeypatch-based enforcement, not a kernel sandbox — it stops an agent's own Python-level actions (and anything called through the patched stdlib entry points) from reaching outside its declared scope. It is not a substitute for OS-level isolation (containers, VMs, seccomp) if you're running code you don't trust at all; it's the harness-side least-privilege layer that sits in front of that.
pip install -e ".[dev]"
pytestfrom agentcage import Cage, Scope
scope = Scope().paths("/repo/workdir/*").commands("pytest", "git").hosts("127.0.0.1")
with Cage(scope):
... # only the granted paths/commands/hosts are reachable hereMIT