Skip to content

Events and prompts

The daemon publishes what happens to its sandboxes as an event stream, and the requests that wait for a human decision as a prompt stream. This page covers both, and the Stream they are read through.

Events

docs_src/client/events.py
import asyncio

import fastsandbox
from fastsandbox.models import EventKind


async def main() -> None:
    client = await fastsandbox.connect()
    events = await client.events.subscribe_live()

    async def watch() -> None:
        async for event in events:
            if event.kind is EventKind.SANDBOX_LIFECYCLE:
                print(event.action, event.sandbox_name)

    watcher = asyncio.create_task(watch())
    async with client.create(kit="shell"):
        await asyncio.sleep(1)
    await asyncio.sleep(1)
    watcher.cancel()
    await events.aclose()


if __name__ == "__main__":
    fastsandbox.run(main())

client.events subscribes in one of two ways:

  • subscribe() first replays recent state, ends the replay with a sync event (is_sync), then sends the live events: what a program needs to build its view of the sandboxes, then keep it up to date;
  • subscribe_live() sends the live events only.

Subscribe before the actions to follow: the example above subscribes, then creates a sandbox, and sees its creation.

A DaemonEvent has an event_type, an action (created, started, stopped, deleted, published…), the sandbox_name it is about (is_about(name)) and its data. kind sorts it into an EventKind:

Kind Events
SYNC the end of the replay
SANDBOX_LIFECYCLE sandboxes created, started, stopped, deleted
SANDBOX_PORTS ports published and unpublished
POLICY_NETWORK network activity
OTHER any other family; event_type names it

Streams

The event stream never ends on its own. A Stream is read with async for; aclose(), or leaving its async with block, closes its connection to the daemon; a closing that fails on the way out never masks the error of the block (see Leaving a block whose cleanup fails). An error while reading, a dropped connection for instance, is raised by the iteration, as a SandboxError.

The other streams of the daemon are read the same way: the progress of a creation (create_stream()) and of an image pull (pull_stream()), the chunks of an image export (client.images.export_stream()), and the prompts.

Approval prompts

A prompt is a request a sandbox raises for a human decision. client.prompts.subscribe() gives the pending prompts, then a synced line (is_synced), then the new prompts as they are raised; respond(id, option_id) answers one with the identifier of one of its options, and raises NotFoundError for an unknown prompt.

docs_src/client/prompts.py
import fastsandbox


async def main() -> None:
    client = await fastsandbox.connect()
    pending = []
    async with await client.prompts.subscribe() as prompts:
        async for prompt in prompts:
            if prompt.is_synced:
                break
            pending.append(prompt)
    print(f"{len(pending)} prompts pending")


if __name__ == "__main__":
    fastsandbox.run(main())

Note

A connection a rule denies is refused by the proxy, without a prompt. Prompts are presumably raised when no rule decides, which the default default-allow-all rule of sbx never lets happen: the shape of a prompt has not been observed yet. A PromptEvent has its action and id, and keeps every other field as an extra field (model_extra).