Introducing a new way to review your PRs.

Read the announcement

hatchet-dev / hatchet

🪓 An orchestration engine for background tasks, AI agents, and durable workflows

View on GitHub

31 documents · Updated Nov 23, 2026

docs / sandbox-sdk-and-provider-management

Sandbox SDK and provider management

The TypeScript sandbox SDK gives Hatchet tasks a durable workspace with a shell and files. Hatchet keeps the task and snapshot flow consistent while a connected vendor runs the sandbox machines. The console lets tenant users connect that vendor, set its defaults, watch its health, and control whether it can serve sandbox work.

TypeScript sandbox SDK

The SDK is exposed by importing @hatchet-dev/sandbox alongside the Hatchet TypeScript client. This adds hatchet.sandbox, so application code uses Hatchet for sandbox actions without calling a vendor API directly. See the parent page Sandbox execution and providers for the wider execution model.

Sandbox lifecycle

The Sandbox resource represents one workspace that can be started, reused, stopped, or removed. Each SDK action runs as a Hatchet task and can produce a snapshot. A later connect can restore the latest workspace state, or a new sandbox can fork from an earlier snapshot.

The main lifecycle is:

  1. create starts a new workspace. It can select a provider, template, ID, metadata, idle limit, resource size, or source snapshot.
  2. exec runs a shell command and returns output, an exit code, and snapshot timing data.
  3. connect finds a workspace by ID and starts it again when it is not live.
  4. kill stops the live workspace but keeps its snapshots.
  5. list finds workspaces by ID or metadata.
  6. delete removes matching workspaces and their stored snapshots and files.

This makes a sandbox suitable for a durable task that may resume after a process crash. A command failure is returned through exitCode; it is not an SDK exception. A command timeout uses exit code 124.

References: sdks/…/README.md• L10-14 sdks/…/README.md• L78-110 sdks/…/sandbox.ts• L245-329

Sandbox sizing

Sandbox creation can request a machine size through resources. The request can set whole CPU cores, memory, or disk. Numeric memory and disk values mean MiB. Strings must include a unit such as 512Mi, 4Gi, or 8G.

Provider support controls the result. A provider can ignore an unsupported size request or fail creation with its own limit message. Callers must treat sizing as a provider capability rather than a universal guarantee. A size request cannot be used with a fork when the source provider owns the size.

References: sdks/…/README.md• L87-99 sdks/…/sandbox.ts• L75-101 sdks/…/sandbox.ts• L134-163 sdks/…/CHANGELOG.md• L31-35

Workspace files

The sandbox.files resource provides file access inside the workspace. It keeps paths relative to that workspace and supports reads, writes, directory inspection, moves, copies, removal, and URL transfers.

Small file data moves through Hatchet. Larger data uses provider storage behind the same SDK methods. Callers keep one file API while the transfer path changes. Invalid paths and transfer errors use FilesystemError codes that let callers handle the cause. A durable replay can renew an expired signed URL before it retries a staged read or write.

References: sdks/…/README.md• L113-127 sdks/…/files.ts• L37-57 sdks/…/files.ts• L101-218

Task durability and telemetry

The SDK maps sandbox actions to the tenant's registered sandbox task. It supports the current hatchet-sandbox task and the legacy brickie-sandbox task, then keeps the selected task for the Hatchet client. Calls made inside a durable task become durable steps. A fresh run is available when the SDK must start a new task execution.

Sandbox run metadata uses hatchet.sandbox.* tags for the function, sandbox ID, image, and source snapshot. These tags connect task traces and run filters to the workspace that they serve. On first use, the SDK enables Hatchet OpenTelemetry instrumentation when the application has the needed telemetry packages. Applications can turn this behavior off with HATCHET_SANDBOX_OTEL=false.

References: sdks/…/CHANGELOG.md• L13-23 sdks/…/CHANGELOG.md• L41-52 sdks/…/sandbox.ts• L15-51 sdks/…/sandbox.ts• L206-242

SDK validation

The SDK checks request options and provider results at the boundary of each sandbox task. This rejects bad input before a task is sent and turns malformed provider output into a clear schema error instead of a later missing-field failure.

The checks cover the public lifecycle, command, file, transfer, and resource options. They also reject unknown option keys, empty IDs or paths, invalid resource quantities, unsafe transfer URLs, and invalid provider results. Valid requests still depend on provider limits and state.

References: sdks/…/CHANGELOG.md• L21-23 sdks/…/sandbox.ts• L54-194 sdks/…/files.ts• L60-95

Wire contract

The @hatchet-dev/sandbox/wire entry point exposes the shared task protocol for code that runs the sandbox task itself. It defines the shapes for task turns, results, commands, sandbox records, metadata, and machine sizes. A separate task runtime can use the same contract without loading the full SDK entry point.

