Skip to content

Fork

What is a Fork?

sandbox.fork() creates a new independent sandbox from the current sandbox’s filesystem state using copy-on-write. The original sandbox keeps running. The two sandboxes share unmodified disk blocks — a block is only copied when one side writes to it.

const branch = await sandbox.fork()
await branch.exec.run('python3 risky_operation.py')
// Discard when done — original is untouched
await branch.kill()

Parallel Experiments

Fork multiple times from the same decision point to explore approaches simultaneously:

await sandbox.exec.run('python3 setup.py')
const [expA, expB, expC] = await Promise.all([
sandbox.fork(),
sandbox.fork(),
sandbox.fork(),
])
const results = await Promise.all([
expA.exec.run('python3 train.py --lr=0.001'),
expB.exec.run('python3 train.py --lr=0.01'),
expC.exec.run('python3 train.py --lr=0.1'),
])
await Promise.all([expA.kill(), expB.kill(), expC.kill()])

AI Agent Rollback

Fork before a risky operation — if it fails, the original sandbox is unaffected:

const branch = await sandbox.fork()
const result = await branch.exec.run(aiGeneratedCode)
if (result.ok) {
await branch.kill()
await sandbox.exec.run(aiGeneratedCode) // replay on original
} else {
// Failure — discard the branch, original is clean
await branch.kill()
}

Test Isolation

Keep one seeded base sandbox alive. Each test forks from it:

const base = await Sandbox.create()
await base.exec.run('npm install && npm run db:seed')
for (const test of tests) {
const env = await base.fork()
const result = await env.exec.run(`npm test -- ${test}`)
expect(result.exit_code).toBe(0)
await env.kill()
}
await base.kill()

Each fork gets an identical filesystem. Tests cannot affect each other regardless of what they write to disk.

Debugging

Keep the sandbox alive at the point of interest and fork from it repeatedly:

// Sandbox is at the buggy state
for (let i = 0; i < 5; i++) {
const investigation = await sandbox.fork()
const logs = await investigation.exec.run('strace ./program 2>&1 | head -100')
console.log(logs.stdout)
await investigation.kill()
}