Custom Sandbox Providers
A custom sandbox provider connects eve to a backend eve doesn't ship, such as your own container service or virtual machine pool. You write a connection that runs commands and moves file bytes in one sandbox, and four methods that build, create, reconnect, and delete sandboxes. eve builds the rest of the sandbox API from them.
Define a provider
defineSandboxProvider() from eve/sandbox/provider takes a name and an environment() function that returns the four methods. This example targets @acme/sdk, a hypothetical client for a machine service that can take snapshots:
Use the provider from a sandbox file like any built-in environment. Every provider accepts the standard prepare option:
environment(options) receives the caller's options without prepare, or {} when the sandbox file passes none. Make every field optional, or require the caller to pass it.
TypeScript infers the option, artifact, state, and extension types from the implementation. Write start() before resume() so the state type comes from start()'s return value, or annotate resume()'s state parameter.
Implement the connection
A SandboxConnection runs commands and reads and writes file bytes in one sandbox:
spawn({ command, workingDirectory, env, signal })runscommandas a Bash login-shell script (bash -lc) inworkingDirectory, with the sandbox's environment plus the per-callenv. Aborting kills the process tree. It resolves to a process withstdoutandstderrbyte streams,wait(), andkill(). When the call passesstdin: true, the process must also have a writablestdinstream, or eve kills the process and rejects the call.readFile({ path, signal })resolves to the file's bytes or a byte stream, or tonullonly when the path does not exist. A directory or an unreadable path rejects.writeFiles({ files, signal })writes every file, creating missing parent directories and overwriting existing files. It is the only write primitive.removePath({ path, force, recursive, signal })removes a file or directory.forceignores a missing path, andrecursiveremoves a non-empty directory.stop()releases compute and keeps the sandbox's files.close(), optional, releases what this process holds for the connection, such as a local interpreter or child processes, without touching the sandbox. eve calls it when the step that opened the connection ends, afterstop(), and before deletion. A connection lasts at most one step, and the next step resumes the sandbox through a new connection.extensions, optional, is a plain object whose own properties eve adds to the sandbox, such assetNetworkPolicy(). Use closures rather than class methods, because eve doesn't copy inherited members.workspaceRoot, optional, is the absolute sandbox directory where commands start and relative paths resolve. Omit it to use/workspace, as every built-in provider does. The directory must exist before eve runs a command in it.
Every path a connection receives is absolute, and workingDirectory is always set. The built-in file tools, inbound attachments, and GitHub checkouts use the same root. eve throws when it opens a connection whose workspaceRoot is not absolute:
eve builds the rest of the sandbox API from these methods, including run(), the binary and text read and write variants, and resolvePath(). An extension member with the name of one of those methods, or stop or delete, is a type error. eve also throws when it opens the connection:
Type the connection with its extensions, such as SandboxConnection<{ setNetworkPolicy(policy: SandboxNetworkPolicy): Promise<void> }>. environment.open() and ctx.getSandbox(environment) then return a sandbox that includes them.
Implement the lifecycle
eve calls the provider's methods at these points:
| When | eve calls |
|---|---|
eve build, or the first sandbox access in eve dev | prepare(ctx), and stores the artifact it returns with the deployment |
| The first sandbox access in a session, or prewarming | start(ctx, options, artifact) from environment.open(options) in the selector |
| The first access to a recorded sandbox in a step | resume(ctx, artifact, state) |
| The step that opened a connection ends | connection.close(), for a turn's step, a "use step" function, or an invokeTool call |
sandbox.stop() | connection.stop(), then connection.close() |
sandbox.delete() | connection.close(), then onSessionEnd(ctx, state, { reason: "deleted" }) |
| The eve server shuts down | connection.stop(), then connection.close(), on every connection eve still holds |
| The session that owns the sandbox ends | connection.close() on a connection the ending step still holds, then onSessionEnd(ctx, state, { reason }) with "completed", "expired", or "failed" |
- A prewarmed session calls
start()before its first message, and its first turn can call it again at the same time. - A turn does not resume its recorded sandbox just by starting. The first tool or file operation that needs it calls
resume(); a turn that never uses the sandbox leaves it alone. A turn that changes a dynamic skill's files also resumes it in the background to write or remove them. - Server shutdown always stops connections that are mid-step. eve logs shutdown failures and doesn't throw them.
- eve calls
close()at most once per connection, and calls it again only after a failure. A failedclose()never preventsonSessionEnd()from deleting the sandbox. stop()can run again after the session uses a stopped connection, so make it a no-op when the sandbox is already stopped.
Defining close() changes shutdown behavior. Without close(), eve keeps the connection between steps, so server shutdown stops its sandbox. With close(), eve releases the connection at the end of its step, so server shutdown between steps doesn't stop that sandbox. If your provider defines close() and runs remote compute, limit idle compute yourself, for example with an idle timeout.
prepare()
prepare() builds a reusable template and returns a JSON-compatible artifact, such as a snapshot or image id. It runs during eve build, so the deployment ships with the artifact. The production runtime never calls it.
A running sandbox keeps the template it started from. resume() receives the current deployment's artifact, but it reconnects to the existing sandbox.
ctx.isEmpty is true when the agent has no workspace files, skill files, or authored prepare, so the provider can skip building a template. See Bake files into templates for ctx.populate() and ctx.templateKey().
start()
start() returns the sandbox for ctx.session.id, and creates it when none exists. It also returns the smallest JSON-compatible state that resume() needs, which eve saves with the session.
Every call for one session id must return the same sandbox while it exists and is usable. This includes concurrent calls from other processes or deployments, which can race to create it.
Derive the sandbox's native name only from ctx.session.id and a provider-level namespace, such as a project or account. Never derive it from the open options or the artifact, which can differ between callers and deployments. Apply them only when start() creates the sandbox.
ctx.session.id is the id of the session the sandbox belongs to. For a subagent that shares its parent's sandbox, it is the parent's id. ctx.kind is "tool-session" for a keyed invokeTool call's sandbox and "session" otherwise.
resume()
resume() reconnects to the sandbox that state names and never recreates it. It doesn't receive open options, so put anything reconnection needs in the state.
When the sandbox no longer exists, throw SandboxSessionGoneError. eve then runs the selector again, which starts a fresh sandbox without the lost files. Let other failures, such as an outage, surface unchanged.
Validate the shape of your own state in resume(). When you change the shape incompatibly, raise stateProtocolVersion in the provider definition. State saved under another version starts a fresh sandbox instead of reaching resume().
onSessionEnd()
onSessionEnd() deletes whatever exists for ctx.session.id. state is null when the session never saved it, so find the sandbox from the session id. This happens, for example, when only a subagent opened the shared sandbox, or a step failed before saving state.
eve retries cleanup, so onSessionEnd() must be safe to repeat and must succeed when nothing exists. When every retry fails, eve logs the failure and the session's outcome doesn't change.
sandbox.delete() calls the same method with reason: "deleted" and the caller's signal. An invokeTool call without a key calls it when the call ends, if the tool requested the sandbox. A subagent that shares its parent's sandbox never calls it, because cleanup belongs to the parent.
The sets of reason and ctx.kind values, like other values eve passes into a provider, can grow in a minor release, so handle unknown values instead of switching exhaustively. See Stability policy.
Bake files into templates
In prepare(), call ctx.populate(connection) on the build instance before you capture it. populate() writes the agent's workspace files to the connection's workspace root and its skill files to $HOME/.agents/skills in one writeFiles() call. It then runs the authored prepare callback over the same connection. eve finds $HOME by running a command through spawn(). When the sandbox has no usable home directory, skills go to skills/ under the workspace root.
A provider that can mount host directories can skip writing every file. ctx.resourcesDirectory() resolves to a read-only host directory that contains workspace/ and skills/, or to undefined. Mount it in the build instance and pass its sandbox path:
With mountedAt, populate() copies <mountedAt>/workspace/. into the workspace root and <mountedAt>/skills/. into $HOME/.agents/skills with cp -a. When eve has no resource directory, populate() ignores mountedAt and writes the files instead.
ctx.templateKey(identity) returns a SHA-256 hex key. The key changes whenever identity, the workspace files, the skill files, or the sandbox file's source changes. Pass every option that affects the template, such as the base image, and use the key to name or look up a cached template.
identityaccepts JSON values,undefined(omitted), plain objects and arrays, and functions (hashed by their source).resourcesandsourceRevisionare reserved, and eve throws ifidentitysets either.- Only the sandbox file's own source counts. Changing a
preparecallback imported from another module doesn't change the key.
Only prepare() seeds files. A sandbox that start() creates gets them from the template, so a provider whose templates can't hold files can't provide workspace or skill files.
Keep sandboxes for keyed tool calls
Set toolSessions: true on the implementation to let keyed invokeTool calls keep their sandbox between calls, as Vercel Sandbox and just-bash do. eve runs the selector and start() on every keyed call, and calls connection.close() when the call ends. eve never calls onSessionEnd() for these sandboxes, so the provider must limit how long it keeps them, for example with snapshot expiration.
Without toolSessions, a keyed call fails when its tool requests the sandbox:
A keyed call whose tool never requests the sandbox runs normally. See MCP tool sessions.
Enforce network policy
eve has no built-in network control for custom providers. To offer one, accept a SandboxNetworkPolicy from eve/sandbox as an open option, a setNetworkPolicy() extension, or both. Reject any part of a policy the backend can't enforce instead of weakening it. The Docker provider, for example, accepts "allow-all" and "deny-all" and throws on anything else.
Set environment variables
SANDBOX_AGENT_ENV is { AI_AGENT: "eve" }, which tells agent-aware CLIs to skip interactive prompts. Apply it beneath your environment-level variables when you create the sandbox, as the built-in providers do. A per-call env passed to spawn() overrides both. If the backend has no sandbox-level environment, merge it in spawn() instead: { ...SANDBOX_AGENT_ENV, ...env }.
Report missing templates
Throw SandboxTemplateNotProvisionedError from start() or resume() when the artifact names an image, snapshot, or template that no longer exists:
eve returns the error to the caller and never retries or rebuilds the template at runtime. eve throws the same error when a deployment has no artifact for the sandbox. The provider decides how long to keep templates and when to delete them. See Sandbox troubleshooting.