Skip to content

Build your own

An AI.Sandboxes Layer is three functions over a key. @alchemy.run/ai ships one for five platforms. Write your own for a platform it doesn’t cover, or when a sandbox should be created, found or stopped another way: a bigger machine, a volume attached, a pool of warm sandboxes.

The harness chooses a key for each sandbox it wants and calls one of three operations with it:

  • open(key) returns the key’s sandbox, running. It creates the sandbox, resumes it, or returns it as it is.
  • suspend(key) stops the key’s sandbox and keeps its files, where the platform can. Where it can’t, it does nothing.
  • remove(key) deletes the key’s sandbox, and does nothing when there is none.

Each must be safe to repeat, because a harness calls them again after a crash or a retried request.

This is the Layer @alchemy.run/ai/Boat ships, written out:

src/Sandbox.ts
import * as AI from "@alchemy.run/ai";
import * as Boat from "@alchemy.run/boat";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
export const Sandbox = Layer.effect(
AI.Sandboxes,
Effect.gen(function* () {
const base = yield* Boat.Snapshot("Base", { run: ["git --version"] });
const sandboxes = yield* Boat.CreateSandbox(base);
const find = (key: string) => sandboxes.list({ labels: { key } });
return {
open: Effect.fn(function* (key) {
const [existing] = yield* find(key);
if (existing === undefined) {
return yield* sandboxes.create({
type: "small",
noEnv: true,
autoStop: "10 minutes",
labels: { key },
});
}
const sandbox = yield* sandboxes.connect(existing.id);
yield* sandbox.resume({ autoStop: "10 minutes" });
return sandbox;
}),
suspend: Effect.fn(function* (key) {
const [existing] = yield* find(key);
if (existing === undefined) return;
const sandbox = yield* sandboxes.connect(existing.id);
yield* sandbox.stop();
}),
remove: Effect.fn(function* (key) {
const found = yield* find(key);
yield* Effect.forEach(found, (sandbox) => sandboxes.delete(sandbox.id));
}),
};
}),
).pipe(Layer.provide(Boat.CreateSandboxHttp));

The outer Effect runs where the harness is yielded, so the snapshot and the binding that creates sandboxes are part of the deploy. The three functions run while the Durable Object does.

The key has to be stored on the platform, not in the Durable Object, so an object that was evicted finds its sandbox again. Use whatever the platform can look up: a label on Boat, metadata on E2B, the sandbox’s name on Daytona, Modal and Archil. A key is lowercase letters, digits and -, at most 48 characters, so it fits all of them.

open returns the platform’s own sandbox handle. A harness needs two things from it: exec, which runs a command to completion, and start, which starts a command and gives its output as a Stream. The handle of every Alchemy sandbox provider has both.

For another platform, return an object with those two functions:

open: Effect.fn(function* (key) {
const machine = yield* machines.ensure(key);
return {
exec: (command, options) => machine.run(command, options),
start: (command, options) => machine.spawn(command, options),
};
}),

exec resolves to { exitCode, stdout, stderr }. start resolves to { output, kill }, where output is a Stream of the command’s output.

Most reasons to write a Layer are in create. This one keeps a sandbox running for an hour after its last use, and labels it with the team that owns it:

if (existing === undefined) {
return yield* sandboxes.create({
type: "small",
noEnv: true,
autoStop: "10 minutes",
labels: { key },
autoStop: "1 hour",
labels: { key, team: "platform" },
});
}

A Layer of your own is provided where a shipped one would be:

const HarnessLive = Layer.mergeAll(
FastLive,
PiCloudflare.DurableObjectStorage,
Pi.CodingTools.pipe(Layer.provideMerge(Sandbox)),
);