Sub topic 9
Disposable sandboxes and reviewing changes
A hardened container limits what an agent can do while it runs. This chapter covers what happens before and after the run. You start every session from a clean, throwaway copy of the project. You bring back only the changes you have read. The chapter assumes you know the hardening flags from the earlier chapters (--read-only, --cap-drop=ALL, --network none) and that you are comfortable with git diff, git apply, and bind mounts.
Why disposable matters
An agent that works in a long-lived environment leaves state behind: installed packages, edited dotfiles, cached credentials, modified build scripts. Each leftover is a place where one bad session can affect the next one. A disposable sandbox removes that carry-over by design.
Two things can be disposable, and they are separate decisions:
- The container.
docker run --rmdeletes the container and its writable layer when it exits. Anonymous volumes and--tmpfscontents go with it. Named volumes and bind mounts do not. - The workspace. The files the agent edits. If you bind-mount your real project, the agent edits your real files, and
--rmprotects none of them.
A disposable container with a persistent, writable view of your real checkout gives you much less than it appears to. The fix is to give the agent a copy of the project, not the project.
| Property | Mounting the real project | Mounting a throwaway copy |
|---|---|---|
| Agent edits | Land in your files immediately | Land in a copy you can delete |
| Undo | Depends on git state and your memory | Delete the copy |
| Review | Easy to skip, because the work is already done | Required before anything is applied |
| Hooks and config in `.git` | Agent can change them | Can be mounted read-only |
The lifecycle
The workflow has five steps. Review is the only step that needs your attention.
A complete script
The script below clones the project, runs a shell in the locked-down container, then builds a patch. It makes three choices you should understand before you reuse it.
- It uses
git clone --no-hardlinks. A plain local clone hardlinks the object files with your original repository. Hardlinked files are shared inodes, so a write through one path would affect the other. A copy removes that link. - It mounts the clone's
.gitdirectory a second time, read-only, on top of the writable workspace. The agent can read history and rungit diff. It cannot add hooks or edit.git/config. - It builds the patch on the host with a separate index file, so it never needs the agent's cooperation and never writes to the read-only
.git.
#!/usr/bin/env bash
set -euo pipefail
PROJECT="$(git rev-parse --show-toplevel)"
WORK="$(mktemp -d)"
PATCH="$(mktemp --suffix=.patch)"
trap 'rm -rf "$WORK"' EXIT
git clone --quiet --no-hardlinks "$PROJECT" "$WORK/repo"
docker run --rm -it \
--user "$(id -u):$(id -g)" \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--pids-limit 256 \
--memory 2g \
--cpus 2 \
--network none \
-v "$WORK/repo":/workspace \
-v "$WORK/repo/.git":/workspace/.git:ro \
-w /workspace \
python:3.12-slim bash || true
# Build the patch on the host. A separate index keeps the clone's .git untouched.
export GIT_DIR="$WORK/repo/.git"
export GIT_WORK_TREE="$WORK/repo"
export GIT_INDEX_FILE="$WORK/index"
git read-tree HEAD
git add -A
git diff --cached --stat --summary
git diff --cached --binary > "$PATCH"
echo "Patch written to $PATCH"
echo "Review it, then run: git -C \"$PROJECT\" apply --check \"$PATCH\" && git -C \"$PROJECT\" apply \"$PATCH\""The || true after the container command stops set -e from aborting the script when the last command inside the shell returned a non-zero status. You still want the patch in that case. The script uses --user "$(id -u):$(id -g)" instead of a fixed 1000:1000 so the agent can write to a temp directory owned by whoever runs the script.
What the script does not capture
- Uncommitted host changes.
git clonecopies commits only. If you have work in progress, commit or stash it first, or the agent starts from a different state than you expect. - Ignored files.
git add -Ahonors.gitignore. A.envfile or a build output the agent created will not appear in the patch. That is usually what you want. It also means the patch is not a complete record of what the agent wrote. - Files outside the workspace.
/tmpis a tmpfs and disappears with the container. If the agent needs to keep something, it has to write into/workspace.
Why the review step is the security boundary
The container limits what the agent can do during the run. The patch is the only thing that crosses the boundary back into your environment. Whatever you apply runs later with your privileges: when you build, test, commit, or push. An agent does not need to escape the container if it can write a line into your test script.
Apply the exact file you reviewed. Do not re-generate the patch after review, and do not copy files out of the clone by hand. A second generation could differ from the first, and a manual copy skips every check git apply does.
What to look for
Read the --stat --summary output first, then the diff. The summary lists mode changes, renames, and created or deleted files, which are easy to miss in a long diff.
| Signal | Why it matters |
|---|---|
| Changes to CI files (`.github/workflows/`, `.gitlab-ci.yml`) | Code that runs later with repository secrets |
| Changes to build or install hooks (`package.json` scripts, `setup.py`, `Makefile`, `pre-commit` config) | Runs on your machine the next time you build or install |
| New or changed dependencies and lockfiles | Supply-chain risk, including look-alike package names |
| `mode change 100644 => 100755` | A file became executable |
| New symlinks (`mode 120000`) | Can point outside the project |
| Large binary diffs | You cannot read them, so treat them as untrusted |
| Edits to tests that make them pass | The agent may have weakened the check instead of fixing the code |
| Strings that look like URLs, tokens, or encoded blobs | Possible exfiltration or a hidden payload |
Apply safely
Run git apply --check first. It verifies that the patch applies cleanly without touching the tree. git apply also rejects paths that escape the repository, which blocks a class of path-traversal tricks.
git -C "$PROJECT" apply --check "$PATCH"
git -C "$PROJECT" apply "$PATCH"
git -C "$PROJECT" status --shortApply onto a new branch, not your main working branch, and let your normal CI and code review run on it. The sandbox reduces risk. It does not replace the review you would give a stranger's pull request.
Edge cases
- Running git on an untrusted directory. Git reads
.git/configand may run programs it names, such ascore.fsmonitor. This is why the script keeps.gitread-only inside the container. If you use a design where the agent can write.git, do not run git on that directory from the host. Extract the working tree and diff it against a trusted copy instead. - Dependencies with
--network none. The agent cannot install packages. Build an image that already contains the dependencies, or run a separate, network-enabled setup step before the agent starts. Do not enable the network for the whole session just to runpip install. - Resuming a session. Keep the clone directory and start a new container on it. The container is disposable. The workspace persists until you decide to discard it.
- Long sessions. The longer an agent works on one copy, the larger and harder to review the final patch is. Prefer several small sessions with a review after each.
- Cleanup on failure. The
trapremoves the clone when the script exits for any reason. The patch is created outside the clone withmktemp, so it survives.
Exercise
The script mounts $WORK/repo writable at /workspace and then mounts $WORK/repo/.git read-only at /workspace/.git. Name two things this prevents, and one thing the agent can still do to the clone that you must catch in review.
Show answer
It prevents the agent from adding Git hooks and from editing .git/config (for example to set core.fsmonitor), which would run code on the host the next time you run git there. It also prevents the agent from rewriting history or committing. The agent can still modify any file in the working tree, including CI configs, build scripts, and dependency manifests, so those changes must be caught when you read the patch.
Exercise
A teammate says that using docker run --rm with -v "$PWD":/workspace is already a disposable sandbox. What is wrong with that claim, and what is the smallest change that fixes it?
Show answer
--rm only deletes the container. The bind mount points at the real project, so every edit the agent makes persists in your files and there is no review step. The smallest fix is to clone the project to a temporary directory (git clone --no-hardlinks), mount the clone instead of $PWD, and apply only a reviewed patch back to the real project.