SDK
Fork

Fork

When you hibernate a sandbox, we create and save a snapshot of the underlying Firecracker VM.

If you call sdk.sandboxes.resume(id) or a network request is made to the Sandbox, we restore the snapshot. Allowing you to continue from exactly where you left off.

As we already have a snapshot, we can also use it to create a fork of your project in a new Sandbox. So you can create multiple instances without having to do all the set up work every time.

import { CodeSandbox } from '@codesandbox/sdk'
const sdk = new CodeSandbox();
 
const sandbox = await sdk.sandboxes.create();
const session = await sandbox.connect()
 
// Run anything on the sandbox
await session.shells.run('echo test > test.txt');
 
const sandbox2 = await sdk.sandboxes.fork(sandbox.id);
 
// Now we have two sandboxes that have the same fs & memory state!

You can use this to add support for checkpoint/restore functionality, or A/B test different agent iterations. At CodeSandbox we use this to enable users to quickly fork shared Sandboxes to their own account.

Manually Creating a Memory Snapshot

You can manually create a memory snapshot by calling sandbox.hibernate():

import { CodeSandbox } from '@codesandbox/sdk'
const sdk = new CodeSandbox();
 
const sandbox = await sdk.sandboxes.create();
 
// Do work
 
await sdk.sandboxes.hibernate(sandbox.id);

Creating a memory snapshot can take between 3-10 seconds. Resuming from a memory snapshot takes between 0.5-2 seconds.

Live snapshots

If a Sandbox is already running we can still fork its exact current state. This has a small overhead of about 0.5 seconds.

Learn More

We have written a couple blog posts about how memory snapshots work under the hood: