feat(api): add POST /snapshots/{snapshotID}/fork — fork N sandboxes from a named snapshot - #3441
Open
AdaAibaby wants to merge 3 commits into
Open
feat(api): add POST /snapshots/{snapshotID}/fork — fork N sandboxes from a named snapshot#3441AdaAibaby wants to merge 3 commits into
AdaAibaby wants to merge 3 commits into
Conversation
Implements Proposal B from issue e2b-dev#3424: fork N sandboxes from a named snapshot template without requiring the original sandbox to still be running. Why RL/agent training workloads need to checkpoint once then run many evaluation batches from that checkpoint: 1. Create sandbox, load model weights / install deps (expensive) 2. Snapshot the sandbox to a named checkpoint (e.g. "team/rl-v1") 3. Kill original sandbox -- no longer needed 4. Per training batch: fork N episode sandboxes, run rollouts, kill 5. Repeat step 4 indefinitely from the same checkpoint Before this change step 4 was impossible after step 3. The only alternatives were fork-from-running-sandbox (keeps the source alive, wastes resources) or N fresh sandboxes (no shared checkpoint, slow). What New endpoint: POST /snapshots/{snapshotID}/fork Request body (same schema as POST /sandboxes/{id}/fork): count int -- number of forks (default 1, max 100) timeout int -- TTL for each fork in seconds Returns 201 with a list of SandboxForkResult entries, one per requested fork. Each entry carries either {sandbox: ...} (success) or {error: ...} (failure), so a partial batch is still useful. Implementation note: templateCache vs snapshotCache Ephemeral pause snapshots (source=snapshot) are soft-deleted when the sandbox is killed -- their deleted_at is set and active_envs returns nothing, so snapshotCache.Get returns 404 for killed sandboxes. Named snapshot templates (source=snapshot_template) are persistent: they survive sandbox deletion and are never touched by the kill path. This handler resolves the alias via templateCache.ResolveAlias, then fetches build data from env_build_assignments for the persistent template env -- the same path POST /sandboxes uses to start from a template.
6 tasks
Unlike /sandboxes/{id}/fork where the source sandbox occupies a slot,
/snapshots/{id}/fork has no running source sandbox. Using >= rejected
count == limit even when all slots were free. Changed to > so users can
fork up to their full concurrency limit in one request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RL/agent training workloads need to run many evaluation batches from a single checkpoint, but there was no way to do this efficiently.
The typical pattern (from issue #3424):
Before this PR, step 4 failed after step 3. The only alternatives were:
POST /sandboxes/{id}/fork) — requires source to still be runningSolution
New endpoint:
POST /snapshots/{snapshotID}/forkResponse (201) — one entry per fork, each independently succeeded or failed:
[ { "sandbox": { "sandboxID": "iabc...", "templateID": "xyz...", ... } }, { "sandbox": { "sandboxID": "idef...", "templateID": "xyz...", ... } }, ... ]Partial batches are still useful — a single failed fork doesn't abort the others.
Key implementation detail: why
templateCachenotsnapshotCacheEphemeral pause snapshots (
source = 'snapshot'in theenvstable) are soft-deleted when the sandbox is killed —deleted_atis set,active_envsJOIN returns nothing, andsnapshotCache.Getreturns 404.Named snapshot templates (
source = 'snapshot_template') are persistent — they survive sandbox deletion and are never touched by the kill path. This handler resolves the alias viatemplateCache.ResolveAliasthen fetches build data fromenv_build_assignmentsfor the persistent template env. This is exactly the same code pathPOST /sandboxesuses to boot from a template, so cluster routing, envd-version checks, and placement logic are all inherited automatically.Verified end-to-end on dev
Full RL training simulation: create sandbox → snapshot → kill original → fork 3 batches (3 + 3 + 5 forks) from the same checkpoint:
Fixes #3424 (Proposal B).
/cc @jakubno @dobrac @ValentaTomas @arkamar @tvi Looking forward to your code review.