Write a sandbox plugin
Goal: add a sandbox backend and prove it with the sandbox contract.
Prerequisites
- A Python package with an
afe.pluginsentry point, as in In-process plugin. - A backend that can run
sh,catandpython3in its test image.
Steps
-
Declare the
sandboxesport. Give it aconfig_schemafor the keys ofsandbox.options. ListhostFolderincapabilitiesonly if the backend can mount a host folder.PortSpec(port="sandboxes",name="my-backend",config_schema={"type": "object", "properties": {"gateway": {"type": "string"}}},capabilities=[],) -
Implement
SandboxProviderfromafe_sdk.sandbox:openandremove_ticket. The sandbox it returns implementsexec,run,stream,putandget.execreturns anExecResult. Keep at mostMAX_OUTPUT_BYTES(100,000 bytes, fromafe_sdk.sandbox) of the output, the end of it, and settruncated=Truewhen you cut. Read the streams withread_boundedso a noisy command never fills memory: the peak is about 400 KB per command whatever the output size.get,putandstreamare not capped. -
Add the optional ports you can support:
configure, to receiveSandboxRuntimeConfigwithoptions.mark_ticket, to annotate a ticket's objects with its state.suspend_ticket, to release a paused ticket's compute and keep its volume.
-
Subclass
SandboxContractin your tests. Provide theprovider,specandticketfixtures.from afe_core.contract import SandboxContractclass TestMyBackendContract(SandboxContract):network_isolation = Falseegress_allowlist = Falsehost_folder = Falseasync 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): ... -
Implement the four hooks:
vanishremoves the ticket's sandbox behind the engine's back.created_objectsdumps 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_argvsreturns every argument list the backend passed to a process.sandbox_identityreturns a token that changes when the sandbox is recreated.
-
Switch on the capability flags your environment can prove. Set
egress_reachable_hostandegress_blocked_hostwhen you switch on the egress flags. -
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
folderproject is refused. The plugin does not listhostFolder. Add it only if the backend mounts a host folder.