Filesystem and process-execution capabilities exposed to tools.
A sandbox is a runtime-swappable backend for the operations a tool needs to reach the outside world: read/write/edit files, run commands, glob, and grep. Built-in sandboxes:
Condukt.Sandbox.Localruns against the host filesystem and spawns real processes viaMuonTrap.Condukt.Sandbox.Virtual(in:condukt_bashkit_nif) runs inside an in-memory virtual filesystem and a Rust-implemented bash interpreter, with no host process spawning by default.Condukt.Sandbox.Microsandboxboots amicrosandboxmicroVM, bind-mounts selected host directories into it, and runs commands inside the guest.
Why a sandbox
Built-in tools (Condukt.Tools.Read, Tools.Write, Tools.Edit,
Tools.Bash, Tools.Glob, Tools.Grep) are sandbox-agnostic: they declare
one tool name and JSON schema to the LLM and route every primitive call
through the active sandbox. This means the same agent definition can run
against the host filesystem in development and against an isolated virtual
filesystem in production by changing one option at start_link/1.
Configuring
Pass :sandbox at session start (or as the agent module's sandbox/0
callback). Accepted forms:
# module only — uses defaults (Local resolves :cwd to File.cwd!())
sandbox: Condukt.Sandbox.Local
# module + init opts
sandbox: {Condukt.Sandbox.Local, cwd: "/path/to/project"}
# already-initialized struct (advanced)
sandbox: Condukt.Sandbox.new(Condukt.Sandbox.Local, cwd: "/tmp")When :sandbox is omitted, sessions default to
{Condukt.Sandbox.Local, cwd: <:cwd opt or File.cwd!()>} so existing
agents continue to behave as they did before sandboxes existed.
Writing a custom sandbox
Implement the Condukt.Sandbox behaviour. init/1 builds the per-session
state; shutdown/1 releases it; the rest are I/O primitives. glob/3,
grep/3, list_dir/2, mount/3, and the process callbacks are optional. Tools dispatch
through this module's facade (Condukt.Sandbox.read/2, .exec/3, etc.) so
custom sandboxes work with every built-in tool automatically.
Processes that outlive their caller
The optional start_process/4, read_process/4, and stop_process/2
run a command under a stable id, apart from the BEAM process that started
it: a caller reads its output from an offset as it runs, learns its exit
status, and kills it, and anyone who knows the id finds it again.
Local, Kubernetes, and Microsandbox implement them;
Condukt.Tools.Bash uses them when they are there and exec/3 otherwise.
Tool authoring rule
If your tool reads or writes files, or runs subprocesses, route through the
Condukt.Sandbox.* facade rather than calling File.*, System.cmd/3, or
MuonTrap.cmd/3 directly. Direct calls bypass the sandbox and break the
ability to swap one in. Tools that touch unrelated systems (HTTP APIs,
databases, in-process state) have nothing to sandbox and are unaffected.
Summary
Callbacks
Tells the sandbox whether a session needs it.
Returns the effective working directory inside the sandbox.
Replaces the unique occurrence of old_text with new_text.
Returns the count of pre-edit occurrences alongside the post-edit content
so callers can decide how to surface ambiguity or no-op edits.
Runs a shell command. Options
Makes port inside the sandbox reachable from outside it and returns the
URL, as a supervised service's portal. Options are the sandbox's own, such
as who may open the URL.
Returns paths matching pattern. Options
Searches file contents for pattern (regex). Options
Initializes per-session sandbox state.
Lists the entries of the directory path, dotfiles included, each with
whether it is a directory or anything else. Whether a symlink to a
directory counts as one is the sandbox's choice: Local follows it, and a
sandbox that confines files to a checkout can list it as a file. Returns
{:error, :enoent} for a missing path and {:error, :enotdir} for one
that is not a directory.
Mounts a host directory into the sandbox at vfs_path. Sandboxes that have
no separate VFS (like Local) should return {:error, :not_supported}.
Reads a file's raw bytes.
Reads a process's output from byte offset, and its status.
Releases any resources held by the sandbox state.
Saves the sandbox's disks so a later sandbox can boot from them, and
returns an id that the sandbox's init/1 takes as :snapshot. See
Condukt.Sandbox.Setup for the preparation phase that uses it.
Starts command as a process named id that runs on its own, apart from
the BEAM process that started it, and returns at once.
Kills a process if it still runs, with the processes it started, and
discards its log and status. Stopping an unknown id returns :ok.
Asks the sandbox to deliver a process's end itself, as
Condukt.signal(session_id, signal, key, status), where status is what
read_process/4 reports once it is no longer :running.
Writes raw bytes to a file, creating parent directories as needed.
Appends data to the stdin of a process started with stdin: true.
Functions
Tells the sandbox whether a session needs it. See activity/2.
Streams the directory path out of the sandbox as a tar archive, in
chunks, without holding it in memory. See Condukt.Sandbox.Transfer.
Exposes a port in the sandbox and returns its URL. See expose_port/3.
Unpacks a tar archive, an enumerable of chunks such as export/3 returns,
into the directory path in the sandbox. See Condukt.Sandbox.Transfer.
Lists a directory's entries. See list_dir/2. Sandboxes that do not
implement it list through exec/3 with a POSIX shell script.
Constructs a sandbox handle by initializing module with opts.
Whether the sandbox runs processes apart from their caller, through
start_process/4, read_process/4, and stop_process/2.
Reads a process's output from offset, and its status. See read_process/4.
Normalizes a user-supplied :sandbox option into a t().
Releases the sandbox state.
Saves the sandbox's disks for later sandboxes to boot from. See snapshot/2.
Starts a process named id. See start_process/4.
Kills a process and discards its log. See stop_process/2.
Whether the sandbox implements the optional callback name/arity.
Asks the sandbox to deliver a process's end. See watch_process/3.
Writes to a process's stdin. See write_process/4.
Callbacks
Tells the sandbox whether a session needs it.
A session calls it with :active when a turn starts, again every
:lease_interval milliseconds while the turn runs or is parked, and with
:idle when the turn ends, however it ends. A sandbox that bills, pauses,
or reaps idle machines maps these onto its lease: Tuist's onto a residency,
Kubernetes onto its pod heartbeat. Errors are logged and otherwise
ignored; the turn goes on.
Returns the effective working directory inside the sandbox.
Used by project-instruction discovery and any caller that needs to address files relative to the sandbox's root without knowing which backend is in use.
@callback edit_file( state :: term(), path :: binary(), old_text :: binary(), new_text :: binary() ) :: {:ok, %{occurrences: non_neg_integer(), content: binary()}} | {:error, term()}
Replaces the unique occurrence of old_text with new_text.
Returns the count of pre-edit occurrences alongside the post-edit content
so callers can decide how to surface ambiguity or no-op edits.
@callback exec(state :: term(), command :: binary(), opts :: keyword()) :: {:ok, %{output: binary(), exit_code: integer()}} | {:error, :timeout | term()}
Runs a shell command. Options:
:cwd— working directory:env— list of{key, value}strings to layer onto the base env:timeout— milliseconds before the command is killed
@callback expose_port(state :: term(), port :: pos_integer(), opts :: keyword()) :: {:ok, url :: binary()} | {:error, term()}
Makes port inside the sandbox reachable from outside it and returns the
URL, as a supervised service's portal. Options are the sandbox's own, such
as who may open the URL.
@callback glob(state :: term(), pattern :: binary(), opts :: keyword()) :: {:ok, [binary()]} | {:error, term()}
Returns paths matching pattern. Options:
:cwd— base directory; pattern is resolved relative to it:limit— maximum number of paths to return
@callback grep(state :: term(), pattern :: binary(), opts :: keyword()) :: {:ok, [%{path: binary(), line_number: pos_integer(), line: binary()}]} | {:error, term()}
Searches file contents for pattern (regex). Options:
:path— directory to search (default: cwd):glob— glob filter applied to file paths relative to:path; one without a/, such as*.ex, matches file names at any depth:case_sensitive— defaults to true:limit— maximum matches to return
Initializes per-session sandbox state.
@callback list_dir(state :: term(), path :: binary()) :: {:ok, [%{name: binary(), type: :directory | :file}]} | {:error, term()}
Lists the entries of the directory path, dotfiles included, each with
whether it is a directory or anything else. Whether a symlink to a
directory counts as one is the sandbox's choice: Local follows it, and a
sandbox that confines files to a checkout can list it as a file. Returns
{:error, :enoent} for a missing path and {:error, :enotdir} for one
that is not a directory.
Optional: list_dir/2 falls back to a shell listing through exec/3.
Implement it when the sandbox confines file access more than its shell.
@callback mount(state :: term(), host_path :: binary(), vfs_path :: binary()) :: :ok | {:error, :not_supported | term()}
Mounts a host directory into the sandbox at vfs_path. Sandboxes that have
no separate VFS (like Local) should return {:error, :not_supported}.
Reads a file's raw bytes.
@callback read_process( state :: term(), id :: binary(), offset :: non_neg_integer(), opts :: keyword() ) :: {:ok, %{ output: binary(), offset: non_neg_integer(), size: non_neg_integer(), status: :running | {:exited, integer() | nil} | :timeout }} | {:error, :not_found | term()}
Reads a process's output from byte offset, and its status.
With :wait (milliseconds), waits up to that long for output past
offset or the process's end before answering, so a follower does not
poll; a sandbox may answer sooner.
Returns at most :max_bytes (default 64 KiB) as output, the offset to
read from next, the size of the whole log so far, and status:
:running{:exited, exit_code}—exit_codeisnilwhen the process ended without the sandbox recording how, such as when its container restarted:timeout— the sandbox killed it after its:timeout
The status is taken before the output, so once it is not :running, the
log is complete and reading up to size gets all of it. Returns
{:error, :not_found} for an id the sandbox holds nothing for.
@callback shutdown(state :: term()) :: :ok
Releases any resources held by the sandbox state.
@callback snapshot(state :: term(), opts :: keyword()) :: {:ok, snapshot_id :: binary()} | {:error, term()}
Saves the sandbox's disks so a later sandbox can boot from them, and
returns an id that the sandbox's init/1 takes as :snapshot. See
Condukt.Sandbox.Setup for the preparation phase that uses it.
@callback start_process( state :: term(), id :: binary(), command :: binary(), opts :: keyword() ) :: :ok | {:error, :already_started | term()}
Starts command as a process named id that runs on its own, apart from
the BEAM process that started it, and returns at once.
id is any string. Starting is idempotent on it: when the sandbox already
holds a process under id, running or exited, it returns
{:error, :already_started} and leaves it alone, so a caller that lost
track of a process, such as a restarted session, finds it again by starting
it again. Output goes to a log the sandbox keeps, combined stdout and
stderr, until stop_process/2. Options are those of exec/3:
:cwd— working directory:env— list of{key, value}strings to layer onto the base env:timeout— milliseconds after which the sandbox kills the command and reports its status as:timeout
and, where the sandbox supports them:
:stdin—truekeeps the command's stdin open forwrite_process/4; otherwise it reads an empty stdin:pty—trueruns the command on a pseudo-terminal, as an interactive shell for a person needs (seeCondukt.Sandbox.Terminal)
Kills a process if it still runs, with the processes it started, and
discards its log and status. Stopping an unknown id returns :ok.
@callback watch_process( state :: term(), id :: binary(), target :: %{session_id: binary(), signal: module(), key: binary()} ) :: :ok | {:error, term()}
Asks the sandbox to deliver a process's end itself, as
Condukt.signal(session_id, signal, key, status), where status is what
read_process/4 reports once it is no longer :running.
Return :ok only when the delivery survives the BEAM process, and the
node, that asked for it, such as when the sandbox's own control plane
watches the process. A parked Condukt.Tools.Bash call then wakes even
after a deploy restarted the node its watcher ran on. A process that has
already ended is delivered at once.
@callback write_file(state :: term(), path :: binary(), content :: binary()) :: :ok | {:error, term()}
Writes raw bytes to a file, creating parent directories as needed.
@callback write_process( state :: term(), id :: binary(), data :: binary(), opts :: keyword() ) :: :ok | {:error, :not_found | :stdin_closed | :offset_mismatch | term()}
Appends data to the stdin of a process started with stdin: true.
Options:
:close— end its stdin after this write:offset— the bytes stdin held before this write. With it, a write sent again, such as after a lost reply, lands once: when stdin already ends with it the call returns:okwithout appending, and when stdin holds anything else thanoffsetbytes it returns{:error, :offset_mismatch}.
Returns {:error, :stdin_closed} once stdin ended, and
{:error, :not_found} for an unknown id.
Functions
Tells the sandbox whether a session needs it. See activity/2.
Streams the directory path out of the sandbox as a tar archive, in
chunks, without holding it in memory. See Condukt.Sandbox.Transfer.
Exposes a port in the sandbox and returns its URL. See expose_port/3.
Unpacks a tar archive, an enumerable of chunks such as export/3 returns,
into the directory path in the sandbox. See Condukt.Sandbox.Transfer.
Lists a directory's entries. See list_dir/2. Sandboxes that do not
implement it list through exec/3 with a POSIX shell script.
Constructs a sandbox handle by initializing module with opts.
Returns {:ok, sandbox} so callers can surface init failures (an in-memory
sandbox that fails to allocate, a virtual sandbox whose NIF refused to
start, etc).
Whether the sandbox runs processes apart from their caller, through
start_process/4, read_process/4, and stop_process/2.
Reads a process's output from offset, and its status. See read_process/4.
Normalizes a user-supplied :sandbox option into a t().
Accepts an already-built struct, a bare module, or a {module, opts} tuple.
Returns {:ok, sandbox} or {:error, reason}.
Releases the sandbox state.
Saves the sandbox's disks for later sandboxes to boot from. See snapshot/2.
Starts a process named id. See start_process/4.
Kills a process and discards its log. See stop_process/2.
Whether the sandbox implements the optional callback name/arity.
Asks the sandbox to deliver a process's end. See watch_process/3.
Writes to a process's stdin. See write_process/4.