mgamadeus / ddd-agent-sandbox
Agent sandbox for mgamadeus/ddd: isolated runner sessions (gVisor, one session per pod), persistent workspaces with a pluggable store, project image and preload, administration with audit, and beside ddd-agents the agent's shell tools and document skills
Requires
- php: >=8.3
- ext-imagick: *
- mgamadeus/ddd: ^3.40.2
Requires (Dev)
- mgamadeus/ddd-agents: ^0.3.2
- mgamadeus/ddd-clouddrive: ^0.2.0
- phpunit/phpunit: ^9.5
Suggests
- async-aws/s3: The object store of the built-in workspace store on S3-compatible storage
- mgamadeus/ddd-agents: ^0.3.2 — adds the agent's shell tools (agent.sandbox.shell), the skills Agent.Sandbox.Shell and Agent.Sandbox.Documents, the flush at a settled turn, the workspace activity of the admin screen and the eval participant
- mgamadeus/ddd-clouddrive: ^0.2.0 — beside ddd-agents: the drive store (AI.Sandbox.workspaces.store = drive): the workspace of an agent space is the working copy of its own drive, the library space mounted below drivespaces/
Provides
None
Conflicts
- mgamadeus/ddd-agents: <0.3.2 || >=0.4.0
- mgamadeus/ddd-clouddrive: <0.2.0 || >=0.3.0
Replaces
None
README
The agent sandbox for mgamadeus/ddd: isolated runner sessions in which an agent (or any PHP caller) works on a persistent workspace as it would on a computer — a shell, Python with the office libraries, file tools — without network and without access to anything but its workspace.
- Runner (
runner/, Go): one session per pod, gVisor on the node, every session process under its own uid with rlimits, seccomp and a memory cgroup, a disk quota, persistent shells and background jobs. Imageghcr.io/ddd-framework/ddd-agent-sandbox-runner:<version>(toolkit base) and:<version>-full(LibreOffice, Chromium headless shell, Node, Typst, fonts), released with the same tag as the Composer package.GET /healthzreportscontractVersion(= the package version),toolkit,imageConfigHash,capabilitiesandlimits. - PHP core (
DDD\Domain\AI\…\Sandboxes):SandboxRunnerClientService(the HTTP contract),SandboxWorkspacesService(workspaces by explicit key: claim and restore, write-behind save, idle release, capacity, expiry, preload), the portSandboxWorkspaceStorewith the built-in manifest store (manifest and contents by sha256 in an object store),SandboxSessionRulesService(pool, timeouts, background jobs, network per workspace), the image and deploy renderers, and the administration with its audit table and endpoints. - Agent part (only beside
mgamadeus/ddd-agents0.3.x): the MCP packageagent.sandbox.shell(shell,readFile,writeFile,editFile,searchFiles,viewWorkspaceImages), the skillAgent.Sandbox.Shelland the document skillsAgent.Sandbox.Documents→.Pptx,.Pdf,.Excel, the workspace of a call (space-<agentSpaceId>,account-<accountId>, an eval run's key), the flush at a settled turn, the state note, the eval participant and the workspace activity of the admin screen. Gated by the account rightuseSandbox(SandboxAccountRights::USE_SANDBOX). - Drive store (only beside ddd-agents and
mgamadeus/ddd-clouddrive0.2.x;AI.Sandbox.workspaces.store=drive): the workspace of an agent space IS the working copy of its own drive (see "Drive store" below). - Frontend (
web/, npm@mgamadeus/ddd-agent-sandbox): the RTK Query client of the admin routes and the admin screens (./screen) on the blocks of@mgamadeus/ddd-admin-ui.
Installation
composer require mgamadeus/ddd-agent-sandbox; for the admin screenpnpm add @mgamadeus/ddd-agent-sandbox.- The runner pool: render the overlay of
deploy/k8s/base(namespace, image, replicas, limits, callers) withphp bin/console app:agent-sandbox:render-deploy --output=deploy/sandbox/<stage>and apply it (kubectl apply -k deploy/sandbox/<stage>). The nodes need gVisor (RuntimeClassgvisor) and a StorageClasssandbox-workspacewith a hard size limit per volume; the pods pull with theghcr-pullsecret. - Configuration (each file replaces the module's file as a whole, keep every key):
config/app/AI/Sandbox/runner.php:enabled/AGENT_SANDBOX_ENABLED,host/AGENT_SANDBOX_RUNNER_HOST(the headless Service, e.g.sandbox-runner.sandbox.svc.cluster.local),port/AGENT_SANDBOX_RUNNER_PORT,pools,cacheGroup(shared across hosts, Redis),timezone, timeout caps,network,toolkitNote,deploy.config/app/AI/Sandbox/workspaces.php:store(manifestordrive),objectStore(sandbox-workspaces),keyPrefix,idleReleaseSeconds,maxSyncedFileBytes,uploadInBackground,storedWorkspaceQuotaBytes, the state note, the content cache,preloadDirectory.- An object store
sandbox-workspaces(config/app/Common/ObjectStorage.php,OBJECT_STORAGE_SANDBOX_WORKSPACES_*).
- In the deploy:
app:agent-sandbox:check-runner(exit 1 on a problem) andapp:agent-sandbox:seed(the preload). - The table
SandboxActionLogEntriesthrough the schema diff (app:entity:show-sql SandboxActionLogEntry). - Two worker loops:
app:agent-sandbox:sessions --sweepevery minute andapp:agent-sandbox:sessions --expireonce a day. Before a runner rollout:app:agent-sandbox:sessions --offload. - Admin: one controller with
use SandboxesControllerTrait;(operators only; the access hookthrowForbiddenUnlessSandboxesAccessAllowed()), and the screens from@mgamadeus/ddd-agent-sandbox/screenwithcreateSandboxesApi({ baseApi, apiPrefix }). - With ddd-agents: bind the skill in
config/app/AI/skillPackages.php('Agent.Sandbox.Shell' => [SandboxShellMCPPackage::class], the document skills to[]) and gate it inskillRequirements.php. The package requires the account rightuseSandbox(ddd-agents maps it to roles inAI.accountRights, by default the superadmins) and an admin with access to all resources (MCPRequiresAdminWithAccessToAllResources). - With ddd-clouddrive:
store=drive, the transfer proxy beside the runner pool (RUNNER_TRANSFER_PROXY_URL), and onceapp:agent-sandbox:migrate-to-drive(see "Drive store").
A project's sandbox
- Installed software:
config/app/AI/Sandbox/image.yaml—base(base or full),apt,python(exact pinsname==1.2.3),npm(exact pins, full only),fonts(paths or globs),files(project path → absolute image path),env.app:agent-sandbox:build-image --image=<registry path> --build --pushwrites the Dockerfile (FROMthe module image of the installed version) and builds it; the tag is<module version>-<config hash>. The hash is written into the image and reported by/healthz;check-runnerfails on drift. - Preloaded content: a folder, default
resources/Sandbox/preload/— templates, kits, sample files.app:agent-sandbox:seedmakes the store equal to the folder; every claim restores it read-only below$SANDBOX_PRELOAD(/workspace/.preload), never saved; astart.shat its top runs once per session. - Runtime rules —
SandboxSessionRulesServiceanswers per workspace: pool, timeout caps, background jobs, session environment. Override it (same-path subclass) for rules per agent space or role. - Network —
AI.Sandbox.runner.network.mode:off(default; the NetworkPolicy lets nothing out) orallowList(the sessions getHTTP(S)_PROXYof the egress proxy componentdeploy/k8s/egress-proxy, which lets onlyallowedHoststhrough).check-runner --isolationprobes from inside a session. - Capacity — one session per pod; when every pod is busy the idle sessions are released once, then the call is refused clearly (no queue).
Drive store
Beside ddd-agents and ddd-clouddrive 0.2.x (Composer conflict outside that range), AI.Sandbox.workspaces.store =
drive selects SandboxDriveWorkspaceStoreService, a SandboxLiveWorkspaceStore: the workspace service hands every
step of a key it serves to it, every other key stays in the manifest store.
- Which keys:
space-<agentSpaceId>and a key a call of the space bound (an eval run's), when the agent space has an own drive (ddd-clouddrive'sDriveSpaceMountResolverService::findOrCreateOwnDriveSpace()); a key with an eval scratch drive.account-<id>, keys made from files (createWorkspaceFromFiles()) andsystem.*stay in the manifest store; an agent space without an own drive (link table missing, host gives none) too. - Claim: the runner gets the own drive's base map (latest versions plus the saved, uncommitted entries) with
restore URLs through the transfer proxy, and every further space of the mount resolver read-only below
drivespaces/<name>/(the library asdrivespaces/library/,$SANDBOX_PRELOADpoints there, itsstart.shruns once per session). - After a call: the runner's status names the call's changes; the flush stages them in the working copy and the runner uploads the snapshots (presigned PUTs). Nothing is visible in the drive yet.
- Commit: at the end of a turn (settled-turn listener: the conversation's changes), at release (every change),
on
drive commitin the shell; the runner's base moves in the drive's after-commit hook (SandboxDriveWorkingCopyListener). A conflict stays uncommitted and comes back as one note of the next call. - Pull: at the first call of every turn, and at the next call when the working copy carries ddd-clouddrive's pull
mark (
DriveWorkingCopiesService::requestPull(), set by the browser file sink); both consume the mark. A version still in the malware scan is waited for (30 s), else held back with the cursor. driveCLI in the shell (status, diff, commit, pull, get, pin): its requests are answered by PHP under the rights of the conversation's owner.- Config
config/app/AI/Sandbox/drive.php(AI.Sandbox.drive.*):thresholdBytes(own-drive files above stay not local),mirrorBudgetBytes,mountTextMaxBytes,ignore,maxRequestRounds,libraryTitle. - Preload:
app:agent-sandbox:seedalso writes the preload folder into the library space (created on first seed; new or changed files only). - Moving existing workspaces:
app:agent-sandbox:migrate-to-drive [--workspaceKey=space-<id>] [--dryRun]copies the manifest-store workspaces of agent spaces into their own drives (working copymigration-<key>, origin MIGRATED; idempotent; the manifest store is left as it is, so switching back tomanifestfinds it unchanged).
Runner contract
runner/README.md. SandboxRunnerContract::CONTRACT_VERSION equals the package version; compatible is the same major
and at least MINIMUM_CONTRACT_VERSION, and while the major is 0 the same minor. With a store other than the manifest
store check-runner requires the capability drive.
Development
composer install && vendor/bin/phpunit docker run --rm -v "$PWD/runner:/src" -w /src golang:1.27.4-trixie sh -c 'go vet ./... && go test ./...' cd web && pnpm install && pnpm typecheck && pnpm test && pnpm build
Untrusted input: no byte from a workspace is interpreted by the server (pictures decoded by RasterImageDecoder,
documents parsed inside the sandbox, files served as untrusted content); raw sinks are listed with their reason in
config/untrusted-sinks-allowlist.php and checked by tests/UntrustedSinkScanTest.php.
License
MIT