# LAB 3: Snapshot it, nuke it

Goal: keep a clean snapshot of your sandbox, let the agent trash the box, restore in
seconds, and make teardown the default. Time: about 25 minutes.

This is the Docker path. If you are on e2b, the same idea exists as sandbox templates and
short-lived sandboxes. Do steps 1 to 6, then read step 7 for the e2b version.

## Steps

1. Watch the whole cycle scripted first:
   ```
   python examples/04_snapshot_restore.py
   ```
   It builds a box, commits a clean snapshot, trashes the box, and restores from the
   snapshot. Read the output top to bottom.
2. Now do it by hand. Start a box you will keep for a few minutes:
   ```
   docker run -d --name lab3box python:3.11-slim sleep 600
   ```
3. Snapshot the clean state:
   ```
   docker commit lab3box lab3-clean
   ```
4. Trash the box on purpose. Pretend the agent did this:
   ```
   docker exec lab3box sh -c "rm -rf /usr/local/lib/python3.11 && echo trashed"
   docker exec lab3box python -c "print('alive')"
   ```
   The second command fails. Python is gone. The box is ruined.
5. Nuke and restore from the snapshot:
   ```
   docker rm -f lab3box
   docker run -d --name lab3box lab3-clean sleep 600
   docker exec lab3box python -c "print('alive')"
   ```
   Python is back. Restore took seconds.
6. Make teardown the default. From now on, one task means one box:
   ```
   docker rm -f lab3box
   docker run --rm --network none --memory 256m lab3-clean python -c "print('task done')"
   ```
   With `--rm` there is nothing to clean up. The box died with the task.
7. e2b version of the same idea: create a sandbox per task from a template, let it die at
   the end of the task. Long-lived sandboxes are the exception, not the rule.

**CHECKPOINT:** you know it worked when step 4 shows the box broken, step 5 prints `alive`
again from the restored snapshot, and step 6 leaves nothing in `docker ps -a`.
