I let a local AI agent loose on my Mac's disk. Here's how I made sure it could only look
I built a small Python agent that uses a local model (Gemma 4 on Ollama) to explain what is filling my Mac's disk. The most useful thing I learned had nothing to do with how clever the model is. A small local model can do useful work on private files, as long as code decides what it can reach and t

I built a small Python agent that uses a local model (Gemma 4 on Ollama) to explain what is filling my Mac's disk. The most useful thing I learned had nothing to do with how clever the model is. A small local model can do useful work on private files, as long as code decides what it can reach and the OS blocks everything else.
I have spent about a decade as a sysadmin, and I am learning Python and agents by building small, useful tools, with Claude as a pair programmer. "What is eating my disk?" is a good task to automate: it is a real problem, the answer is checkable, and it touches private files. That last part forces the question every agent builder should ask early: how do I know it won't do something I didn't want?
The prompt asks. The code limits. The OS enforces.
Everything here runs locally. No API keys, no cloud, and no pip install: just Python's standard library and Ollama. That makes it easy to move to another machine, which is what I do in part 2.
Disk Detective is one Python file of about 280 lines. It gives Gemma 4 three tools, and none of them can change anything on disk.
My machine: an Apple Silicon Mac with 24 GB of RAM, macOS 15, Python 3.14, and gemma4 (5 GB) in Ollama.
python3 disk_detective.py # whole home folder python3 disk_detective.py ~/Desktop # one folder Enter fullscreen mode Exit fullscreen mode The model decides where to dig. A typical run on my home folder took 5 tool calls: overview, then ~, then ~/Library, then ~/Library/Caches, then ~/Downloads. It finished in about two minutes and wrote a Markdown report.
The whole "agent" is a loop of about 25 lines. The model never runs code. It only writes text that asks for a tool, and the loop decides whether to act on it.
for step in range ( 1, max_steps + 1 ): msg = chat ( model, messages, TOOL_SCHEMAS ) messages. append ( msg ) calls = msg. get ( "tool_calls" ) or [ ] if not calls: break # no tool call = "I'm done" for call in calls: name = call [ "function" ] [ "name" ] args = call [ "function" ]. get ( "arguments" ) or { } if name not in TOOLS: # allow-list result = f "Unknown tool {name}." else: try: result = TOOLS [ name ] ( ** args ) except Exception as e: # errors go back to the model result = f "ERROR: {e}" messages. append ( { "role": "tool", "tool_name": name, "content": result } ) else: log ( "Step budget used." ) Enter fullscreen mode Exit fullscreen mode The line that stopped me was the first one: why range(1, max_steps + 1)? I asked Claude. range excludes its end, so with max_steps = 10 this counts 1 to 10. It is a ceiling, not a target: Gemma stopped after 5 of 10 steps on its own.
And the else on the for loop only runs if the loop never hit break, which means the budget ran out.
The model was good at picking folders and less good at judging them. It labelled ~/.vscode as SAFE to clear, even though it holds my installed extensions. That is why none of the tools can delete anything.
My system prompt says "You can only LOOK; you cannot delete anything." That is a request, not a guarantee. So I checked at three levels, weakest to strongest.
The only place model output becomes an action is TOOLS[name](**args). Nothing passes model text to eval, exec or a shell. So "is the agent read-only?" becomes "are these three functions read-only?":
Every path the model supplies also goes through safe_path(). It calls.resolve() before checking that the path is inside my home folder, so ~/../../etc and symlinks pointing outside are refused. Check first and resolve second, and both get through.
There is exactly one write in the whole file: saving the report, to a filename I choose with --out. The model never decides it.
grep -nE "open\(|write|remove|unlink|rmtree|rename|mkdir|chmod|shell=True|eval\(|exec\(|subprocess|os\.system" disk_detective.py Enter fullscreen mode Exit fullscreen mode This should print exactly three hits: the subprocess import, the du call, and write_text for the report. A fourth hit means something changed. It takes a second to rerun after every edit.
Reading code proves what the code says. A sandbox makes it impossible to do anything else, whatever bug I add later. I have used Macs for years and had no idea this was built in. macOS ships sandbox-exec, which runs a program under a Seatbelt profile. Seatbelt is the same mechanism coding agents such as OpenAI's Codex CLI use to sandbox commands on macOS.
My whole profile, readonly.sb, is four lines:
( version 1 ) ( allow default ) ( deny file-write* ( subpath ( param "HOME" ) ) ) ( allow file-write* ( literal ( param "OUT" ) ) ) Enter fullscreen mode Exit fullscreen mode It blocks every write in my home folder except the one report file. Running the agent inside it:
OUT = "$PWD/report.md" sandbox-exec -D HOME = "$HOME" -D OUT = "$OUT" -f readonly.sb python3 disk_detective.py --out "$OUT" Enter fullscreen mode Exit fullscreen mode I tested it both ways. touch ~/Desktop/should-fail.txt inside the sandbox failed with "Operation not permitted", and the file was never created. The agent itself ran normally inside the sandbox and wrote its report. Ollama runs as its own process outside the sandbox, so the model is unaffected.
Apple marks sandbox-exec as deprecated, but it still works on current macOS (I have since tested it on macOS 26 too). On Linux, the equivalent is a read-only bind mount or bwrap.
Once Disk Detective worked, I stripped it down to a reusable template. In part 2, I take that template to a second Mac with a different model and point it at the internet: an agent that reads IT news feeds. Reading text written by strangers is a different threat, and the sandbox has to do a different job.
If you build agents, try the grep check and the four-line sandbox profile on your own tools. Seeing "Operation not permitted" on a write you know your code would never attempt is reassuring in a way no system prompt is. And if you've locked down an agent a different way on macOS or Linux, I'd like to see your profile in the comments.
Built on a Mac with Ollama and Gemma 4; I worked through it with Claude as a pair programmer.