The wire contract keeps provider-facing size fields in MiB and carries operation names such as $create, $command, and $fs.read. The main SDK uses this contract when it sends turns and parses results. Other task runtimes can import it as a small shared boundary.

References: sdks/…/package.json• L23-35 sdks/…/wire.ts• L5-53 sdks/…/sandbox.ts• L5-13

Provider lifecycle and configuration

A sandbox provider is a vendor connection owned by a tenant. Hatchet uses the connection and its settings to serve the SDK while the vendor account remains responsible for the sandbox machines. The supported provider kinds are Daytona, E2B, Archil, and Freestyle.

Provider selection

The SDK does not require application code to know a vendor API. With one provider, a create request can use the tenant default. With more than one, the request selects a provider by its configured name. A sandbox created from a snapshot stays tied to the provider that owns that snapshot.

The provider name is also carried as sandbox metadata when the SDK creates a sandbox. This connects the user-facing provider choice to the execution request without adding a separate vendor client to the application.

References: sdks/…/README.md• L61-74 sdks/…/README.md• L87-98 sdks/…/sandbox.ts• L75-92 sdks/…/sandbox.ts• L263-279

Provider configuration

Provider configuration controls the defaults and limits that apply when the SDK creates work. The console collects a required API key during creation and supports key rotation during editing. It stores the key encrypted and does not show it again.

The shared settings are:

SettingPurpose
NameIdentifies the provider in the console and SDK selection.
Default templateSupplies the vendor image or template when a sandbox request omits one.
Maximum idle timeSets the longest idle period before the vendor removes a sandbox.
Provider configHolds vendor-specific settings such as endpoint, work directory, snapshot, or region values.
LabelsAdds default metadata to sandboxes created through the provider.
EnabledAllows or blocks sandbox turns while preserving existing sandboxes.

The default template remains vendor-specific. It can be an image, a base template, or a snapshot name depending on the provider kind. Advanced settings also cover a working directory and whether to snapshot every turn. Vendor-specific choices include a self-hosted E2B API URL or an Archil region that matches the API key.

References: frontend/…/form-fields.tsx• L46-89 frontend/…/form-fields.tsx• L181-293 frontend/…/kinds.ts• L14-103 frontend/…/use-sandbox-providers.tsx• L144-189

Provider state

The console reduces provider health to four states. Enabled providers move between pending, healthy, and unhealthy as the sandbox operator checks their credentials. Disabled providers remain visible and keep their vendor sandboxes, but all sandbox turns through them fail until they are enabled again.

StateMeaning
PendingNo provider check has completed.
HealthyThe vendor accepted the credentials.
UnhealthyThe last vendor check rejected the credentials.
DisabledThe provider serves no sandbox turns.

References: frontend/…/provider-status-badge.tsx• L10-47 frontend/…/disable-provider-dialog.tsx• L7-17

Provider removal

Deleting a provider removes the tenant's connection and deletes the Hatchet sandbox state tied to it. The SDK can no longer connect to those sandboxes, restore their snapshots, or replay runs that depend on them. The confirmation flow warns that vendor machines and snapshots in the vendor account are not affected. Disable is the safer control when the tenant needs to stop work without losing sandbox access.

References: frontend/…/provider-actions.tsx• L77-100 frontend/…/delete-provider-dialog.tsx• L20-32 frontend/…/delete-provider-dialog.tsx• L107-152

Console management flows

The console provides a list view for provider selection and a detail view for configuration and health. Both views refresh about every five seconds, so changes and operator events become visible without a manual reload.

Sandbox entitlement gate

Provider management is available only when the tenant has the sandbox operator entitlement. A self-hosted control plane shows that sandboxes are Cloud-only. A Cloud tenant without the entitlement is directed to support instead of seeing provider controls.

References: frontend/…/gate.tsx• L8-35 frontend/…/index.tsx• L19-24 frontend/…/index.tsx• L48-53

Provider creation flow

A user starts with Connect provider, chooses a vendor, and then enters the vendor key and shared settings. The form checks the name, key, idle limit, JSON-like settings, and label pairs before it sends the connection request. On success, the console opens the new provider's detail page.

The flow joins the tenant, the selected vendor, and the provider defaults into one connection:

flowchart LR
    A[Connect provider] --> B[Choose vendor]
    B --> C[Enter key and settings]
    C --> D[Validate form]
    D --> E[Create tenant provider]
    E --> F[Open provider detail]
    E --> G[Refresh provider list and status]

References: frontend/…/index.tsx• L94-139 frontend/…/create-provider-dialog.tsx• L73-91 frontend/…/use-sandbox-providers.tsx• L192-240

Provider editing flow

