The Python sandbox library
argusvane-datum-sandbox runs Datum's two programs in Python: the sandbox program that validates a submission and the PDF program. It does the hard part of running them once, for every Python host:
- Choosing the engine. It finds and starts the WebAssembly engine.
- Isolating each run. Each run gets a fresh instance, a read-only file system holding only its own files, a time and memory budget, and capped output.
- Checking the answer. Every answer is checked against the run protocol.
- Proving the programs. It proves the programs at start.
- Limiting runs at once. It caps how many runs go at the same time.
- Reporting. It tells you how its runs went.
It needs no framework, so it works in a script, a worker, Django, Flask, Litestar or anything else in Python. The FastAPI host is built on it, and on FastAPI you set the same settings as environment variables.
Packages
| Distribution | What it is |
|---|---|
argusvane-datum-sandbox | The library: the settings, the lifecycle, the run protocol, the diagnostics. No engine and no framework. |
argusvane-datum-sandbox-wasmtime | The engine, in-process, on wasmtime. |
argusvane-datum-programs | The two programs, packaged. The library depends on it. |
argusvane-datum-qjs-wasm | The pinned WebAssembly module the engine runs. The engine depends on it. |
The library has two extras: aio for the async facade, and testing for the contract kit an engine's tests use. The packages are not on PyPI yet: build the workspace as FastAPI describes, then add the two you need.
uv add argusvane-datum-sandbox argusvane-datum-sandbox-wasmtimeValidate a submission
Make a Datum, start it once, and validate as many submissions as you like. With no arguments it uses the default settings, the engine it finds installed, and the packaged programs. Used as a context manager, it starts on entry and stops on exit.
import json
from pathlib import Path
from datum_sandbox import DatumSandbox
definition = json.loads(Path("customer-intake.json").read_text())
with DatumSandbox() as datum:
context = {
"form": definition["root"],
"values": {"fullName": "", "contactMethod": "email"},
"locale": "en",
}
result = datum.sandbox.validate(context, real=True)
print(result.outcome["errors"])[{"code": "required", "componentId": "fullName", "path": "fullName", "source": "wasm", "stage": "field-constraints"}]The context and the outcome are the ones on Your own server:
- The context. It takes the form's root, the answers and the locale, plus the optional keys listed there.
- The outcome. It lists the errors by code. When no error blocks, it also carries the cleaned-up
values, which is what you store. real. PassTruefor a real submission, whose run log is passed on, andFalsefor a check.
datum. also answers the other questions the sandbox program takes:
inspect, about a definition;check, whether a definition may be saved;public_, the copy of a definition a browser may be served.copy
start() proves both programs before it returns.
- When a proof fails. By default,
start()raises. - Starting down instead. With
fail_in the settings, it starts down:fast=False status()says why, and every run is refused untilrecheck()passes. - Threads. A validation blocks the calling thread until it answers. The sandbox lets as many run at once as it has slots (one per CPU by default). A call that finds no free slot waits up to
validation_(2 s) and is then refused as busy.wait
Settings
Sandbox takes every setting by keyword. A keyword is the setting's key without datum. or datum., with its dots as underscores: validation_ is datum., and formula_ is datum.. A few drop their section:
| Keyword | Setting |
|---|---|
sandbox_, pdf_ | datum., datum. |
trace, report | datum., datum. |
capture_, capture_, capture_, ... | datum. |
A setting you leave out, or pass as None, takes its default. FastAPI settings lists every key with its default and bounds. unknown_ and log_ are checked and kept for the host that reads them (the FastAPI host's submission service); the sandbox itself does not.
from datum_sandbox import DatumSandbox, SandboxSettings
settings = SandboxSettings.build(
validation_time_limit=3.0,
validation_concurrency=4,
trace="failures",
)
with DatumSandbox(settings) as datum:
...How build treats a value:
- Out of bounds. It is moved to its bound, and
settings.holds onechecks datum.warning for it.W008 - Cannot work. It is refused with a
Datumthat names the setting and what to do, such as a trace word that is none ofConfiguration Error off,failuresandall, or a clock tick below 1 ms.
explain() says what the sandbox runs with. It lists every setting with its value in force and where that value came from: default, configured, clamped from what was asked, or replaced when you passed an input of your own in its place. With validation_ added to the settings above, part of it reads:
Datum sandbox settings (value, then where it came from):
datum.runtime.engine = wasmtime (default)
datum.runtime.fail_fast = True (default)
datum.runtime.validation.time_limit = 3.0 (configured)
datum.runtime.validation.memory = 4194304 (clamped from 5 to the bound 4194304 bytes to 1073741824 bytes)
datum.runtime.validation.concurrency = 4 (configured)
datum.runtime.pdf.concurrency = 0 (min(2, CPUs) = 2) (default)
datum.runtime.diagnostics.trace = failures (configured)
...Status and shutdown
status(). It says whether the sandbox is up and why not.status().is the readiness answer, andis_ up status().gives the state, the engine and its mode as a dict for a health check.details recheck(). It proves the programs again without a restart, reading a program override afresh. It never raises: a failure leaves the sandbox down with the reason.stop(). It stops taking runs, ends the runs in flight, and closes the engine. Leaving thewithblock calls it.
In a framework, make the sandbox once at application start, call start() from the framework's start hook and stop() from its shutdown hook, and serve status() as a health check. The FastAPI host does the same, and adds the settings from the environment, the observation and the management routes.
From async code
The library is synchronous. datum_, from the aio extra, runs each call on a worker thread so the event loop never waits:
from datum_sandbox import DatumSandbox
from datum_sandbox.aio import AsyncDatumSandbox
async with AsyncDatumSandbox(DatumSandbox()) as datum:
result = await datum.sandbox.validate(context, real=True)PDFs
The PDF program runs on the same sandbox. datum. sends it a request and checks the answer. The fonts ship in argusvane-datum-programs:
import base64
from importlib.resources import files
font = files("datum_programs") / "resources" / "fonts" / "DejaVuSans.ttf"
with DatumSandbox() as datum:
accepted = datum.sandbox.validate(context, real=True).outcome["values"]
file = datum.pdf_kernel.document({
"form": definition["root"],
"values": accepted,
"locale": "en",
"fonts": [{
"id": "sans",
"family": "sans-serif",
"data": base64.b64encode(font.read_bytes()).decode(),
}],
"env": {"createdAt": "2026-11-23T12:00:00.000Z", "documentId": "submission-1"},
})
Path("answers.pdf").write_bytes(file.pdf)- What it renders. Render the cleaned-up
valuesfrom a validation, not what was posted. - The layout. With no
layout, the answers choose the layout.Id - The other operations.
fill,sign,complete,verifyandinspecttake the requests Your own server lists. PDFs and Signing describe them. - Limits. PDF runs have their own time, memory and concurrency settings (
pdf_,time_ limit pdf_,memory pdf_).concurrency
The FastAPI host's PDF service adds the rest on top: the bold and italic faces, the seal and the server's key, timestamps and trust.
Diagnostics
All three are settings. None of them can change a run's answer, and none can hold a run's slot.
The run trace
trace="failures" writes one line for each failed run, and trace="all" one for every run, including the ones the sandbox makes at start. The line goes at INFO to the logger datum..
run=cf0f863e0564 program=sandbox op=validate abi=4.1.0 engine=wasmtime mode=cranelift slotWaitMs=0.0 prepareMs=1.3 instantiateMs=0.5 startMs=522.4 totalMs=525.7 termination=budget exit=- outcome=budget stdinBytes=290 stdoutBytes=51 stderrBytes=0 capture=20261123-143359-cf0f863e0564- What it holds. How the run went and how long each step took, how it ended, and the sizes of its input and output. It never holds an answer, the definition or the run's output.
- What counts as failed. A run that trapped, threw, hit its time or memory budget or an output cap, was stopped, could not start, or gave an answer the protocol could not take.
- What does not. A validation that finds errors is a normal answer, and so is the program's own refusal of a request.
To send the records somewhere else, such as metrics, pass a trace. It is called with a Trace, whose fields are the line's:
records: list[TraceRecord] = []
with DatumSandbox(SandboxSettings.build(trace="all"), trace=records.append) as datum:
datum.sandbox.validate(context, real=False)
print(records[-1].outcome, records[-1].total)The failed-run capture
The capture keeps each failed run on disk so you can replay it: its input, its output, the generated run script, and a meta. with the trace fields and the settings report.
SandboxSettings.build(
capture_enabled=True,
capture_directory="/var/lib/myapp/datum-captures",
)datum-captures/
20261123-143359-cf0f863e0564/
meta.json run.js stderr.bin stdin.bin stdout.binA capture holds personal data
A capture holds the submitted answers and the form definition, in clear text. Turn the capture on to debug a failure and off again after. The sandbox logs a warning saying so at every start while it is on.
The directory must be one only the application's own user can read:
Missing. It is created with mode
700.Refused at start. The start is refused if the directory:
- is a link, or a file;
- belongs to another user;
- gives the group or others any permission;
- is on macOS and has an access control list (
chmod -Nremoves it; remove any inherited from its parent too).
On a file system without POSIX permissions, such as Windows, the capture cannot be used.
Checked again. The same checks run before every write.
Files. Each is created with mode
600.Complete or absent. A capture appears whole or not at all.
Bounds and timing:
- Bounds.
capture_(20) andmax_ runs capture_(64 MiB) bound what is kept, and the oldest captures go first.max_ bytes - The queue.
capture_(64 MiB) bounds what waits to be written. A capture that does not fit is dropped and counted.queue_ bytes - Off the run's path. The writing happens on a thread of its own, so a run never waits for its capture.
- At stop. What is still waiting is written in whatever is left of the shutdown grace.
status(). shows capture and capture while the capture is on. To keep captures elsewhere, such as an object store, pass a capture_. The bounds and the queue still apply.
The settings report
At start, before the programs are proved, the sandbox logs one block at INFO on datum.. It holds:
- what
explain()lists; - the engine, its release and its mode;
- each program's sha256 and interface version;
- whether the trace and the capture are on.
Any user name and password in a URL is masked, and so are URL query and fragment values. Turn it off with report=False.
Inputs
Each of these is an optional keyword of Datum. Give one only when you need something other than the default.
| Keyword | What it replaces |
|---|---|
settings | The defaults |
runtime | The engine found installed: a Guest of your own. The sandbox closes it at stop. |
sandbox | The sandbox program itself, with a Sandbox of your own |
engine_ | The stop grace and the stray bound of the settings |
sandbox_, pdf_ | Nothing: wrappers that observe every call, the first one outermost |
run_ | Logging a real submission's run log on datum. |
now | The clock today() is read from |
trace | The log line of the run trace |
capture_ | The capture directory |
on_ | Nothing: told once the sandbox is up |