Sandbox package
Run commands in sandbox sessions and expose one destructive agent tool.
Use @yolk-sdk/sandbox when an agent needs an isolated workspace for commands, scripts, or generated files.
For lifecycle, source, env, ports, and approval policy, see Sandbox lifecycle and safety. On Cloudflare Workers, see Cloudflare Containers.
Install:
pnpm add @yolk-sdk/sandbox@canary @yolk-sdk/agent@canary effect@4.2.0Run a command
import { Effect, Layer } from 'effect'
import { Sandbox } from '@yolk-sdk/sandbox'
import { makeInMemorySandboxStateStoreLayer } from '@yolk-sdk/sandbox/testing'
import { makeVercelSandboxLayer } from '@yolk-sdk/sandbox/vercel'
const SandboxLayer = makeVercelSandboxLayer({ sandboxSessionId: 'user-session-123' }).pipe(
Layer.provide(makeInMemorySandboxStateStoreLayer())
)
const program = Effect.gen(function* () {
const sandbox = yield* Sandbox
return yield* sandbox.run({ command: 'node --version' })
}).pipe(Effect.provide(SandboxLayer))
const result = await Effect.runPromise(program)Production apps should replace the in-memory state store with durable storage.
Add the agent tool
import { Effect } from 'effect'
import { makeSandboxToolModule } from '@yolk-sdk/sandbox/agent'
const sandboxTools = await Effect.runPromise(
makeSandboxToolModule({
workspaceDescription: 'A temporary Node.js workspace.',
lifecycleDescription: 'Disposable sandbox; files may reset after expiry.',
previewPorts: [3000]
}).pipe(Effect.provide(SandboxLayer))
)Resolve sandboxTools with @yolk-sdk/agent/tools, pass the tool definitions into the loop, and provide a Sandbox layer to the tool executor.
Core command results
Sandbox.run returns SandboxCommandResult. Command failures are result data, not transport failures:
- nonzero exit code sets
exitCode - timeout sets
timedOut - workspace recreation sets
workspaceReset
Workspace path types
Raw command cwd inputs remain strings. normalizeWorkspaceCwd returns a
NormalizedWorkspaceCwd branded string; both are exported from @yolk-sdk/sandbox. The normalizer
resolves dot segments and rejects absolute paths, NUL, and workspace escape. It preserves whitespace
that belongs to remaining directory names. The brand records lexical validation, not filesystem
or symlink confinement.
SandboxApi may also transfer files: writeFile({ path, bytes }) and readFile({ path, maxBytes })
take workspace-relative paths (normalizeWorkspacePath returns a NormalizedWorkspacePath).
Absolute paths and escapes fail with SandboxInputError invalid_path, a missing file with
file_not_found, and a read past maxBytes with file_too_large. The Vercel and Cloudflare
layers implement both; custom VercelSandboxHandles need readFile. @yolk-sdk/files uses them for
file_to_sandbox and file_from_sandbox.
Agent tool results
The @yolk-sdk/sandbox/agent adapter converts command failures into agent tool results:
- nonzero exit code returns a tool result with
isError: true - timeout returns a tool result with
isError: true - provider/config/state failures become safe
ToolErrors
What the host provides
Your app owns the scoped sandboxSessionId, durable state store, env/secrets, source/snapshot/git policy, cleanup, auth, approvals, UI, diffs, commits, PRs, and audit logs.