Sandbox¶
The sandbox port, Docker, callable and development-only local backends, and code_tool. Concepts: Sandboxed code.
Sandboxed code execution: SandboxPort, Docker, callable and dev-only local backends.
CODE_FILES = {'python': 'main.py', 'javascript': 'main.js', 'bash': 'main.sh'}
module-attribute
¶
CODE_KIND = 'code'
module-attribute
¶
InputFiles = Mapping[str, bytes | str]
module-attribute
¶
OnResult = Callable[[SandboxResult, ToolContext], Any]
module-attribute
¶
RunFn = Callable[[SandboxRequest], Any]
module-attribute
¶
CallableSandbox
¶
Bases: GuardedSandbox
Wraps fn(request); the service does the isolating, this class checks and bounds.
network=True lets requests ask for network access; your service must enforce
request.network and request.timeout_s itself.
Raises:
| Type | Description |
|---|---|
ToolUsageError
|
|
ConfigError
|
bad languages, limits or |
DockerSandbox
¶
Bases: GuardedSandbox
Runs code in a throwaway container: no network, read-only root, no capabilities.
The image needs python3 (the runner), plus node or bash for those languages. It is
never pulled at run time: docker pull it first. network=True lets a request use the
default bridge network: unrestricted egress, including cloud metadata addresses and
the host's own services.
Raises:
| Type | Description |
|---|---|
ConfigError
|
a bad image, user (root uid or gid is refused), runtime or limit. |
DockerStatus
dataclass
¶
Whether Docker can run Linux containers here, with the versions found.
GuardedSandbox
¶
Bases: ABC
A SandboxPort that checks every request and re-applies its limits to every result.
Backends implement _run. Output and files over the limits are cut here too, so a
backend that ignores them fails closed (less output), never open.
request(code, *, language='python', files=None, timeout_s=30.0, max_output_bytes=64000, network=False, env=None)
¶
The checked request run would send to the backend.
Raises:
| Type | Description |
|---|---|
ToolUsageError
|
bad code, language, files or env. |
ConfigError
|
bad limits, or network asked of a sandbox that does not allow it. |
run(code, *, language='python', files=None, timeout_s=30.0, max_output_bytes=64000, network=False, env=None, ctx=None)
async
¶
Run code in the sandbox and return its bounded result; a timeout is a result.
Raises:
| Type | Description |
|---|---|
ToolUsageError
|
bad code, language, files or env. |
ConfigError
|
bad limits, or network asked of a sandbox that does not allow it. |
IntegrationError
|
the backend failed (the cause is chained). |
AdapterError
|
a fail-closed audit sink could not record the start; nothing ran. |
run_sync(code, *, timeout=None, **kw)
¶
run from sync code (inside a running loop, on a worker thread).
Limits
dataclass
¶
Bounds on what a backend returns: produced files and their bytes.
LocalSubprocessSandbox
¶
Bases: GuardedSandbox
DEVELOPMENT ONLY: Python in a temporary folder with a time limit and output caps.
It isolates nothing (files, network, processes and memory are the host's) and refuses to
start unless allow_unsafe_local=True. Every run logs a WARNING. On Windows, processes
the code leaves running in the background are not killed when it exits.
Raises:
| Type | Description |
|---|---|
ConfigError
|
|
SandboxFile
¶
SandboxPort
¶
Bases: Protocol
Runs untrusted code in isolation, within a time limit and an output limit.
SandboxRequest
dataclass
¶
A checked run: what a backend's _run receives.
code_file
property
¶
The file name the code is written to.
SandboxResult
¶
code_tool(sandbox, *, name='run_code', description=None, languages=('python',), timeout_s=30.0, max_output_bytes=64000, network=False, env=None, files=None, on_result=None, tags=())
¶
A tool (kind "code") that runs the model's code in sandbox; every run needs approval.
confirm=True is fixed: there is no option to run model-written code unapproved. Limits,
network, env and input files are the host's; the model chooses only the code (and the
language, when more than one is offered).
Raises:
| Type | Description |
|---|---|
ConfigError
|
bad limits, env, files or languages, or |
ToolUsageError
|
|
docker_status(docker='docker', *, timeout_s=10.0)
¶
Probe the docker CLI and its daemon; never raises.