FastSandbox¶
fastsandbox.FastSandbox
¶
FastSandbox(name: str = 'fastsandbox', *, lifespan: Lifespan | None = None, kit: Kit | KitPackage | str = 'shell', kits: KitCache | None = None, client: Client | None = None, socket: str | None = None, max_concurrency: int | None = None)
An application of tasks that run in sandboxes.
Run its tasks with await app.run(name, **arguments), from synchronous code
with app.run_sync, or from the command line with fastsandbox run. Use
it as an async context manager to run several tasks with one client and one
lifespan; otherwise each run opens its own.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
The name of the application, the prefix of its sandboxes. |
|
tasks |
dict[str, Task]
|
The tasks it declares, by name. |
state |
Mapping[str, Any]
|
What its lifespan yielded, while the application is entered
( |
An application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the application, the prefix of its sandboxes. |
'fastsandbox'
|
lifespan
|
Lifespan | None
|
Run around the life of the application; what it yields
is |
None
|
kit
|
Kit | KitPackage | str
|
The kit of tasks that do not name one. |
'shell'
|
kits
|
KitCache | None
|
The kit cache; the one of the process otherwise ( |
None
|
client
|
Client | None
|
A client to use instead of connecting. |
None
|
socket
|
str | None
|
The socket to connect to, when |
None
|
max_concurrency
|
int | None
|
How many runs may hold a sandbox at once. |
None
|
task
¶
task(fn: None = None, *, name: str | None = None, kit: Kit | KitPackage | str | None = None, network: Network | None = None, timeout: float | str | timedelta | None = None, env: Mapping[str, str] | None = None, cpus: int | None = None, memory: str | None = None, keep: bool = False, description: str | None = None, max_concurrency: int | None = None) -> Callable[[Callable[..., Any]], Task]
task(fn: Callable[..., Any] | None = None, *, name: str | None = None, kit: Kit | KitPackage | str | None = None, network: Network | None = None, timeout: float | str | timedelta | None = None, env: Mapping[str, str] | None = None, cpus: int | None = None, memory: str | None = None, keep: bool = False, description: str | None = None, max_concurrency: int | None = None) -> Task | Callable[[Callable[..., Any]], Task]
Declares a task; use it as @app.task or @app.task(...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any] | None
|
The function, when used without arguments. |
None
|
name
|
str | None
|
The name of the task; the name of the function by default. |
None
|
kit
|
Kit | KitPackage | str | None
|
The kit of its sandbox; the kit of the application by default. |
None
|
network
|
Network | None
|
The network rules of its sandbox. |
None
|
timeout
|
float | str | timedelta | None
|
The deadline of a run: seconds, a |
None
|
env
|
Mapping[str, str] | None
|
Environment variables of its sandbox. |
None
|
cpus
|
int | None
|
Number of virtual CPUs. |
None
|
memory
|
str | None
|
Memory limit, e.g. |
None
|
keep
|
bool
|
Keeps the sandbox after the run, or after a launch that failed once it existed, for debugging. |
False
|
description
|
str | None
|
Its documentation; the docstring by default. |
None
|
max_concurrency
|
int | None
|
How many runs of this task may run at once. |
None
|
agent
¶
agent(agent: Agent | str, *, name: str | None = None, kit: Kit | KitPackage | str | None = None, network: Network | None = None, mcp: Sequence[McpServer] = (), timeout: float | str | timedelta | None = None, env: Mapping[str, str] | None = None, cpus: int | None = None, memory: str | None = None, keep: bool = False, description: str | None = None, max_concurrency: int | None = None) -> Callable[[Callable[..., Any]], Task]
Declares a task whose sandbox runs a coding agent, reached with ctx.agent.
agent is one of claude, codex, copilot, cursor, devin, docker-agent,
droid, kiro or opencode.
The keys of the agent are read from the environment (e.g. ANTHROPIC_API_KEY), as its
profile names them (Agent.keys), and given to the proxy of its sandbox only: the
sandbox holds a placeholder. They are given whatever the kit: the agent's own, composed
or not, an image, a package or a kit the CLI compiles; a key whose service no kit of the
sandbox declares is left out. When none is set, the credential the daemon holds for the
kit applies, a Claude subscription for instance. kiro has no service in sbx: the
variables it reads in clear (Agent.forwarded_env, KIRO_API_KEY) are given to its
sandbox as they are, and visible inside it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent | str
|
The agent. |
required |
name
|
str | None
|
The name of the task; the name of the function by default. |
None
|
kit
|
Kit | KitPackage | str | None
|
The kit, the agent's own by default, e.g. composed with mixins. |
None
|
network
|
Network | None
|
The network rules of its sandbox. |
None
|
mcp
|
Sequence[McpServer]
|
MCP servers registered in the agent, for |
()
|
timeout
|
float | str | timedelta | None
|
The deadline of a run. |
None
|
env
|
Mapping[str, str] | None
|
Environment variables of its sandbox. |
None
|
cpus
|
int | None
|
Number of virtual CPUs. |
None
|
memory
|
str | None
|
Memory limit, e.g. |
None
|
keep
|
bool
|
Keeps the sandbox after the run, or after a launch that failed once it existed, for debugging. |
False
|
description
|
str | None
|
Its documentation; the docstring by default. |
None
|
max_concurrency
|
int | None
|
How many runs of this task may run at once. |
None
|
Raises:
| Type | Description |
|---|---|
TaskError
|
When |
get_task
¶
run
async
¶
run(task: str | Task, /, *, on_progress: ProgressHandler | None = None, **arguments: Any) -> Any
Runs a task in a throw-away sandbox and returns its validated result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str | Task
|
The task, or its name. |
required |
on_progress
|
ProgressHandler | None
|
Receives what the task reports with
|
None
|
**arguments
|
Any
|
The inputs of the task, validated by its signature. |
{}
|
An error of the task goes on as it is, the timeout of a command or of an agent run included; when the removal of the sandbox fails after it, that failure is noted on it.
Raises:
| Type | Description |
|---|---|
TaskError
|
When the application has no such task. |
ValidationError
|
When the inputs or the result do not match the signature of the task. |
InvalidSpecError
|
When the sandbox of the run breaks a rule of a spec: a host of its network rules that is not a host pattern, a mount without a workspace. |
InvalidKitError
|
When its kit is not named as the |
KitForgeError
|
When its kit is one only the |
LaunchError
|
When the launch fails once the sandbox exists, a rule the daemon
refuses for instance; its |
SandboxTimeoutError
|
When the run goes over the timeout of the task. |
fastsandbox.run
¶
Runs a task, or any awaitable, from synchronous code.
For a script, a notebook cell or a test.
Example
fastsandbox.run(run_tests, repo=".") fastsandbox.run(main())
fastsandbox.Task
dataclass
¶
Task(app: FastSandbox, name: str, fn: Callable[..., Any], description: str, kit: Kit | KitPackage | str, network: Network, timeout: float | None, env: dict[str, str], cpus: int | None, memory: str | None, keep: bool, agent: Agent | None, mcp: tuple[McpServer, ...], parameters: list[Parameter], input_model: type[BaseModel], output: TypeAdapter[Any] | None, semaphore: PerLoop[Semaphore] | None = None)
A task of an application: a function run in a throw-away sandbox.
Call it (await task(repo=".")) to run it through its application.
Attributes:
| Name | Type | Description |
|---|---|---|
app |
FastSandbox
|
The application of the task. |
name |
str
|
The name of the task. |
fn |
Callable[..., Any]
|
The function of the task. |
description |
str
|
Its documentation, from the docstring by default. |
kit |
Kit | KitPackage | str
|
The kit of its sandbox: a composition, its name, or a package, held in memory or in a directory. |
network |
Network
|
The network rules of its sandbox. |
timeout |
float | None
|
The deadline of a run, in seconds. |
env |
dict[str, str]
|
Environment variables of its sandbox. |
cpus |
int | None
|
Number of virtual CPUs of its sandbox. |
memory |
str | None
|
Memory limit of its sandbox, e.g. |
keep |
bool
|
Whether the sandbox is kept after the run, for debugging. |
agent |
Agent | None
|
The agent of the task, for one declared with |
mcp |
tuple[McpServer, ...]
|
The MCP servers registered in its agent. |
input_model |
type[BaseModel]
|
The Pydantic model of the inputs. |
output |
TypeAdapter[Any] | None
|
The validator of the result, from the return annotation. |
fastsandbox.Context
¶
Context(app: FastSandbox, task: Task, client: Client, lease: Lease, agent: AgentRunner | None, state: Mapping[str, Any], loop: AbstractEventLoop, progress: ProgressHandler | None)
What a task run has access to.
It is injected into a task, or a dependency, by annotating a parameter with Context.
Attributes:
| Name | Type | Description |
|---|---|---|
app |
The application. |
|
task |
The task being run. |
|
run_id |
The identifier of this run. |
|
client |
The client of the daemon. |
|
lease |
The lease that owns the sandbox of the run: its creation, its report. |
|
sandbox |
Sandbox
|
The sandbox of the run. |
state |
What the lifespan of the application yielded. |
|
logger |
The logger of the task, |
agent
property
¶
agent: AgentRunner
The agent of the sandbox, for a task declared with app.agent.
Raises:
| Type | Description |
|---|---|
TaskError
|
When the task has no agent. |
mcp_token
¶
The token of the MCP server of the host server of the task, drawn for this run.
The server checks it in the Authorization header of the requests the agent sends it:
it is the sandbox's own, so that the server tells this run apart from any other process.
Raises:
| Type | Description |
|---|---|
TaskError
|
When the task registers no server of the host of that name. |
report_progress
async
¶
Reports the progress of the run to the progress handler of the call.