Skip to content

Sandboxes

fastsandbox.Sandboxes

Sandboxes(native: Sandboxes, client: Client)

The collection of sandboxes.

list async

list() -> list[SandboxInfo]

Every sandbox.

get async

get(name: str) -> SandboxInfo

The sandbox name.

Raises:

Type Description
NotFoundError

When there is none.

find async

find(name: str) -> SandboxInfo | None

The sandbox name, or None.

create async

create(request: CreateRequest) -> Created

Creates a sandbox from a request and waits for the result.

The kit images must already be loaded (see KitPackage.create_with).

create_stream async

create_stream(request: CreateRequest) -> Stream[CreateEvent]

Creates a sandbox, streaming its progress.

fastsandbox.Sandbox

Sandbox(native: Sandbox, client: Client)

A sandbox of the daemon, by name.

name property

name: str

The name of the sandbox.

client property

client: Client

The client the sandbox is reached through.

native property

native: Sandbox

The handle of the native extension.

inspect async

inspect() -> SandboxInfo

The current state of the sandbox.

exists async

exists() -> bool

Whether the sandbox exists.

start async

start(*, ssh_agent_socket: str | None = None) -> SandboxInfo

Starts the sandbox (idempotent).

It stops again about 30 s after the last session closes: hold one with running() or hold_session() while working in it.

stop async

stop() -> SandboxInfo

Stops the sandbox.

delete async

delete(*, force: bool = False, expected_id: str | None = None) -> None

Deletes the sandbox.

Parameters:

Name Type Description Default
force bool

Stops it first when it runs.

False
expected_id str | None

Refuses to delete a different sandbox that took the same name.

None

force_delete async

force_delete() -> None

Stops the sandbox if needed, then deletes it.

save async

save(tag: str) -> SavedImage

Saves the sandbox as the image tag, a template for new sandboxes.

set_working_dir async

set_working_dir(dir: str) -> None

Sets the working directory of new sessions.

allow_host_path async

allow_host_path(host_path: str | PathLike[str]) -> None

Grants the sandbox access to an existing path of the host.

revoke_host_path async

revoke_host_path(host_path: str | PathLike[str]) -> None

Revokes access to a path of the host.

vm_metrics async

vm_metrics() -> str

The metrics of the virtual machine, in Prometheus text format.

wait_running async

wait_running(timeout: float = 60.0, interval: float = 0.5) -> SandboxInfo

Waits until the sandbox runs, polling every interval seconds.

Raises:

Type Description
SandboxTimeoutError

When it does not run within timeout seconds.

hold_session async

hold_session() -> Session

Opens a session that keeps the sandbox running while it is held.

ensure_running async

ensure_running() -> Session

Starts the sandbox if needed and holds a session on it.

running async

running() -> AsyncIterator[Self]

Keeps the sandbox running for the duration of the block.

Example

async with sandbox.running(): ... await sandbox.exec("make test")

exec async

exec(command: Command | ExecSpec, *, user: str | None = None, working_dir: str | None = None, env: Mapping[str, str] | None = None, timeout: float | None = None, check: bool = False) -> ExecOutput

Runs a command to completion.

Parameters:

Name Type Description Default
command Command | ExecSpec

A shell script, run with POSIX sh -c, which parses any value put into it; an argument list, run with no shell; or a full ExecSpec, which holds the user, the working directory and the environment itself.

required
user str | None

User name or UID ("0" for root); the kit's user by default.

None
working_dir str | None

Working directory.

None
env Mapping[str, str] | None

Additional environment variables.

None
timeout float | None

Deadline of the wait, in seconds; the long-operation deadline of the client by default. It is not sent to the daemon: the command goes on in the sandbox when it expires.

None
check bool

Raises CommandFailedError on a non-zero exit code.

False

Raises:

Type Description
TypeError

When user, working_dir or env comes with a full ExecSpec.

SandboxTimeoutError

When the deadline expires.

CommandFailedError

With check=True, on a non-zero exit code.

exec_inspect async

exec_inspect(exec_id: str) -> ExecInspect

The state of the execution exec_id, while the daemon remembers it.

attach async

attach(command: Command | AttachSpec, *, tty: bool = False, size: tuple[int, int] | None = None, user: str | None = None, working_dir: str | None = None, env: Mapping[str, str] | None = None) -> Interactive

Starts an interactive command.

Parameters:

Name Type Description Default
command Command | AttachSpec

A shell script, an argument list, or a full AttachSpec, which holds the other arguments itself.

required
tty bool

Allocates a pseudo-terminal.

False
size tuple[int, int] | None

Initial (width, height) of the terminal.

None
user str | None

User name or UID.

None
working_dir str | None

Working directory.

None
env Mapping[str, str] | None

Additional environment variables.

None

Raises:

Type Description
TypeError

When another argument comes with a full AttachSpec.

agent_session async

agent_session(args: Sequence[str] | AgentSessionSpec = (), *, tty: bool = False, size: tuple[int, int] | None = None, working_dir: str | None = None, env: Mapping[str, str] | None = None) -> Interactive

Starts the agent of the sandbox kit in an interactive session.

