Skip to main content
Run commands inside a running sandbox. See Commands for usage examples.

Sandbox

sandbox.exec()

Run a command inside the sandbox and wait for it to complete, buffering all stdout and stderr into memory. The keyword-only options apply to this call alone and don’t change the sandbox’s defaults. For long-running processes or large output, use exec_stream() instead. Raises ExecTimeoutError if timeout elapses and MicrosandboxError if the process can’t be spawned.

Parameters

cmdstr
Command to execute (e.g. “python3”, “/usr/bin/node”).
argslist[str] | ExecOptions | None
Command arguments, or a typed options mapping that may include its own args list.
cwdstr | None
Working directory for this command.
userstr | None
Guest user to run as.
envMapping[str, str] | None
Environment variables, merged on top of the sandbox defaults.
timeoutfloat | None
Seconds before the process is killed.
Stdin configuration. Raw bytes are sent inline; default is /dev/null.
ttybool
Allocate a pseudo-terminal, merging stdout and stderr.
POSIX resource limits applied to the process.

Returns

Collected stdout, stderr, and exit status.

sandbox.shell()

Run a command through the sandbox’s configured shell (defaults to /bin/sh). Shell syntax like pipes, redirects, and && chains works. Accepts the same keyword-only options as exec().

Parameters

scriptstr
Shell command string (e.g. “ls -la /app && echo done”).
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Collected stdout, stderr, and exit status.

Stream methods

sandbox.exec_stream()

Run a command with streaming output. Returns an ExecHandle that emits stdout, stderr, and exit events as they happen rather than buffering everything. Takes the same per-call options as exec(). Pass stdin=Stdin.pipe() to write to the process while it runs via take_stdin(). For TTY sessions, call resize(rows, cols) when the terminal dimensions change.

Parameters

cmdstr
Command to execute.
argslist[str] | ExecOptions | None
Command arguments, or a typed options mapping.
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Streaming handle for receiving events and controlling the process.

sandbox.shell_stream()

Streaming variant of shell(): runs script through the configured shell but returns an ExecHandle instead of buffering output.

Parameters

scriptstr
Shell command string.
cwd, user, env, timeout, stdin, tty, rlimits
Same per-call options as exec().

Returns

Streaming handle.

Attach methods

sandbox.attach()

Bridge your terminal directly to a process inside the sandbox for a fully interactive PTY session. Press the configured detach key sequence (default Ctrl+]) to disconnect without stopping the process. Returns the process exit code.

Parameters

cmdstr
Command to run.
argslist[str] | None
Command arguments.
cwdstr | None
Working directory.
userstr | None
Guest user to run as.
envMapping[str, str] | None
Environment variables for the session.
detach_keysstr | None
Detach key sequence (e.g. “ctrl-]” or “ctrl-p,ctrl-q”).

Returns

int
Exit code of the process.

sandbox.attach_shell()

Bridge your terminal to the sandbox’s default shell in a fully interactive PTY session. Returns the shell’s exit code.

Returns

int
Exit code of the shell process.

ExecHandle

Returned by exec_stream() · shell_stream()

A handle to a running streaming execution and its events.

handle.id

str Correlation ID for this execution

handle.take_stdin()

Take the stdin writer. Returns None after the first call, or when stdin wasn’t piped

Returns

ExecSink \| None

handle.recv()

(async) Receive the next event. Returns None when the stream ends

Returns

ExecEvent \| None

handle.wait()

(async) Wait for the process to exit. Returns (code, success)

Returns

tuple[int, bool]

handle.collect()

(async) Drain remaining output and wait for exit

Returns

ExecOutput

handle.signal()

(async) Send a POSIX signal (numeric) to the process

handle.kill()

(async) Send SIGKILL to the process

handle.resize()

(async) Resize the PTY to the given uint16 dimensions

ExecSink

Returned by ExecHandle.take_stdin()

Writer for sending data to a running process’s stdin. Obtained from ExecHandle.take_stdin() when the execution was configured with stdin=Stdin.pipe().

sink.write()

(async) Write bytes to the process’s stdin

sink.close()

(async) Close the sink. Sends EOF in non-TTY pipe mode; the guest PTY remains open in TTY mode

Stdin

Used by exec() · shell() · exec_stream() · shell_stream()

Factory for process stdin configuration.

Stdin.null()

Connect stdin to /dev/null (default)

Stdin.pipe()

Open a writable pipe. Write via take_stdin() on the handle

Stdin.bytes()

Inline data sent before the process starts, then EOF The factories set the corresponding StdinMode member.

Rlimit

Used by exec() · shell() · exec_stream() · shell_stream()

Frozen dataclass describing a POSIX resource limit. Construct one directly or via a factory, then pass a list as the rlimits argument.

rlimit.resource

RlimitResource Which resource is limited

rlimit.soft

int Soft limit

rlimit.hard

int Hard limit

Rlimit.nofile()

Max open file descriptors

Rlimit.cpu()

CPU time limit in seconds

Rlimit.as_()

Virtual memory size

Rlimit.nproc()

Max number of processes

Rlimit.fsize()

Max file size

Rlimit.memlock()

Max locked memory

Rlimit.stack()

Max stack size

Types

ExecOptions

Accepted by exec() · exec_stream()

Typed dictionary for passing arguments and per-call options together as the second positional argument. Use a Stdin object to select a mode or raw bytes for inline data.

ExecOutput

Returned by exec() · shell() · ExecHandle.collect()

The result of a completed command execution: collected output plus exit status. All members are properties. In TTY mode, stdout_bytes contains the combined terminal output and stderr_bytes is empty because a PTY doesn’t preserve separate stdout and stderr streams.

ExecEvent

Emitted by ExecHandle

Native event object emitted by recv() and by iterating an ExecHandle. Fields that don’t apply to a given event are None.

ExecEventType

Returned by ExecEvent.event_type

String enum (enum.StrEnum) identifying a streaming execution event.

ExitStatus

Used by ExecHandle.wait()

Frozen dataclass describing a process exit result. ExecHandle.wait() returns the same information as a (code, success) tuple.

StdinMode

Used internally by Stdin

String enum (enum.StrEnum) identifying how command stdin is connected.

RlimitResource

Used by Rlimit.resource

String enum (enum.StrEnum) naming a limitable POSIX resource.