# Sandboxing a coding agent's shell on a laptop, and its 10.4 ms cost

URL: https://codepanel.ai/blog/sandboxing-local-coding-agents

How CodePanel confines local coding agent commands with macOS sandbox-exec and Linux bwrap over git worktrees, the git gotchas we hit, and measured latency.

Confining a coding agent's file-editing tools to a git worktree is not a security boundary if the agent can also run shell commands as your OS user. Without an OS sandbox, a single shell command can read `~/.ssh`, `~/.codepanel` (including the runner's API token), or sibling worktrees, or edit `package.json` and run an allowed test script. CodePanel now wraps every local command, check, stdio MCP server, and external CLI in `sandbox-exec` (macOS) or `bwrap` (Linux), adding 10.4 ms median overhead per command.

## The hard part: git worktrees share one `.git` directory

A fresh git worktree looks isolated on disk, but its `.git` file is a pointer back into the main checkout's `.git` directory. If you deny all writes outside the worktree, `git commit` breaks; if you allow writes to the whole shared `.git` directory, an agent can rewrite `refs/heads/main` or modify another run's branch.

The sandbox profile solves this by granting narrow write access to only three paths inside the shared `.git` directory:

- `worktrees/<run>/` (the run's own worktree metadata and index)
- `objects/` (writing new git blobs, trees, and commits)
- The run's own branch ref and reflog (blocking `git update-ref refs/heads/main` and sibling branches)

Two macOS surprises broke naive profiles during testing:

1. `/usr/bin/git` writes an `xcrun_db` cache inside the per-user temporary directory regardless of `TMPDIR`.
2. Git 2.50 acquires `packed-refs.lock` even on a plain commit.

Both printed errors under a naive profile until explicitly accounted for.

## What the escape tests verify

Every change to the local runner runs an automated escape suite (`control/local/sandboxEscape.test.ts`) where each action must succeed unsandboxed (control) and fail inside the sandbox:

- Reading the runner's ticket key or `~/.codepanel`
- Reading `~/.ssh` or cloud credentials
- Reading another run's worktree
- Writing any file outside the run's worktree
- Running `git update-ref refs/heads/main`
- Calling `curl` when network access is denied (the default for [Autopilot](/agent-autopilot) rule-started runs)

The runner also runs a runtime sandbox proof at startup (23 to 32 ms) and fails closed if the sandbox cannot be enforced, unless the user explicitly opts out in `config.json`.

## Measured overhead on macOS

Measured with `bun run perf:runner` on an Apple M1 Pro (n = 100 interleaved runs of `true` through the runner):

| Measurement (Apple M1 Pro) | p50 | p95 | Notes |
| --- | --- | --- | --- |
| Plain command (2026-11-20, load 7) | 6.2 ms | 9.6 ms | Baseline without sandbox. |
| Sandboxed command (2026-11-20, load 7) | 16.6 ms | 19.8 ms | Sandbox adds 10.4 ms p50 / 12.1 ms p95 (budget 50 ms). |
| Startup runtime proof | 23 to 32 ms | - | Runs once when the runner starts. |
| Strict-mode `git status` hold check (n = 30) | 32 ms | 38 ms | Detects edits to `package.json`, `Makefile`, or `scripts/` made by shell commands. |

Ten milliseconds per command is negligible next to model latency and test suites, and it closes the largest local-execution risk for teams running coding agents on developer laptops. Read the full [security model](/security) or install the [desktop app](/download).
