Skip to content

Testing

Testing without the daemon.

A fake sandboxd on a Unix socket, and kit packages that need no sbx CLI.

FakeDaemon speaks enough of the daemon API for the client and FastSandbox: health, the life cycle of sandboxes, held sessions, command execution, attached commands and agent sessions, network rules, credentials, files, image loading, and the pulls and exports of the published kits shell and claude with the kit settings, so that kits are forged without the CLI as they are by default. Its state is in memory and every request is recorded. Replace or add a route with route, and decide what commands print with on_exec.

Example

async with FakeDaemon() as daemon: ... daemon.on_exec(lambda sandbox, cmd: ExecOutput(exit_code=0, stdout="ok")) ... app = FastSandbox(socket=str(daemon.socket_path), kit=fake_package(tmp_path)) ... await app.run("my_task")

Attached commands and agent sessions upgrade the connection, as the daemon does, and on_exec answers them too. SSH tunnels are not faked: the fake answers them with 501, so code that uses them needs the real daemon.

FakeDaemon

FakeDaemon(*, version: str = 'v0.54.1', api_version: str = '0.52.0')

A fake sandboxd on a Unix socket, for tests.

Attributes:

Name Type Description
version

The sbx version it reports.

api_version

The API version it reports.

sandboxes dict[str, dict[str, Any]]

The sandboxes, by name, in the shape of GET /sandbox/{name}.

rules list[dict[str, Any]]

The network rules added through it: those of a client, and the HTTP rules of a launch. The host rules of a launch are a mixin of its kit, in the creation request.

files dict[tuple[str, str], bytes]

The files of the sandboxes, by (sandbox, absolute path): what PUT /sandbox/{name}/files extracted, and what GET archives.

kits dict[str, PublishedKit]

The published kits it pulls and exports, by repository, e.g. docker.io/sbx/shell: shell and claude at first.

settings dict[str, Any]

The daemon settings it knows, by key; an unknown one is a 404, as the daemon answers. The kit settings allow local, unsigned kits at first.

requests list[Request]

Every request received, in order.

socket_path property

socket_path: Path

The socket to connect to (fastsandbox.connect(daemon.socket_path)).

start async

start() -> None

Starts listening.

stop async

stop() -> None

Stops listening and closes held sessions.

route

route(method: str, pattern: str) -> Callable[[Handler], Handler]

Registers a handler for method on pattern, e.g. /sandbox/{name}/ports.

It comes ahead of the built-in routes.

on_exec

on_exec(handler: ExecHandler) -> ExecHandler

Decides what commands print and exit with; usable as a decorator.

It answers the commands run with exec, those attached (Sandbox.attach, which an agent task runs its agent with), and agent sessions, given [agent, *args]: the agent of the sandbox and the arguments of the session.

requests_to

requests_to(method: str, pattern: str) -> list[Request]

The recorded requests matching method and pattern.

Request dataclass

Request(method: str, path: str, query: dict[str, str], headers: dict[str, str], body: bytes, params: dict[str, str] = dict())

A request the fake daemon received.

Attributes:

Name Type Description
method str

HTTP method.

path str

Path, without the query string.

query dict[str, str]

Query parameters, the last value of each.

headers dict[str, str]

Headers, lowercase names.

body bytes

Raw body.

params dict[str, str]

Parameters of the matched route, e.g. {"name": "job-42"}.

json

json() -> Any

The body, decoded as JSON (None when empty).

Response dataclass

Response(status: int = 200, body: Any = None, lines: list[Any] | None = None, hold: bool = False, raw: bytes | None = None, content_type: str = 'application/octet-stream')

An answer of the fake daemon.

Attributes:

Name Type Description
status int

HTTP status.

body Any

A JSON value, sent as JSON.

lines list[Any] | None

JSON values sent as an NDJSON stream instead of body.

hold bool

Keeps the (empty) streamed body open until the client leaves, as the daemon does for a session.

raw bytes | None

Bytes sent as they are instead of body, e.g. a tar archive.

content_type str

The content type of raw.

PublishedKit dataclass

PublishedKit(descriptor: dict[str, Any], entrypoint: list[str] = list(), cmd: list[str] = list())

A published kit the fake daemon pulls and exports, so that kits are forged from it.

Attributes:

Name Type Description
descriptor dict[str, Any]

The kit descriptor its image carries, its spec.yaml as JSON.

entrypoint list[str]

The entrypoint of its image.

cmd list[str]

The command of its image.

export

export() -> bytes

The image as GET /docker/images/export streams it: the small files of its export.

fake_package

fake_package(directory: str | Path, *, agent: str = 'shell', sbx_version: str = 'v0.54.1', artifact: dict[str, Any] | None = None) -> KitPackage

A kit package with no image, for the fake daemon.

It is written into directory, and creates sandboxes without the sbx CLI.

Parameters:

Name Type Description Default
directory str | Path

Where to write the package.

required
agent str

The agent of its kit, e.g. shell or claude.

'shell'
sbx_version str

The sbx version it claims to be compiled with.

'v0.54.1'
artifact dict[str, Any] | None

The kit artifact. An agent is described by its manifest (name, binary, runOptions) and the hosts it needs by caps.network.allow, e.g. {"manifest": {"name": "claude", "binary": "claude"}, "caps": {"network": {"allow": ["api.anthropic.com:443"]}}}.

None