Frames and the sandbox protocol
Needed only when driving a session (a sandbox accepting commands) without an SDK. The SDKs handle all of this.
Frames
Section titled “Frames”A frame is a single-line JSON object ending in LF, followed by the exact
number of raw bytes in its bytes field. Omit bytes when the payload is
empty.
- JSON header line (without LF): at most 64 KiB.
- Raw payload: at most 16 MiB.
Each SDK ships a codec matching dvm-api (encode_frame, FrameDecoder).
Output frames
Section titled “Output frames”GET /sandboxes/{id}/output returns one frame with this header:
| Field | |
|---|---|
stdout |
how many of the payload’s bytes are stdout; the rest are stderr |
stderr |
how many are stderr |
more |
the run is still going, or more is held: ask again |
cut |
the page stopped short, at 4 MiB a stream: ask again from the new offsets |
bytes |
the payload’s length, left out when empty |
The payload holds stdout bytes, then stderr bytes. Add each length to its
offset and repeat until more and cut are both false. This standalone
example decodes a frame locally without connecting to dvm.run:
def decode_output(body: bytes) -> tuple[dict, bytes, bytes]: line_end = body.index(b"\n") header = json.loads(body[:line_end]) payload = body[line_end + 1 :] if len(payload) != header.get("bytes", 0): raise ValueError("the frame's payload isn't the length its header counts") stdout, stderr = payload[: header["stdout"]], payload[header["stdout"] :] return header, stdout, stderrbody = b'{"stdout":2,"stderr":4,"more":false,"cut":false,"bytes":6}\nhioops'header, stdout, stderr = decode_output(body)assert (stdout, stderr) == (b"hi", b"oops")assert header["more"] is False and header["cut"] is FalseSession protocol
Section titled “Session protocol”A session’s first process is sandbox-server. Append request frames to its
input (POST …/input) and
read response frames from its stdout via the output route. Stderr stays
empty.
Read responses while writing requests: the server writes as it goes, and a slow reader slows it down.
Request IDs
Section titled “Request IDs”Each request has an unsigned integer id. A run, read, or the first
chunk of a write starts an operation and must use an ID greater than all
prior started IDs (gaps are allowed; 0 is valid). Lower IDs fail with an
error naming the last started ID. Later write chunks, stdin, and signal
reuse their operation’s ID.
The SDKs use each request’s input byte offset as its ID.
Requests
Section titled “Requests”| Request | Fields | Payload |
|---|---|---|
{"id": n, "run": {…}} |
argv (a list) or script (a string), exactly one; env (an object), cwd, user, stdin (true to keep it open; false gives /dev/null) |
none |
{"id": n, "stdin": {"close": bool}} |
close, false if left out |
bytes for the command’s stdin |
{"id": n, "signal": s} |
a signal from 1 to 64, sent to the command’s process group | none |
{"id": n, "write": {…}} |
path; mode (decimal, 420 if left out) on the first piece; last, false if left out |
a piece of the file |
{"id": n, "read": {"path": …}} |
path |
none |
A script runs under /bin/bash -lc if the image has /bin/bash, or
/bin/sh -c otherwise. stdin and signal return no response when they
succeed. Unknown fields, two operations in one frame, or specifying both
argv and script are rejected.
Responses
Section titled “Responses”| Answer | Means |
|---|---|
{"id": n, "started": {"pid": …, "argv": […]}} |
the command started; first for a command |
{"id": n, "stdout": true}, {"id": n, "stderr": true} |
a chunk of the command’s output, in the payload |
{"id": n, "exited": {"code": c}} |
the command’s process was reaped: its exit code, or 128 plus the killing signal |
{"id": n, "written": true} |
a write’s last piece was written |
{"id": n, "data": true} |
a piece of a read file, at most 256 KiB, in the payload |
{"id": n, "end": true} |
a read is done |
{"id": n, "error": "…"} |
a request refused, or a file operation failed |
{"error": "…"} |
a frame that couldn’t be read at all |
- A command’s stdout and stderr each preserve their own order. No ordering is guaranteed between the two streams or between different commands.
- A child process started by a command can continue writing under that
command’s ID after
exited. - An error frame with no ID also fails every write in progress, each with its own error, because the lost frame may have carried a chunk of any of them.
Ending a session
Section titled “Ending a session”When its input closes, the server kills every running command and exits 0, ending the sandbox.