Skip to main content
Restate Harness doesn’t provide sandboxes for commands and files. Your handler connects to a sandbox at a provider, such as Modal, and passes a client for it to the tools in the run context. The handler wraps each lifecycle call to the provider in restate.run, so a replay reuses the journaled sandbox reference instead of creating another sandbox.

Connect to a sandbox

The examples on this page use a small provider interface. Implement it with your provider’s SDK:
  • Store only the reference. State and the journal hold SandboxRef, the data that identifies the sandbox. connect builds a client from it in each attempt, without I/O, also when another process continues the request.
  • Make the lifecycle calls safe to repeat. If an attempt fails after the provider completed a call, but before Restate journaled the result, Restate calls it again. For example, name the sandbox after its key, and reuse a sandbox with that name in provision.
  • Keep credentials out of the sandbox. A command that the model runs can print environment variables and files into the conversation and the journal.

Keep files across requests

A Virtual Object keeps one sandbox per session. The first request provisions the sandbox. Each later request resumes it, and each request suspends it when the run ends, which stops the compute and keeps the files:
  • One run at a time. Restate runs one exclusive handler per key at a time, so only one run uses the sandbox at a time, without a lock of your own.
  • The reference in state. The handler stores the reference as soon as it has one, so the next request finds the sandbox even if this request fails. The finally block suspends the sandbox when the run completes, fails with a terminal error, or is cancelled. If you kill the invocation, finally doesn’t run and the compute keeps running, so the next resume reuses it or starts new compute. Set a timeout at your provider, so that the provider stops compute that no request suspends.
  • No retries for commands. After a failed command, the model receives the error and can inspect the sandbox. A crash before the result is journaled still runs the command again.
  • Files, not processes. Suspending stops the processes in the sandbox. Start long-running processes again in a later request.
To provision the sandbox only for runs that use it, acquire it in the first sandbox tool call instead of at the start of the request. The reference architecture does this, and lets parallel tool calls wait for one acquisition.

One sandbox per request

For a self-contained task, run the agent in a service. Provision a sandbox at the start of the request, and destroy it in finally. The coding agent lesson does this with a Modal sandbox. A request that provisions its own sandbox shouldn’t use a conversation: the history of a later request would refer to files in a sandbox that no longer exists.

Providers

The reference architecture implements this lifecycle for Modal. A Modal Volume per agent keeps the files, and each run that uses the sandbox mounts the Volume in new Sandbox compute. See its Modal provider and its sandbox documentation, which covers retries, cancellation, and how to add a provider.