An authorized user can edit the provider name, defaults, vendor settings, labels, and enabled state. The vendor kind cannot change in place, and an empty API key keeps the current key. A successful update refreshes the list, detail data, and event data so the console shows the new state.

The enabled switch is also available on the detail page and in the action menu. Disabling opens a confirmation that explains the effect on all sandbox operations. Both paths use the same update contract, which keeps a pause or resume action separate from credential rotation.

References: frontend/…/edit-provider-dialog.tsx• L34-81 frontend/…/provider-actions.tsx• L34-100 frontend/…/disable-provider-dialog.tsx• L7-61 frontend/…/index.tsx• L136-171 frontend/…/use-sandbox-providers.tsx• L242-272

Provider monitoring flow

The list shows each provider's vendor, state, and creation time. The detail view adds the default template, vendor configuration, labels, credential status, the last error, and an event log. The event log covers credential checks, sandbox creation and destruction, turn failures, snapshot failures, and provider health changes.

The monitoring path links an operator observation to the provider state shown to the user:

sequenceDiagram
    participant O as Sandbox operator
    participant P as Provider record
    participant C as Console
    O->>P: Check credentials or process sandbox turn
    P-->>C: Health or sandbox event
    C->>P: Poll current provider and events
    P-->>C: Status, error, and event history
    C-->>C: Show badge, detail status, and log

References: frontend/…/use-sandbox-providers.tsx• L21-43 frontend/…/index.tsx• L65-80 frontend/…/index.tsx• L175-265 frontend/…/events-log.tsx• L11-49

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the SDK provider selector aligned with the console's provider name and metadata contract.
  • Keep provider defaults aligned with the vendor-specific template and configuration rules.
  • Keep resource units and provider capability rules aligned with the create contract.
  • Preserve the distinction between disabling a provider and deleting its tenant connection and Hatchet sandbox state.
  • Keep status labels and event kinds aligned so a provider health change is shown consistently in the list, detail page, and event log.
  • Keep sandbox entitlement behavior aligned across the provider list and provider detail routes.
  • Keep the registered sandbox task name and run metadata keys aligned when changing task routing or dashboard filters.
  • Keep the public wire export aligned with the task input and output contract when changing SDK options or provider results.

Important failure modes

TriggerConsequenceGuard
Provider credentials are rejectedThe provider becomes unhealthy and sandbox work can fail.Check the status error and credential events before using the provider.
A provider is disabledAll sandbox operations on it fail while existing sandboxes remain at the vendor and can idle out.Re-enable it when the tenant should route work to it again.
A provider is deletedHatchet sandbox state and snapshots are removed, so dependent runs cannot replay or reconnect.Disable the provider when access may be needed later.
SDK input contains an unknown key or invalid valueThe SDK rejects the request with a ZodError before it sends a task.Use the public option shapes and keep resource quantities unit-qualified.
A provider returns malformed task outputThe SDK rejects the result instead of exposing incomplete sandbox or file data.Keep provider output aligned with the shared wire contract.
SDK connect receives an unknown sandbox IDThe connect call fails because no sandbox can be restored.Persist and reuse the sandbox ID returned by create.
A command exits non-zero or times outThe SDK returns a failed exit code instead of throwing for the command.Inspect exitCode, stderr, and timeout code 124.
A file path leaves the workspaceThe file action raises a filesystem error.Use workspace-relative paths and handle the error code.
A signed staging URL expires during durable replayA staged file transfer can fail unless the provider signs the file again.Let the SDK renew the URL and keep the original sandbox operation durable.
A resource string has no unitSDK validation rejects the create request instead of treating the string as MiB.Use a number for MiB or a quantity with a unit.
A tenant switches between sandbox task names while durable runs are activeExisting runs can fail on resume with a non-determinism error.Drain in-flight runs before changing the served task name.

Key files

FileWhy to read it
sdks/…/README.mdProduct view of the SDK, provider setup, and public calls.
sdks/…/sandbox.tsPublic sandbox lifecycle, provider selection, sizing, validation, and task routing behavior.
sdks/…/files.tsWorkspace file actions, transfer rules, staging, and file errors.
sdks/…/wire.tsShared task input, output, resource, and sandbox record contract.
sdks/…/CHANGELOG.mdRecent SDK behavior changes and provider capability notes.
frontend/…/use-sandbox-providers.tsxProvider queries, mutations, form validation, and request contracts.
frontend/…/form-fields.tsxShared provider configuration and enable controls.
frontend/…/disable-provider-dialog.tsxConfirmation and operator-facing effects for pausing a provider.
frontend/…/delete-provider-dialog.tsxConfirmation and data-loss effects for removing a provider.
frontend/…/index.tsxProvider list and creation entry point.
frontend/…/index.tsxProvider detail, status, and event monitoring flow.
frontend/…/gate.tsxSandbox entitlement and Cloud availability gate.