Skip to content

Rivet.CreateSandbox reference

Source: packages/rivet/src/Sandbox.ts

agentOS VMs on Rivet, created from a SandboxTemplate: one VM per key, each its own Rivet actor on the Rivet Services pool, reached through the control plane’s gateway. A VM runs a Linux-compatible userland on WebAssembly and V8 isolates, keeps its files across sleep, and sleeps when idle unless a process is running.

Calls go over HTTP, so any runtime can make them (a Docker service, a Lambda, a Worker) without RivetKit. Bound in a runtime, the client reads the control plane’s address and token from its environment, as Rivet.DurableStream does; provide a services pool’s Layer (RivetDocker.ServicesPool, …).

import * as Rivet from "@alchemy.run/rivet";
import * as Registry from "@alchemy.run/rivet/Registry";
const base = yield* Rivet.SandboxTemplate("Agent", { software: [Registry.Git] });
const sandboxes = yield* Rivet.CreateSandbox(base);
// in fetch:
const sandbox = sandboxes.getOrCreate(taskId);
yield* sandbox.files.write("/home/agentos/hello.sh", "echo hello");
const result = yield* sandbox.exec(["sh", "/home/agentos/hello.sh"]);

exec runs to completion and returns { exitCode, stdout, stderr }; a non-zero exit is returned, not failed. A string runs in sh -c, an array runs directly.

const build = yield* sandbox.start("npm run build", { cwd: "/home/agentos/app" });
yield* build.output.pipe(
Stream.runForEach((chunk) => Effect.log(new TextDecoder().decode(chunk.data))),
);
const { exitCode } = yield* build.wait;

CreateSandbox: Reaching a server in the VM

Section titled “CreateSandbox: Reaching a server in the VM”
const port = yield* sandbox.getTcpPort(3000);
const response = yield* port.fetch(HttpClientRequest.get("/health"));