Use cases/Agent storage and sandboxes

How to keep an agent’s work when its sandbox crashes

A step-by-step guide to saving an agent’s work as it goes, so when a sandbox crashes, a new one picks the job up right where it stopped.

  • 12 min read
  • TypeScript
  • Beginner friendly
sb-1842shop-app
Migrated tests/t30.test.jsmachine-a
Migrated tests/t31.test.jsmachine-a
machine-a stopped
Migrated tests/t32.test.jsmachine-b

Why sandboxes lose work

Sandboxes are cheap, short-lived machines you can throw away. That is great for safety and bad for long jobs. Anything an agent writes lives on the sandbox’s own disk, and when the machine is taken back, the disk goes with it. The job starts again from the beginning. The work was done. It was just kept on a disk that no longer exists.

The usual fixes each leave a gap. Copy the files to a bucket when the job ends, and a crash before the end still loses everything. Upload them every few minutes, and you lose the last few minutes. Worse, the note that says how far the job got can run ahead of the files that were really saved, so the next machine skips work or does it twice.

The usual setup

AgentSandbox disk Machine diesStart again at file 1

What we’ll build

AgentSandbox branchSaved every stepAny machine, from file 32

In this guide we’ll give the sandbox a working folder that is saved in your own bucket, one small save after every step. Each save holds the changed file and a note of how far the job got, together. When a machine dies, a new one opens the same folder and goes on from the last save.

What we’re building

By the end, you’ll have:

  • A base workspace, saved once, that every sandbox starts from.
  • A branch per sandbox, so each job works on its own copy.
  • A save after every step, with the file and the progress note together.
  • A log of every step, so you can see where a job stopped.
  • A crashed job that carries on from its last save on a new machine.

You’ll need a Skywake API key and Node. Set SKYWAKE_API_KEY, then install the SDK with npm i skywake.

Step 1

Save the base workspace once

Every sandbox in our story starts from the same project: the shop app, with its 40 test files still written for the old runner.

workspace/

  • package.json
  • src/cart.js
  • tests/t01.test.js# the first of 40
  • tests/…
  • tests/t40.test.js

We’ll save it once on a disk: a set of files with branches and history, like a Git repository for files. Every disk starts with a branch called main. Each save is a commit.

save-base.ts
import { Skywake } from "skywake";

const sky = new Skywake();
const app = sky.disk("shop-app");
await app.upload("./workspace", { message: "Base workspace" });

The disk shop-app holds this project. The first write creates it. You do this step once, not once per sandbox.

Step 2

Give the sandbox its own branch

When a sandbox starts, it gets its own branch, named after it. We make it with a fork: a new branch that starts exactly where main is. A fork copies nothing, so it is ready at once, however big the workspace.

new-sandbox.ts
await app.fork("sb-1842");

const sandbox = sky.disk("shop-app", { branch: "sb-1842" });
await sandbox.download("./sandbox");

The fork is made once, by whatever starts your sandboxes. The sandbox itself opens the branch by its name, and download writes the branch’s files into a normal folder on the machine. The agent works there with its usual tools. Other sandboxes get their own forks, so they never touch each other’s files.

Step 3

Save after every step

Now the job itself. The agent goes through the test files in order. After each one, we save two files in one commit: the test it just changed, and progress.json, a tiny note that says how many files are done.

work.ts
import { readFile, readdir } from "node:fs/promises";

const saved = await readFile("./sandbox/progress.json", "utf8")
  .catch(() => `{ "done": 0 }`);
const progress = JSON.parse(saved);
const tests = (await readdir("./sandbox/tests")).sort();

for (const name of tests.slice(progress.done)) {
  const path = `tests/${name}`;
  await migrate(`./sandbox/${path}`);
  progress.done += 1;
  await sandbox.write(
    {
      [path]: await readFile(`./sandbox/${path}`, "utf8"),
      "progress.json": JSON.stringify(progress),
    },
    { message: `Migrated ${path}` },
  );
}

Here migrate stands for your agent: it rewrites one file in place. The loop starts from progress.done, so on a fresh sandbox it starts at the first file, and after a crash it starts after the last saved one.

The important part is that the test and the note go in the same commit. A commit is saved whole or not at all. So the note can never say 31 while only 29 files are saved.

Tip: make each step safe to run twice

If the machine dies while the agent is halfway through a file, that file was not saved yet. The new machine will do it again. Write steps that give the same result when they run twice, and this costs you one step, not a broken file.

Step 4

Pick the job up on a new machine

Now say the machine is taken away after file 31. Everything it saved is still on the branch.

A new sandbox starts on another machine. Make the client as in step 1. Before it starts, it can read the branch’s last commits to see where the job stopped:

resume.ts
for await (const commit of sandbox.log({ limit: 3 })) {
  console.log(commit.message);
}

The newest commit is Migrated tests/t31.test.js. Now open the branch and download it as in step 2, then run step 3 again, unchanged. download brings back every saved file, progress.json says 31, and the loop goes on at file 32.

One job, two machines

Files 1 to 31 · saved by machine A before it died

Files 32 to 40 · machine B, from the last save

Tip: one machine per branch

Start the new sandbox only once the old one has stopped. Your sandbox provider tells you when. Two machines on one branch would do the same files twice.

Step 5

Keep the result, drop the sandbox

When the last file is done, the job is finished. The progress note has done its work, so we delete it. Then we look at what changed, and merge the sandbox into main, which brings its changes into that branch.

finish.ts
await sandbox.delete("progress.json", { message: "Job done" });
const changed = await sandbox.diff("main");
console.log(`${changed.length} files changed`);

await sandbox.mergeInto("main");
await sandbox.destroy();

The diff lists the 40 tests and nothing else. After the merge, the next sandbox that forks main starts with the new tests. destroy deletes the sandbox’s branch, and the machine can be thrown away with nothing left on it to lose.

If main changed the same lines in the meantime, mergeInto throws a MergeConflict and saves nothing. The branch then stays, for you to look at. If the job’s output is not meant for main, skip the merge. Read what you need from the branch, then destroy it.

Running many sandboxes

The same steps work for one sandbox or a thousand. A few things to know as you grow:

  • One base, many forks. Each sandbox forks the same main. A fork copies nothing: about 95 ms for a workspace of any size.
  • Saved means saved. Killing a machine with kill -9 loses no saved writes. Only the step in progress runs again.
  • See how far a job got. sandbox.read("progress.json") reads the note without downloading the branch.
  • Clean up as you go. Destroy each sandbox’s branch when its job is done. main stays.
  • It all stays yours. Every save lands in your own bucket, not on a machine you rent for an hour.

Questions

Start building with Skywake.

One shared disk for all your agents, with search built in.