Use cases/RL environments and evals

How to give every RL run a fresh copy of its environment

A step-by-step guide to giving each rollout and eval its own clean copy of the environment, so a reward means what you think it means.

  • 12 min read
  • TypeScript
  • For RL and eval teams
RunScorefrom c76ff44
run-10.71
run-20.68
run-30.83
run-40.92
run-50.77

When the world gets easier

An environment is mostly files: a repository, some data, a config. Rollouts read them and write them. When rollouts share one copy, or reuse a container between tasks, whatever one run leaves behind becomes the starting point for the next. A bug fixed by an early rollout is already fixed for every rollout after it, and the scores go up for free. The policy has not got better. The world has got easier.

The obvious fixes cost a lot. You can copy the whole environment for every rollout, but that is slow, and the copies pile up when you run hundreds at once. You can rebuild a clean container each time, but then every reset waits on a rebuild. And neither one tells you, afterwards, what a strange rollout actually changed.

The usual setup

run 1Shared environment Left over from run 1run 2

What we’ll build

v3Saved once · never changesrollout-0001rollout-0002rollout-0003

In this guide we’ll do it the other way. We save the environment once, as a fixed version that nothing can change by accident. Each rollout gets its own copy of that version, which takes no time and copies no data. When the rollout ends, you look at exactly what it changed, and then delete it.

What we’re building

By the end, you’ll have:

  • Your task suite saved once, as a fixed starting point.
  • A fresh, private copy of it for every rollout, made in 10 ms.
  • Tools your policy calls to read and write files on its own copy.
  • A score for each rollout, and a list of exactly what it changed.
  • Resets that are one delete, with odd rollouts kept for a closer look.

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 world once

Our environment is a small suite of coding tasks. Each task has a short brief, some broken code, and a test the policy has to make pass:

task-suite/

  • package.json
  • tasks/date-parser/README.md# the brief
  • tasks/date-parser/parse.js# the bug lives here
  • tasks/date-parser/test.js# passes once it is fixed
  • tasks/slugify/README.md
  • tasks/slugify/slug.js
  • tasks/slugify/test.js

We’ll upload it to a disk, a set of files with branches and history. It lands on the disk’s first branch, main. A branch is a line of saved versions, and each saved version is a commit, with its own id.

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

const sky = new Skywake();

const suite = sky.disk("task-suite");
const start = await suite.upload("./task-suite", {
  message: "Task suite v3",
});
const v3 = await suite.fork("v3");

Two things just happened. start is the id of the commit we saved. And v3 is a new branch at that commit: this is the world every rollout will begin in. Nothing ever writes to v3, so it stays the same, even when main moves on.

Step 2

Give each rollout its own copy

Now the key idea. A fork is a new branch that starts where another one is. It copies nothing. It points at the same files until someone changes one, and only that change is saved, on the fork alone.

rollout.ts
import type { Disk } from "skywake";

async function newRollout(id: string): Promise<Disk> {
  return v3.fork(`rollout-${id}`);
}

const run = await newRollout("0001");

run holds every file of the suite, exactly as it is on v3. Whatever the policy does from here stays on rollout-0001.

Tip: fork from v3, not main

We fork v3 on purpose. If the suite changes next week, main moves, but v3 still has this week’s files. Store start next to each result too: fork(name, { at: start }) rebuilds the exact world a score came from.

Step 3

Let the policy work on its copy

Your policy acts on the world through tools. We’ll give it two: read a file and write a file. Both are bound to one rollout, so the policy cannot reach any other copy, or main.

tools.ts
function toolsFor(run: Disk) {
  return {
    readFile: (path: string) => run.read(path),
    writeFile: (path: string, text: string) =>
      run.write(path, text, { message: `policy: write ${path}` }),
  };
}

const tools = toolsFor(run);
await policy(tools);

Hand tools to your agent loop, here called policy. Each write becomes its own commit on the rollout, so you get a step-by-step record of what the policy did, for free.

Step 4

Score the rollout

Some rewards need a real folder on disk, like running a test. download writes the rollout’s files into a local folder. We run the task’s test there: it passes, reward 1; it fails, reward 0.

score.ts
import { execFileSync } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

async function score(run: Disk, task: string) {
  const dir = await mkdtemp(join(tmpdir(), "rollout-"));
  await run.download(dir);
  try {
    execFileSync("node", [`tasks/${task}/test.js`], {
      cwd: dir,
      stdio: "ignore",
    });
    return 1;
  } catch {
    return 0;
  } finally {
    await rm(dir, { recursive: true });
  }
}

const reward = await score(run, "date-parser");

The local folder is only a scratch copy for the test, and we remove it right after. The rollout itself still lives on its branch.

Step 5

Reset with one delete

Before you throw a rollout away, look at what it changed. diff lists every file the rollout added, removed or edited, compared with v3. That makes one kind of cheating easy to catch: a policy that “passes” by rewriting the test.

finish.ts
async function finish(id: string, run: Disk) {
  const changed = await run.diff("v3");
  const touchedTests = changed.some((c) => c.path.endsWith("test.js"));
  if (touchedTests) {
    console.log(`Look at rollout-${id}:`, changed);
  } else {
    await run.destroy();
  }
}

await finish("0001", run);

v3 never moves, so only the policy’s own changes show. An honest rollout is deleted, and that is the whole reset. A rollout that edited a test stays on its branch, ready for you to open:

Rollout · what it changedReward
rollout-0001modified tasks/date-parser/parse.jsDeleted
1
rollout-0002modified tasks/date-parser/test.jsKept for review
1

Both rollouts scored 1. Only the list of changed files tells them apart. The next rollout you start forks from v3 again, with the bug back in place.

Step 6

Run them side by side

Because each rollout is its own branch, you can run as many at once as your policy can serve. Here are eight, in parallel. policy is your own agent loop, called with the rollout’s tools.

many.ts
const ids = Array.from({ length: 8 }, (_, i) => `run-${i}`);

const rewards = await Promise.all(
  ids.map(async (id) => {
    const run = await newRollout(id);
    await policy(toolsFor(run));
    const reward = await score(run, "date-parser");
    await finish(id, run);
    return reward;
  }),
);

This is where a shared environment goes wrong, now fixed. We tested it with a policy that fixes the bug in only the first four rollouts it is given. Exactly four rewards came back as 1. No fix leaked from one rollout into another, and every honest rollout was gone when the batch finished.

Running it for real

Nothing in the code above changes as your suite and your runs grow. A few things to know along the way:

  • Copies stay fast at any size. Copying an environment takes about 95 ms, however big it is. Copying 20,000 files takes about 10 ms.
  • Copies stay small. A fork copies no data. Each rollout only adds the files it writes.
  • Old results stay reproducible. Keep the start commit next to every score. Any rollout can be replayed from the same world, months later.
  • It all stays yours. Environments and rollouts live in your own bucket.

Shipping a new version of the suite

Change main the careful way too: fork it, edit the fork, and merge it back. Then main moves to v4 in one step.

new-version.ts
const v4 = await suite.fork("suite-v4");
await v4.write(
  "tasks/slugify/README.md",
  "Also handle tabs and many spaces.",
  { message: "Task suite v4" },
);
await v4.mergeInto("main");
await v4.destroy();

New runs can fork from the new head of main. Runs that fork from v3 still get v3, file for file. So you can compare a checkpoint on v3 and v4 without keeping two copies of anything.

Questions

Start building with Skywake.

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