Skip to content

Sandboxes

View Markdown

A Sandbox is an isolated, long-running container for agents and untrusted code. You drive it with commands and file operations; it stops when idle and starts again on the next command.

examples/python/sandbox/main.py
"""Create a Sandbox, run commands in it, move files and delete it.
Run it with `python examples/python/sandbox/main.py`.
"""
import nodus
def main() -> None:
sb = nodus.Sandbox.create(
cpu=1,
memory="2Gi",
idle_timeout="5m",
max_cost=1,
)
try:
p = sb.exec("python", "-c", "print(6 * 7)")
print("stdout:", p.stdout.read().strip(), "exit:", p.wait())
with sb.open("/workspace/notes.txt", "w") as f:
f.write("written from the SDK\n")
print(sb.exec("cat", "/workspace/notes.txt").stdout.read(), end="")
finally:
sb.terminate()
if __name__ == "__main__":
main()
sb = nodus.Sandbox.create(
name="agent-1", # optional; creating the same name again reconnects
image="nodus/agent-tools", # the default: Python 3.12, Node 22, git
cpu=2, memory="4Gi",
regions=["us"], # optional Nodus region classes; no cross-region fallback
timeout="24h", idle_timeout="5m", on_idle="stop",
network=nodus.Egress.open(), # outbound access; the default is nodus.Egress.deny()
max_cost=5,
)

create returns as soon as the Sandbox is admitted; commands wait for it to start. Egress is denied unless you open it. regions optionally restricts the quote and placement to the named Nodus region classes; an unavailable class does not fall back to another region. nodus.Sandbox.from_name("agent-1") reconnects and starts it again if you stopped it.

Pass existing Nodus Secrets with secrets=[...] to expose their values to the Sandbox. The filesystem client still needs its mount configuration; enabling FUSE does not configure a remote filesystem.

GPU Sandboxes can request the FUSE path when the selected runtime supports it:

sb = nodus.Sandbox.create(
name="gpu-work", gpu="A100", fuse_mount=True, regions=["us"],
)

Omitting fuse_mount uses the startup profile’s value, or False if no profile supplies it. Setting it to True asks Nodus for guest FUSE support; it does not install or mount JuiceFS or provide JuiceFS credentials. Configure the filesystem in the Sandbox separately. GPU Sandboxes are ephemeral, so their local files do not persist across deletion.

sb = nodus.Sandbox.create(name="research-1", startup_profile="research", cpu=4)

The API resolves organization, project and selected-profile defaults. Here, cpu=4 overrides the profile; omitted arguments remain available for inheritance. inherit_startup=False skips organization/project bindings while retaining an explicitly selected profile. fuse_mount=False and empty optional lists are explicit overrides. Without an inherited or explicit image, the API’s Interactive defaults use nodus/agent-tools. See startup profiles for review, authorization and lifecycle rules.

The local-backend GPU + JuiceFS qualification example source shows two Processes using the same US A100 Sandbox, then explicitly deletes it. It creates SQLite metadata and a file object backend inside the guest; it does not connect to a customer-owned JuiceFS service or provide credentials. The example uses the exact image digest from the recorded A100 qualification. It does not qualify other GPU families, customer remote filesystems, staging or production.

examples/sandboxes/gpu-juicefs/main.py
"""Run the local-backend GPU + JuiceFS qualification flow in one warm Sandbox.
Run from a checkout with the Python SDK configured:
python examples/sandboxes/gpu-juicefs/main.py
This is a qualification example, not a customer remote-filesystem recipe. The guest
scripts use SQLite metadata and a local file object backend and provide no credentials.
To qualify a private image, set QUALIFICATION_IMAGE to its registry reference and
REGISTRY_SECRET to an existing project Registry Secret name. This does not validate
a customer remote filesystem.
"""
import os
from pathlib import Path
import nodus
HERE = Path(__file__).resolve().parent
QUALIFICATION_IMAGE = (
"ghcr.io/nodus-compute/catalog/pytorch@"
"sha256:480e0f80ff3bae0e1498f4de57886d0c3836fb6ff29adbefdaa1255a94374d1a"
)
def run_guest_script(sb: nodus.Sandbox, script_name: str) -> None:
process = sb.exec("bash", "-s", stdin=True, timeout="12m")
process.stdin.write((HERE / script_name).read_text())
process.stdin.write_eof()
output = process.stdout.read()
exit_code = process.wait()
print(output, end="")
if exit_code != 0:
raise RuntimeError(f"{script_name} failed with exit code {exit_code}")
def main() -> None:
image = os.environ.get("QUALIFICATION_IMAGE", QUALIFICATION_IMAGE)
registry_secret = os.environ.get("REGISTRY_SECRET")
sb = nodus.Sandbox.create(
image=image,
image_pull_secrets=[registry_secret] if registry_secret else [],
cpu=30,
gpu="A100",
fuse_mount=True,
regions=["us"],
network=nodus.Egress.open(),
timeout="15m",
idle_timeout="5m",
max_cost=1,
)
try:
run_guest_script(sb, "guest-first.sh")
# The second Process reconnects to the same local metadata and object paths.
run_guest_script(sb, "guest-second.sh")
finally:
sb.terminate()
if __name__ == "__main__":
main()

For a prepared on-demand GPU Sandbox that mounts an existing customer JuiceFS filesystem from a Nodus Secret, see the customer-owned JuiceFS preparation recipe. It uses the bounded one-item ready queue; the prepared GPU remains billable while idle. The recipe verifies a sentinel read and a small CUDA operation before queue admission, but does not establish reachability to your filesystem or qualification of a GPU offering.

p = sb.exec("python", "-c", "print(6 * 7)")
print(p.stdout.read()) # everything the process wrote to stdout
assert p.wait() == 0 # the exit code
p = sb.exec("pip install requests && python app.py") # one string runs under /bin/sh -c
for chunk in p.stdout: # stream output as it arrives
print(chunk, end="")
p = sb.exec("bash", pty=True)
p.write("ls\n"); p.resize(40, 120); p.signal("SIGINT")

Every command is recorded as a Process, and its output is kept, so a reader that reconnects continues where it stopped. p.stdin.write(...) and p.stdin.write_eof() feed input; p.cancel() stops the process.

with sb.open("/workspace/notes.txt", "w") as f:
f.write("hi")
print(sb.files.read("/workspace/notes.txt"))
print(sb.files.list("/workspace"))
sb.stop() # saves the filesystem; compute billing stops, the saved files count as storage
sb.start()
image = sb.snapshot_filesystem() # Beta: an Image of the current filesystem
sb.terminate() # deletes it