FIELD REPORT
How to sandbox your coding agent in 10 minutes (full setup)
·
An upgrade removed my agent’s fence. In October my coding agent read and wrote files outside its project folder, and not one prompt asked me first. I put the limit back. The shell walked straight around it.
That is the whole lesson, and it took me five incidents to believe it: a rule the agent is told is not a fence. A fence is something the operating system refuses.
Here is what happened, and the file that finally held.
1. The upgrade that removed the fence
On 17 October Homebrew moved OpenCode, the coding agent my builds run on, to version 2. Its default build agent now allowed everything, with no rule for paths outside the working folder.
I found out the plain way: a live run read and wrote files outside its folder, and no prompt appeared.
The fix looked easy. One setting per run denies “external directories”. I tested it: the agent’s write tool was refused outside the folder, and its read tool too.
Then I tried the shell. touch ../outside worked. So did cat.
The permission covered the agent’s file tools. The shell is a different door.
On 10 November it happened again from the other side. A backup model, building in its own folder, had its read tool refused on a note outside the folder. It read the note with the shell instead.
It wasn’t malicious. It was being helpful.
2. Isolation that lived in the prompt
Back in July I ran five agent workers in parallel. The prompt said each needed its own isolated folder, and to stop if there was none.
When I looked at the running processes, all five had the same working directory as their parent. The tool that started them had no option for a separate folder per worker. The isolation was a sentence in the prompt.
That setup is retired now, and the fix I planned for it was never built. But it was the first time I wrote down: a prompt rule is not isolation.
3. “Don’t print secrets” is not a rule a model can keep
On 27 October I asked an agent for a read-only audit of my Mac. The brief said: don’t print secrets.
It ran cat ~/.zshrc. That file held an API key in plain text. The key went to the outside model doing the audit, and into two local session databases.
The key was removed and revoked. The lesson was worse than the leak: the model can’t know a file holds a secret until it has printed it.
Ten days earlier, my main agent’s own redaction had failed the same way, three times in one day:
- cutting a line at 60 characters still printed most of a proxy password
- a filter on key names missed the token fields and printed 60 characters of a refresh token
- macOS
sedhas no\s, so a redaction pattern matched nothing and the secret went straight to the transcript
A sub-agent then repeated the same mistake in the same session.
The proxy password and the refresh token were rotated afterwards.
What works is parsing the file and printing only key names and value types:
jq 'map_values(type)' config.json
Never the values. Not even “redacted” ones.
4. Read-only, and it built 189 MB
On 31 October a “read-only” job needed one file from a library in a production project. It imported the library by absolute path.
There was no prebuilt version for the option it asked for, so the library did what it was built to do: it cloned and compiled its native code, 189 MB, into the production project’s dependencies folder.
The live service was fine. The job never wrote a line itself. It caused a write through someone else’s code.
“Read-only” describes intent. It doesn’t stop a call from building something.
5. The night the only fences were sentences
On 10 November my builder agents were working on a dashboard project. Three of them ran whole test folders that no task had asked for. Those tests wrote to live folders, and setting a temporary home folder didn’t move them.
Then a deliberate break, run to check that the tests catch a bug, made the unprotected tests write a settings file into a live folder.
A before-and-after snapshot found the stray file within two minutes. Nothing important changed. But every guard I had that evening was a sentence in a brief.
So the builders went inside a macOS sandbox, with their project folder as the only place they can write. I probed it before trusting it, and the test suites ran the same inside it as outside.
Later that night a builder read a project note outside its folder and tried to rewrite it. The sandbox refused the write.
What is still only a sentence
Being honest about my own setup:
- Agents can still read most files and reach the internet. They need both to work. So the brief still says what not to open, and every diff still gets checked before it lands.
- “Verify with real commands before claiming success” is a line in my instructions file. It is a request, not a fence. Review is what catches the misses.
The rules say what I expect. The sandbox decides what happens.
Copy this
A minimal macOS sandbox profile with one write root. Save it as agent-fence (a .sb file):
(version 1)
(allow default)
; Writes: only the project, the temp folders and the agent's own state.
(deny file-write*)
(allow file-write*
(subpath (param "PROJECT"))
(subpath "/private/tmp")
(subpath "/private/var/folders")
(literal "/dev/null")
(literal "/dev/tty")
; the agent's own state; these four are OpenCode's on my Mac
(subpath (string-append (param "HOME") "/.local/share/opencode"))
(subpath (string-append (param "HOME") "/.local/state/opencode"))
(subpath (string-append (param "HOME") "/.cache/opencode"))
(subpath (string-append (param "HOME") "/.config/opencode")))
; Reads: not the places where secrets usually live.
(deny file-read*
(subpath (string-append (param "HOME") "/.ssh"))
(subpath (string-append (param "HOME") "/.aws"))
(subpath (string-append (param "HOME") "/Library/Keychains"))
(regex #"/\.env$")
(regex #"/\.env\.[^/]*$"))
Run your agent inside it from the project folder:
sandbox-exec -f agent-fence.sb -D PROJECT="$PWD" -D HOME="$HOME" opencode run "your task"
I ran a real agent session inside this exact file on 12 November and asked it for three shell commands:
echo hi > hello.txtin the project: workedtouch ~/fence-probe: “Operation not permitted”cat ~/.ssh/config: “Operation not permitted”
The agent decided macOS privacy settings had blocked it. It was the fence.
Notes before you use it:
- Your agent writes its own state somewhere. If it fails to start, run it once and add the folders it was refused. Mine needed the four above.
sandbox-execis marked deprecated in its man page. It still ships and works on macOS 27. On Linux the same idea is a container or bubblewrap; I haven’t tested those.- Reads stay mostly open on purpose. Anything that holds a secret and isn’t in the deny list is still readable.
And three rules that go with it:
- After every agent CLI upgrade, run two probes before the next real task: one write outside the project, one read of a file it shouldn’t see. Both must fail.
- Never ask a model to “not print secrets”. Forbid printing whole shell rc, config,
.envand plist files. Allow key names only. - Give every test run its own fence. A break run can remove exactly the guard a test relied on.
Your turn
What’s one thing your agent can still reach that you’d rather it couldn’t?
One email a week with each new field report, plus Agent Checklists, my free 37 page starter AGENTS.md and habits guide.
Instant PDF download on the next page. Unsubscribe any time.