Sandboxes¶
fastsandbox.Sandboxes
¶
Sandboxes(native: Sandboxes, client: Client)
The collection of sandboxes.
get
async
¶
get(name: str) -> SandboxInfo
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.
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.
delete
async
¶
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
¶
Grants the sandbox access to an existing path of the host.
revoke_host_path
async
¶
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 |
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 |
required |
user
|
str | None
|
User name or UID ( |
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 |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
When |
SandboxTimeoutError
|
When the deadline expires. |
CommandFailedError
|
With |
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 |
required |
tty
|
bool
|
Allocates a pseudo-terminal. |
False
|
size
|
tuple[int, int] | None
|
Initial |
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 |
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 |
()
|
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 |
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 |
upload_archive
async
¶
Extracts a tar archive into dir of the sandbox.
write_file
async
¶
Writes contents (UTF-8 for a string) to the file path.
read_text
async
¶
The contents of the file path, decoded.
upload_dir
async
¶
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
¶
Copies path of the sandbox into the directory host_dir of the host.
publish
async
¶
publish(*ports: PortBinding) -> list[PortBinding]
Publishes ports; the result has the bindings the daemon applied.
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 |
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; |
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.
load_mcp_server
async
¶
load_mcp_server(server: str) -> LoadedServer
Loads the registered MCP server server into the gateway.