Parameters:

Name Type Description Default
args Sequence[str] | AgentSessionSpec

Arguments of the agent, as sbx run AGENT -- ARGS passes them, or a full AgentSessionSpec, which holds the other arguments itself.

()
tty bool

Allocates a pseudo-terminal; interactive agents expect one, non-interactive runs separate stdout and stderr without it.

False
size tuple[int, int] | None

Initial (width, height) of the terminal.

None
working_dir str | None

Working directory.

None
env Mapping[str, str] | None

Additional environment variables.

None

Raises:

Type Description
TypeError

When another argument comes with a full AgentSessionSpec.

upload_archive async

upload_archive(dir: str, archive: bytes) -> None

Extracts a tar archive into dir of the sandbox.

download_archive async

download_archive(path: str) -> bytes

A tar archive of path of the sandbox.

write_file async

write_file(path: str, contents: bytes | str, *, mode: int | None = None) -> None

Writes contents (UTF-8 for a string) to the file path.

read_file async

read_file(path: str) -> bytes

The contents of the file path.

read_text async

read_text(path: str, encoding: str = 'utf-8') -> str

The contents of the file path, decoded.

upload_dir async

upload_dir(host_dir: str | PathLike[str], dir: str) -> None

Copies the directory host_dir of the host, under its own name, into dir.

upload_dir("./project", "/work") gives /work/project/…; to copy the contents alone, send a tar archive with upload_archive.

download_dir async

download_dir(path: str, host_dir: str | PathLike[str]) -> None

Copies path of the sandbox into the directory host_dir of the host.

ports async

ports() -> list[PortBinding]

The published ports.

publish async

publish(*ports: PortBinding) -> list[PortBinding]

Publishes ports; the result has the bindings the daemon applied.

unpublish async

unpublish(*ports: PortBinding) -> None

Unpublishes ports.

replace_desired_port async

replace_desired_port(old: PortBinding, new: PortBinding) -> None

Replaces a port the sandbox publishes when it starts.

network_rules async

network_rules() -> list[NetworkRule]

The network rules scoped to this sandbox.

The global rules, which apply to it too, are not among them: client.policy.rules() lists every rule.

allow_hosts async

allow_hosts(*hosts: str) -> RuleChangeResult

Allows hosts for this sandbox only.

Each host is a host pattern (HostPattern): a name, *.example.com, an address, with a port or not.

Raises:

Type Description
InvalidRequestError

When a host is not a host pattern, before anything is sent.

OperationFailedError

When the daemon refuses the change.

deny_hosts async

deny_hosts(*hosts: str) -> RuleChangeResult

Denies hosts for this sandbox only.

Each host is a host pattern (HostPattern): a name, *.example.com, an address, with a port or not.

Raises:

Type Description
InvalidRequestError

When a host is not a host pattern, before anything is sent.

OperationFailedError

When the daemon refuses the change.

remove_hosts async

remove_hosts(*hosts: str) -> RuleChangeResult

Removes the rules of this sandbox on hosts.

remove_network_rule async

remove_network_rule(id: str) -> RuleChangeResult

Removes the rule id of this sandbox.

push_credentials async

push_credentials(update: CredentialsUpdate) -> CredentialsOutcome

Pushes a credentials update: custom secrets, service credentials and their sources.

Returns:

Type Description
CredentialsOutcome

The outcome; its withheld names the services the daemon gives nothing.

push_service_secret async

push_service_secret(service: str, value: str | SecretStr) -> CredentialsOutcome

Sets the secret of service for this sandbox, given as text or as a SecretStr.

The proxy adds it to requests; the sandbox never sees it.

Returns:

Type Description
CredentialsOutcome

The outcome; service is in its withheld when no kit of the sandbox is

CredentialsOutcome

authorized its credential, and the proxy then injects nothing.

push_proxied_keys async

push_proxied_keys(*keys: ProxiedKey) -> CredentialsOutcome

Gives the sandbox keys, created with their placeholders.

The proxy writes each key in place of its placeholder in the requests to its hosts (see ProxiedKey). The keys replace the custom secrets the sandbox had: one call carries every key of the sandbox.

Returns:

Type Description
CredentialsOutcome

The outcome of the push.

remove_service_secret async

remove_service_secret(service: str) -> CredentialsOutcome

Removes the secret of service for this sandbox.

Returns:

Type Description
CredentialsOutcome

The outcome of the update.

add_kit_artifact async

add_kit_artifact(artifact: Any) -> None

Installs a kit artifact in place, in the running container.

swap_container async

swap_container(request: SwapRequest) -> SwapOutcome

Replaces the container with one built with more kits.

mcp_gateway async

mcp_gateway() -> McpGateway

The MCP gateway of the sandbox.

load_mcp_server async

load_mcp_server(server: str) -> LoadedServer

Loads the registered MCP server server into the gateway.

unload_mcp_server async

unload_mcp_server(server: str) -> None

Unloads the MCP server server from the gateway.

ssh_tunnel async

ssh_tunnel() -> SshTunnel

An SSH connection to the sandbox, as a byte tunnel.