Skip to main content

Write a sandbox plugin

Goal: add a sandbox backend and prove it with the sandbox contract.

Prerequisites​

  • A Python package with an afe.plugins entry point, as in In-process plugin.
  • A backend that can run sh, cat and python3 in its test image.

Steps​

  1. Declare the sandboxes port. Give it a config_schema for the keys of sandbox.options. List hostFolder in capabilities only if the backend can mount a host folder.

    PortSpec(
    port="sandboxes",
    name="my-backend",
    config_schema={"type": "object", "properties": {"gateway": {"type": "string"}}},
    capabilities=[],
    )
  2. Implement SandboxProvider from afe_sdk.sandbox: open and remove_ticket. The sandbox it returns implements exec, run, stream, put and get.

    exec returns an ExecResult. Keep at most MAX_OUTPUT_BYTES (100,000 bytes, from afe_sdk.sandbox) of the output, the end of it, and set truncated=True when you cut. Read the streams with read_bounded so a noisy command never fills memory: the peak is about 400 KB per command whatever the output size. get, put and stream are not capped.

  3. Add the optional ports you can support:

    • configure, to receive SandboxRuntimeConfig with options.
    • mark_ticket, to annotate a ticket's objects with its state.
    • suspend_ticket, to release a paused ticket's compute and keep its volume.
  4. Subclass SandboxContract in your tests. Provide the provider, spec and ticket fixtures.

    from afe_core.contract import SandboxContract


    class TestMyBackendContract(SandboxContract):
    network_isolation = False
    egress_allowlist = False
    host_folder = False

    async def vanish(self, provider, ticket_key): ...
    async def created_objects(self, provider, ticket_key): ...
    async def recorded_argvs(self, provider): ...
    async def sandbox_identity(self, provider, ticket_key): ...
  5. Implement the four hooks:

    • vanish removes the ticket's sandbox behind the engine's back.
    • created_objects dumps everything the backend created, the environment included. Docker is the one exception: its container config holds the value, so it leaves the environment out, as R1 allows.
    • recorded_argvs returns every argument list the backend passed to a process.
    • sandbox_identity returns a token that changes when the sandbox is recreated.
  6. Switch on the capability flags your environment can prove. Set egress_reachable_host and egress_blocked_host when you switch on the egress flags.

  7. Run the suite. A case with its flag off is skipped, and the skip names the flag.

    uv run pytest plugins/my-backend/tests

Troubleshooting​

  • The canary case fails. An environment value reached an argument list or a created object. Pass the value by name and let the backend read it from its own environment.
  • A recreate case passes the path check and fails the identity check. The backend ignored the new specification. Recreate the sandbox on a signature change.
  • A folder project is refused. The plugin does not list hostFolder. Add it only if the backend mounts a host folder.

See also​