Server sandboxes¶
For administrators who want model-written code to run on the server
Server sandboxes run code in isolated containers on a host you control, instead of in the user's browser. They are off by default. This page is the operator's view: how to switch them on, what they cost, and what protects the host. The design is in ADR 0115.
What runs on it today
The model's execute_code tool can run in a server sandbox (below). Python code blocks in the
chat still run in the user's browser (Pyodide), whatever you set here. Hosted agent machines
(a later phase) and the egress proxy they need are not built; a request for one is refused.
Run execute_code on the server¶
Two settings, both off by default: the sandboxes themselves (SANDBOX, below) and where the
model's code runs (CHAT_CODE_EXECUTION).
--code-execution browser|server|off sets CHAT_CODE_EXECUTION. server needs SANDBOX to be
local or remote; with SANDBOX=off, or while the manager is not answering, the chat runs the
tool in the browser instead, automatically, and logs it ([code-execution] ... code runs in the
person's browser, once when it starts and again when it recovers). A call whose sandbox cannot be
leased is handed to the browser too, before any code has run. After the code has started there is
no fallback: a failure is reported as the run's error.
The model sees the same execute_code tool, with the same argument and the same result format.
What differs is the engine, which the tool's description says:
- Preinstalled stack, no network. Python 3.13 with numpy, pandas, scipy, scikit-learn,
matplotlib, seaborn, sympy, Pillow, lxml, python-docx, openpyxl, XlsxWriter, python-pptx, pypdf and
reportlab (the image
cerea-sandbox-python, versions pinned insandbox/exec-image/requirements.txt). Nothing can be installed or downloaded. - State lasts. One sandbox per conversation: variables, imports and files stay between calls.
The manager releases it after about twenty idle minutes (
SANDBOX_IDLE_SECONDS); the next call makes a new one, and the result tells the model its earlier state is gone. - Files in. The conversation's attachments are copied to
/mnt/data/<name>on demand, by the same plan as the browser (same names, the extracted text beside each file as<name>.md, the same size caps). A file a privacy rule withheld is never copied: it is neither planned nor readable. This includes an image the gateway refused for vision under a privacy rule. Its owner can still save it with the file card's "Download (withheld)" button, a one-time link the sandbox cannot reach. Files the document tools produced are not copied yet. - Reading a big file back. A run's stdout is cut at 8,000 characters, the same as the browser's; the
cut says how many characters there are in total and suggests slices (
print(text[6000:12000])). To send a file to a connector the model does not read it back at all: it passes a{"$file": "<name>"}reference (see Sending a file to a connector). - Files out. Files the code writes under
/mnt/output/come back as the conversation's files and show as download cards, kept as long as any deliverable (CHAT_FILE_RETENTION_DAYS). At most 20 files and 8 MB each per run; a figure drawn with matplotlib is saved asfigure-N.pngfor the model, as in the browser. An image the code saves is shown to the user, not to the model: when the model can see images it has theview_imagetool (the file at/mnt/output/<name>or/mnt/data/<name>, with an optionalcropandrotate) to look at it, for example to zoom into a region of a photographed page. - Rich output. Results come back as text, like the browser's: a DataFrame prints as a table. (A
full Jupyter kernel, with inline HTML tables and widgets, is not used: the exec daemon in the image,
sandbox/exec-image/execd.py, is a small standard-library program that keeps one interpreter and returnsstdout,stderr, the last value, the error and the files. It is the seam to swap if inline HTML is ever wanted.) - A run that cannot be stopped. The daemon stops a run at its limit and replaces its interpreter
(nothing survives, not even a changed module). A C call that holds the interpreter (a huge
sum(range(...))) defeats the daemon's own timers, so the client in the sandbox gives up after the limit plus a grace and kills the daemon's process group; the chat then releases that sandbox, and the next call gets a fresh one and the model is told its state is gone. - Fallback is told. When a call runs in the browser because a sandbox could not be leased (the limits below, or the manager down), the model's result says so. A scheduled run, which has no browser to fall back to, fails with an "unavailable" result instead.
- Limits. One run is stopped after the configured limit (
CHAT_CODE_RUN_TIMEOUT_SECONDS, 60 s by default); a stopped run restarts the interpreter and the model is told. Memory, CPU, processes, workspace and the other limits are the sandbox's (see Sizing); one sandbox is leased per conversation, soSANDBOX_MAX_LEASES_PER_OWNERis also the number of conversations a user can run code in at once.
What users see. The same code card as before: it shows "Running code" while the sandbox works (no "keep this page open" hint, because nothing runs in their page), then the output and the file cards. A turn does not need the page to stay open, and a scheduled run can use the tool.
The sandbox lease is kept in the chat process's memory. With several chat replicas, a conversation's next call may land on another one, which leases a second sandbox; the first is released by the idle timeout. Run a single replica, or accept the extra start-up.
The switch¶
One setting, SANDBOX, in .env. ./configure writes the rest.
SANDBOX |
What runs | You give |
|---|---|---|
off (default) |
nothing | nothing |
local |
the manager and the socket proxy, in this compose (the sandbox profile) |
nothing: ./configure mints the token and fills in the address |
remote |
nothing here; the manager runs on another host | its URL and token |
./configure --sandbox local
./configure --sandbox remote --sandbox-url https://sandbox.example.org --sandbox-token-env SANDBOX_MANAGER_TOKEN
./configure --sandbox off
docker compose up -d
localadds thesandboxprofile toCOMPOSE_PROFILES, setsSANDBOX_MANAGER_URL=http://sandbox-manager:8080and mintsSANDBOX_MANAGER_TOKEN.remoteremoves the profile and stores the URL and token you give. The token comes from an environment variable, never from the command line (which other users can read).offremoves the profile, the URL and the token.localneeds gVisor../configureasks Docker (docker info) whether therunscruntime is registered. If it is not, it says so, leavesSANDBOX=offand carries on with the rest of your answers. It never changes the Docker daemon's configuration.
The chat reads SANDBOX, SANDBOX_MANAGER_URL and SANDBOX_MANAGER_TOKEN, and reaches the
manager by that address and token alone, never by assuming a service name. A mix of the three
(SANDBOX=local without a token, SANDBOX=off with a URL, the profile on while remote) is
reported as an error by ./configure --check and as a warning in the chat's log at start; the
chat treats it as off. It is never half on. At start the chat logs one line: [sandbox] off, or
[sandbox] manager healthy with the runtime, or a warning that the configured manager does not
answer.
CHAT_CODE_EXECUTION stays a separate setting (above). With SANDBOX=off it is forced to the
browser.
Install gVisor¶
On the host that will run the sandboxes (the chat host for local, the sandbox host for
remote). This restarts Docker, so plan it; the kit's scripts never do it for you.
# Install runsc from https://gvisor.dev/docs/user_guide/install/ (apt repository or release binary), then:
sudo runsc install # registers the runtime in /etc/docker/daemon.json
sudo systemctl restart docker
docker info --format '{{json .Runtimes}}' # must list runsc
docker run --rm --runtime=runsc hello-world
Rootless Docker, Docker Desktop and some hosted container platforms do not support custom runtimes; use a Linux host with the standard Docker Engine.
For development and tests only, SANDBOX_RUNTIME=runc with SANDBOX_ALLOW_RUNC=true runs the
sandboxes with the host's own kernel and no gVisor in between. The manager logs a warning at
every start. Never use it for real users.
A separate sandbox host¶
Run the sandboxes on their own machine when they should not share a host with the chat: more
CPU, or a different trust zone. kit/sandbox/ has a standalone compose with its own
.env.example.
# on the sandbox host
git clone https://github.com/paoloviviani/Cerea && cd Cerea/kit/sandbox # or ./get-kit.sh
cp .env.example .env && chmod 600 .env
# set SANDBOX_MANAGER_TOKEN (openssl rand -hex 32) and, if needed, SANDBOX_BIND, SANDBOX_DOCKER_GID
docker compose up -d
# on the chat host
SANDBOX_MANAGER_TOKEN=... ./configure --sandbox remote --sandbox-url https://sandbox.example.org --sandbox-token-env SANDBOX_MANAGER_TOKEN
The manager listens on SANDBOX_BIND:SANDBOX_PORT (default 127.0.0.1:8080). It has no TLS of
its own and the token is a bearer secret, so either put a TLS reverse proxy in front of it on
the same host:
or bind it to a private address (a VPN or LAN interface) and use http:// there; ./configure
notes plain http. Every call but GET /v1/health needs the token, in the kit and standalone alike.
The socket proxy never publishes a port.
How the host is protected¶
- The Docker socket is mounted by one service. The socket proxy has it; the manager and
the chat never do. The proxy allows only the calls the manager makes (create, start, remove,
inspect, stats, exec, and pulling the one allow-listed image), refuses every other Docker
endpoint, and on every call for a container checks that it carries the manager's labels
(
cerea.sandbox=1and this project'scerea.sandbox.instance). It also re-checks each create request, so even a buggy or compromised manager cannot ask for a privileged container, a host mount, a network, a capability, a root user, another image or another runtime. - The manager refuses to act on a container without the label. It checks before every command, file transfer and removal. On start it removes the labelled containers a previous run left behind (and only this project's).
- Each sandbox is its own container: the
runscruntime, a non-root user, a read-only root file system, a small RAM-backed workspace, no host mounts, all capabilities dropped,no-new-privileges, no swap, memory, CPU and process limits, and no network at all. - A lease is its own container. One lease cannot see another's files; a lease id is random,
and the chat also sends the owner so a mismatched owner gets "no such lease". File paths are
normalised under
/workspace:..is refused, and a symlink out of the workspace is refused. Command output and file sizes are bounded; a command that outlives its time limit has its sandbox destroyed. - The manager and the socket proxy are boxed too: read-only root, no capabilities, limits,
on internal networks (
sandbox-dockerholds the manager and the proxy alone;sandbox-apithe manager and the chat). - Audit log. Every lease, command, file transfer, release and denial is one JSON line,
with OCSF-style fields (
time,activity,status,actor,lease,owner) on the manager's output and appended to/var/lib/sandbox/audit.jsonl(thesandbox-auditvolume). It holds sizes, counts and a short hash of a command, never the command, a file name or any content. - No secret is ever put inside a sandbox.
- The chat reads the three
SANDBOX*settings from its environment only, never from the administration config store, so a saved setting cannot repoint it at another manager.
The audit log grows¶
/var/lib/sandbox/audit.jsonl is append-only and the manager never rotates or trims it: a busy
deployment adds a line per call. Rotate it from the host, for example with logrotate on the
volume's path (docker volume inspect <project>_sandbox-audit) using copytruncate, or ship the manager's output (which carries the same lines, prefixed audit) to your log system. Keep what your policy requires before you delete anything.
Sizing¶
Everything below is a variable in .env (see .env.example); an empty one means the default.
Per sandbox: a default, and a maximum a request may not pass (a request above the maximum is refused with
limit_exceeds_maximum, never silently reduced).
| Limit | Variable | Default | Maximum |
|---|---|---|---|
| Memory | SANDBOX_EXEC_MEMORY_MB |
1024 MB | 2048 MB |
| CPUs | SANDBOX_EXEC_CPUS |
1 | 2 |
| Processes | SANDBOX_EXEC_PIDS |
256 | 1024 |
| Workspace (a RAM-backed tmpfs: it counts against memory) | SANDBOX_EXEC_WORKSPACE_MB |
256 MB | 1024 MB |
| Wall time of one call | SANDBOX_EXEC_TIMEOUT_SECONDS |
30 s | 300 s |
Output of one call (more is cut, the call is marked truncated) |
SANDBOX_EXEC_MAX_OUTPUT_BYTES |
256 KiB | 1 MiB |
| A file put or got | SANDBOX_MAX_FILE_BYTES |
8 MiB | |
| Idle before release | SANDBOX_IDLE_SECONDS |
20 min | 60 min |
| Lifetime, however busy | SANDBOX_MAX_LIFETIME_SECONDS |
4 h |
(Each maximum has its own SANDBOX_EXEC_MAX_* variable.)
Totals:
| Limit | Variable | Default |
|---|---|---|
| Concurrent sandboxes overall | SANDBOX_MAX_LEASES |
8 |
| Concurrent sandboxes per user | SANDBOX_MAX_LEASES_PER_OWNER |
3 |
Requests the manager handles at once (more get 503 busy, before a body is read) |
SANDBOX_MAX_INFLIGHT |
16 |
| File transfers in flight (a subset of the above) | SANDBOX_MAX_FILE_TRANSFERS |
4 |
| Warm pool (pristine sandboxes kept ready) | SANDBOX_WARM_POOL_SIZE |
0 (reserved; ignored for now) |
A request over a total gets 429 quota_exceeded. A CPU and memory total over all sandboxes is in A shared budget.
The services: the manager is capped at 512 MB, 0.5 CPU and 128 processes
(SANDBOX_MANAGER_MEM_LIMIT, _CPUS, _PIDS); the socket proxy at 128 MB, 0.25 CPU and 64
processes (SANDBOX_PROXY_*).
Why the manager gets 512 MB. It buffers what it moves: a call's output is held as bytes, as a string, and again as JSON (a binary-looking byte can become six characters of JSON), and a file transfer holds its file several times over. The worst case is about
64 MB + (SANDBOX_MAX_INFLIGHT - SANDBOX_MAX_FILE_TRANSFERS) x 15 x SANDBOX_EXEC_MAX_OUTPUT_BYTES_CAP
+ SANDBOX_MAX_FILE_TRANSFERS x 4 x SANDBOX_MAX_FILE_BYTES
With the defaults that is 64 + 12 x 15 MiB x 1 + 4 x 4 x 8 MiB = about 370 MB. Raise any of the
three caps and raise SANDBOX_MANAGER_MEM_LIMIT with it; lower them and you may lower it. A request
past SANDBOX_MAX_INFLIGHT is refused before its body is read, so the worst case is a ceiling, not a hope.
What N concurrent sandboxes cost. The limits are caps, not reservations: a sandbox that runs a small script uses far less than its cap. Plan for the worst case, with the defaults:
| Concurrent sandboxes | Memory cap (plus about 0.65 GB for the two services) | CPU cap |
|---|---|---|
| 1 | 1 GB | 1 |
| 2 | 2 GB | 2 |
| 4 | 4 GB | 4 |
| 8 (the default total) | 8 GB | 8 |
gVisor adds some memory and start-up time per sandbox beyond the cap's own use; measure on your
host. The default total of 8 wants a host with 8 to 16 GB to spare; on a 4 to 8 GB host shared with the rest of the stack set SANDBOX_MAX_LEASES to 3 or 4. On a dedicated sandbox host, raise SANDBOX_MAX_LEASES to what the memory table allows, and keep
the per-user limit low enough that one user cannot take them all.
Since a sandbox lives only while a conversation uses it (and for about twenty idle minutes after), a
deployment of N users rarely needs N sandboxes at once.
Quotas with execute_code on the server. Each conversation that runs code holds one sandbox. A user
can run code in SANDBOX_MAX_LEASES_PER_OWNER conversations at once (default 3); the whole deployment in
SANDBOX_MAX_LEASES (default 8). A call that needs a new sandbox when a limit is reached is not refused:
it runs in the browser, and the model is told so in the result. Raise the limits with the RAM table above,
or lower SANDBOX_IDLE_SECONDS so idle sandboxes free their share sooner.
A shared budget¶
Per-sandbox limits are caps, and by default eight sandboxes may each use theirs at once. To let each sandbox burst (say to 1 CPU and 1 GB when it is alone) while all of them together stay inside a total (say 1.5 CPU and 1.5 GB), set a total, a reservation per sandbox, and optionally a hard cap that the kernel enforces.
| Setting | Meaning | Default |
|---|---|---|
SANDBOX_TOTAL_MEMORY_MB |
The most memory all sandboxes may reserve together | empty: no limit |
SANDBOX_TOTAL_CPUS |
The most CPUs all sandboxes may reserve together (fractions work, such as 1.5) |
empty: no limit |
SANDBOX_EXEC_MEMORY_RESERVATION_MB |
What one sandbox reserves (its soft memory limit) | its memory limit |
SANDBOX_EXEC_CPU_RESERVATION |
What one sandbox reserves (its CPU weight, cpus x 1024 shares) |
its CPU limit |
SANDBOX_EXEC_MEMORY_MB / _CPUS |
The limit: the most one sandbox may use, alone | 1024 MB / 1 |
SANDBOX_CGROUP_PARENT |
A systemd slice that holds every sandbox under one hard cap | empty: no slice |
How they interact.
- Admission counts reservations. A new sandbox is refused (
429 quota_exceeded) when the reservations of the sandboxes that exist, plus its own, would pass either total. The chat then runs that call in the browser and tells the model the server sandboxes are busy. A sandbox still being created already holds its share. - A limit above the reservation is a burst. Docker gets the reservation as
MemoryReservationandCpuShares: when the host is short of memory or CPU, sandboxes are pushed back towards their reservation and share CPU in proportion to it. Alone, a sandbox may use its whole limit. - Reservations alone do not cap the sum. Three sandboxes bursting at once can use three limits. Only the slice below is a hard cap on all of them together.
- Checked at start-up (the manager refuses to start otherwise): a reservation must not exceed its limit, and one sandbox's limit (and maximum) must not exceed the total. An unset maximum is lowered to the total for you.
- When the cap is hit. The kernel kills a process that passes the sandbox's memory limit, or the slice's.
After a failed call the manager asks Docker whether the sandbox was OOM-killed or is no longer running. If so
it answers
410(oom_killedorsandbox_gone) and releases the lease at once, so its reservation is free; the chat says "the sandbox ran out of memory" and the next call gets a fresh sandbox with no earlier variables. When Docker cannot confirm it (the exec daemon died but the sandbox still runs), the model reads "the sandbox was killed (most likely it ran out of memory)" and the chat gives that sandbox back. If Docker cannot remove a released sandbox, its reservation stays counted until the manager's reaper removes it. - After a restart. The manager removes the sandboxes of a previous run first and refuses new leases
(
503 starting, so the chat runs the call in the browser) until that has succeeded. While a container of the previous run cannot be removed,/v1/healthsays"sweep": "blocked"("pending"before the first try,"ok"after) and the manager logs the reason, at most once a minute. - The proxy holds the maxima too. It refuses a sandbox whose memory, CPUs or processes pass
SANDBOX_EXEC_MAX_MEMORY_MB,SANDBOX_EXEC_MAX_CPUSorSANDBOX_EXEC_MAX_PIDS(the same variables, passed to both services), whole numbers only, and a request body that repeats a key or holds a number that is not finite.
The hard cap: a systemd slice (once, on the Docker host). Create the slice, then name it. Cerea never creates it, and the sandbox proxy lets sandboxes join only this one slice:
sudo tee /etc/systemd/system/cerea-sandbox.slice >/dev/null <<'EOF'
[Slice]
CPUQuota=150%
MemoryMax=1536M
EOF
sudo systemctl daemon-reload
systemctl show -p MemoryMax,CPUQuotaPerSecUSec cerea-sandbox.slice # check
Then set SANDBOX_CGROUP_PARENT=cerea-sandbox.slice in .env and docker compose up -d.
- The name.
name.slicewith letters, digits,_,.and-. A-in a systemd slice name means nesting:cerea-sandbox.slicelives insidecerea.slice(which systemd creates empty, with no cap, if you have not defined it), so capcerea-sandbox.sliceitself, as above. The manager, the proxy and./configurerefuse-.slice(the root), systemd's ownsystem,userandmachineslices and anything under them (user-1000.slice), and a name with a leading, trailing or doubled-. The proxy also refuses a sandbox that names a different parent, or none. - Cgroup v2 is required for the limits to work (
MemoryMaxis a v2 setting):docker info --format '{{.CgroupVersion}}'must say2, the default on current distributions. - Docker must use the systemd cgroup driver (
docker info --format '{{.CgroupDriver}}'sayssystemd). Withcgroupfs, the value names a cgroup directory instead of a slice: you create that cgroup by hand with the limits, or switch Docker to the systemd driver. - Checks are warnings.
./configure --checkwarns (it does not fail) when the slice is set but systemd does not know it, sets no cap, or Docker is on cgroup v1 orcgroupfs. It is a check on the host that runs./configure, not a runtime guard: the manager starts either way, and a slice that does not exist is created by systemd empty and caps nothing. The standalone sandbox host kit (kit/sandbox/compose.yaml) has no./configure, so nothing checks it there: run thesystemctl showline above yourself.
A small-box preset (1.5 CPU and 1.5 GB for all sandboxes, each may burst to 1 CPU and 1 GB):
SANDBOX_TOTAL_CPUS=1.5
SANDBOX_TOTAL_MEMORY_MB=1536
SANDBOX_EXEC_CPUS=1
SANDBOX_EXEC_MEMORY_MB=1024
SANDBOX_EXEC_CPU_RESERVATION=0.5
SANDBOX_EXEC_MEMORY_RESERVATION_MB=512
SANDBOX_MAX_LEASES=3
SANDBOX_CGROUP_PARENT=cerea-sandbox.slice
Three sandboxes reserve 1.5 CPU and 1536 MB, so a fourth is refused. One alone may use 1 CPU and 1 GB. The slice keeps all three, bursting, inside 1.5 CPU and 1.5 GB. Leave the memory the manager and proxy use (about 0.65 GB) out of the host's budget.
A dedicated VM (for example a multipass VM that only runs the sandbox host): the VM's vCPUs and memory are
already the outer wall, so no slice is needed. Set the totals a little under them (leave about 1 GB for the
operating system, Docker and the two services) and keep SANDBOX_CGROUP_PARENT empty.
The API (for the curious)¶
All calls but health send Authorization: Bearer <token> and must send X-Sandbox-Owner, the owner the lease was made for (on a create it must equal the owner in the body); a lease is only ever visible to its owner, so a leaked or guessed lease id is not enough. The
chat's typed client is src/lib/server/sandbox/client.ts.
| Call | |
|---|---|
GET /v1/health |
no token; status, runtime, whether machines are enabled |
GET /v1/limits |
token, no owner; the per-sandbox defaults as plain numbers: cpus, memoryMb, timeoutSeconds, maxLeases, maxLeasesPerOwner. The chat reads it so the model can state its sandbox's real share (an older manager has no such route and the chat simply omits the line) |
POST /v1/leases |
{kind: "exec", owner, conversation?, limits?} returns the lease; kind: "machine" is refused (403 machines_disabled; 501 when SANDBOX_MACHINES_ENABLED=true, as hosted machines are not built) |
GET /v1/leases/:id |
status, limits, usage |
DELETE /v1/leases/:id |
release |
POST /v1/leases/:id/exec |
{cmd: [...]} or {command: "..."}, plus timeout_seconds, cwd, env, stdin, max_output_bytes; returns exit_code, stdout, stderr, truncated, timed_out |
PUT / GET /v1/leases/:id/files/<path> |
raw bytes, under /workspace |
Troubleshooting¶
./configure --sandbox localsays runsc is not registered. Install it (above), or use a manager on another host with--sandbox remote.- The chat logs "the manager is configured but not reachable". Check
docker compose ps sandbox-manager sandbox-socket-proxyand the manager's logs; forremote, the URL, TLS and the token. - The manager logs
orphan sweep failed. The socket proxy was not ready or cannot read the Docker socket: checkSANDBOX_DOCKER_GIDagainststat -c %g /var/run/docker.sock. 503 sandbox_start_failed. Usually the runtime is not registered, or the image cannot be pulled by the Docker host. The audit log names the Docker error.
Not built yet¶
The warm pool (the first call of a conversation pays for a start, a second or two with the image already pulled; the first ever pays for the image pull) and staging files the document tools made; hosted agent machines and the default-deny egress proxy they need (a later phase).