The code sandbox
The sandbox runs Python written by the model or the student. It must survive hostile or broken code without harming the server, other chats or the student's data. This page explains the layers that make that true. The service is in sandbox/executor/; the API's client is app/sandbox/client.py.
The service
A small FastAPI service runs Jupyter kernels, one per chat, so variables persist between cells. A request names the chat, sends the code and a time limit, and gets back what the cell printed, drew (PNG plots) and wrote (files), plus any error and traceback.
- At most 4 kernels run at once (
SANDBOX_MAX_KERNELS). When a fifth chat needs one, the least recently used idle kernel stops. - A kernel idle for 15 minutes stops (
SANDBOX_IDLE_SECONDS). - A cell's time limit is 30 seconds by default, capped at 120.
- Output is capped: 200,000 characters per stream and 50 MB per cell, files included.
Only the API talks to it, with a shared token (SANDBOX_TOKEN).
Layers of isolation
Figure 1 shows the layers from the outside in.
flowchart TB
subgraph Host
subgraph Container["Container: gVisor runsc, read-only root, no capabilities, 3 GB, 2 CPUs"]
subgraph Net["Internal network: no route out"]
EX[Executor<br/>non-root]
subgraph PG["Kernel process group"]
K[IPython kernel<br/>rlimits: memory, file size, processes]
C[Processes a cell starts]
end
end
end
end
API[Backend] -- token --> EX
EX -- watchdog every 5 ms --> PG
Figure 1. The sandbox's isolation layers.
- gVisor. The container runs under
runsc, which gives it its own user-space kernel. A kernel exploit inside the sandbox reaches gVisor, not the host. The executor checks at start-up that it is under gVisor and refuses to run otherwise, unlessSANDBOX_ALLOW_RUNC=true(development only). - The container. Read-only root file system;
tmpfsfor/work(1 GB) and/tmp(256 MB); all capabilities dropped;no-new-privileges; a non-root user;initas PID 1 to reap orphans; memory 3 GB, 2 CPUs, a host pids limit of 4,096. - The network. The container sits only on an internal Docker network with a fixed address (
172.31.250.10). The backend joins that network; nothing else does, and it has no route to the internet. - Per-kernel limits. Each kernel starts with resource limits: address space 2 GB, files 50 MB, 120 processes, no core dumps. gVisor enforces these.
- The process watchdog. While a cell runs, the executor lists the kernel's process group every 5 ms. Past 24 processes (
SANDBOX_MAX_PROCESSES) it contains the cell.
Containing a runaway cell
Each kernel leads its own process group, which every process a cell starts inherits. contain():
- sends SIGSTOP to the whole group at once, so no member can fork again,
- kills every member except the kernel, repeating until none are left,
- sends the kernel SIGINT (the cell sees
KeyboardInterrupt), then SIGCONT, - sweeps the group until it has stayed empty for 0.3 seconds (at most 3 seconds), because a fork in flight when the group stopped completes after the kernel resumes.
The kernel survives, so the chat keeps its variables. Live tests under gVisor: 6 exponential fork bombs in a row left 0 processes behind, the variables were kept, and multiprocessing.Pool(4) still worked afterwards.
A cell that runs out of memory hits its address-space limit and gets a MemoryError. A cell past its time limit is interrupted.
Why the pids limit is high
Under gVisor, every process inside costs host processes (the sentry's stubs). A limit of 256 starved gVisor's own forks and killed the whole container during a fork bomb. The real ceiling on a cell is the per-kernel RLIMIT_NPROC and the watchdog. The host's 4,096 is a backstop.
What a cell can use
Python 3.13 with NumPy, SciPy, SymPy, Matplotlib, OpenCV, scikit-image, pandas and NetworkX (sandbox/requirements.txt, pinned). Matplotlib draws inline. A cell can write files in its working folder; they come back with the output.
How answers use it
The answer graph's code and math nodes call the sandbox through the API (see The answer graph). Cells and their outputs are saved in code_cells, linked to the chat and, for answer cells, to the message. The Code tab shows them.
Testing it
The sandbox's tests in sandbox/tests include fork bombs and memory hogs. They run only inside a container, never on a bare host.