Architecture Overview
The @purista/sandbox package is designed as a decoupled, message-driven component within the PURISTA ecosystem.
Core Components
1. Sandbox Runtime
A standard PURISTA service that exposes commands for sandbox lifecycle management (createSandbox, ensureSandbox, destroySandbox) and execution (executeBash).
2. Sandbox Drivers
The service is driver-agnostic. It interacts with virtualization technologies via a common SandboxDriver interface. This allows the same business logic to run against Docker, Podman, Lima, or Firecracker.
3. Sandbox Registry
A specialized resource that tracks active sandboxes. It uses the PURISTA State Store for persistence. Key features include:
- Tenant Isolation: Ownership is derived from message
tenantId+principalIdand stored asorganizationId+userIdplus explicitprojectId. - Self-Healing: On startup, the service reconciles its internal registry only from runtimes that can recover the full owner tuple from labels/tags.
Message Flow
- Request: A user or agent sends
createSandboxorensureSandboxwithtenantId/principalIdin the PURISTA message andprojectIdin the payload. - Provisioning: The service calls
driver.createSandbox(). - Registration: The new sandbox metadata is stored in the
SandboxRegistry(State Store). - Execution: AI agents use the
executeBashcommand. The service verifies ownership via the registry before delegating to the driver. - Teardown: When a task is finished,
destroySandboxis called, cleaning up both the infrastructure and the registry entry.
Lifecycle Rules
createSandboxis explicit and fails with409 Conflictif an owner already has a sandbox.ensureSandboxis the idempotent path. It returns the current healthy sandbox or recreates an unhealthy one.- Drivers must not return partial metadata during reconciliation. If owner metadata cannot be recovered,
scanRunningSandboxes()must return[]for those instances.
Resource-Based Injection
Following the PURISTA v1 patterns, dependencies are injected as resources:
driver: The chosen virtualization implementation.registry: The state-store-backed registry.
This ensures that handlers remain pure and easily testable by mocking these resources.
