Rules a coding agent cannot talk its way round
The hook that reads every command a coding agent wants to run on my Linux boxes: what it refuses, the 27 proofs that its tests fail when a rule is taken out, and the four lines every report opens with.
My coding agents run with the permission prompts switched off. They work unattended, and a prompt stops all of it until I’m back at the keyboard.
That leaves written rules. I had three that mattered: never delete on the home folder or /, never copy one of my local databases over another, and publish nothing. The database one exists because a session spent over an hour piping a dump of one database into another, when those databases are disposable and a script builds a fresh one. All three now have code behind them as well as text.
The hook
Every shell command an agent wants to run goes to a hook first. The hook gets the command and the working directory as JSON, and answers allow, or deny with a reason the agent reads. It’s Python. A command with none of rm, mv, -delete, rsync, clean, dump or sql in it skips the safety checks altogether.
This is a refusal:
$ rm -rf "$BUILD_DIR/"
Refused: `rm -rf '$BUILD_DIR/'` could delete or overwrite / when a variable in
it is empty. Never delete, move over or sync --delete onto $HOME, ~, /home/<me>
or /, or a target that is only a variable. The allowed way: make scratch space
with `mktemp -d` (under /tmp or the session scratchpad), use its literal path,
and delete only that literal path, or a literal path inside the repo. Do not
change settings or hooks to get past this check.
It reads ~, $HOME and ${HOME} as the home folder. /, /home and my home folder are always protected. It refuses a recursive delete whose target is only a variable, and find -delete or find -exec rm on a protected folder. rm -rf /tmp/tmp.Ab12Cd34/out goes through.
So does rm -r on a folder inside my home. The hook protects the home folder itself and leaves what’s in it alone, and anything it can’t work out (a cd into a variable, an unknown working directory) it allows, because a false refusal stops unattended work the same way a prompt did. An agent deleted a cache folder in my home that way. It was 55 MB and the program that owned it rebuilt it. The written rule covers that case and the code doesn’t.
The database check looks for a dump piped into a load, mysqldump, mariadb-dump or mydumper into mysql, mariadb or myloader, on the databases the rule is about. Its refusal doesn’t repeat the command, because a database command line can carry a password.
Publishing isn’t in the hook. The two tools that can put a page on a public link are on a deny list in the agent’s settings, so they can’t be called at all.
If the hook crashes, the command runs and a warning is shown. A guard that fails closed would stop every session on both boxes at once.
Two more rules in the same place
The hook’s first job was a different one. On the box I carry it refuses test suites, installs and builds, and prints the command that runs the same thing on the box at home.
A second hook sits on delegation, where one agent hands a task to another. It refuses a task given to the most expensive model, and one given to the next model down unless the brief carries a line with the reason. Before switching it on I replayed it over the previous seven days: 263 delegated tasks, 97 of them on that second model, none with a reason line. It would have refused 37 per cent of them, so it started in warn mode.
Tests that have to fail
A guard’s tests passing tells me little unless they fail when the guard is gone. So there’s a file of 27 proofs, and this is one:
{
"id": "safety-bare-var",
"rule": "a recursive delete whose target is only a variable is refused",
"old": " return \"anything (the target is only a variable, which may be empty)\" if bare else None",
"new": " return None",
"test": "tests/test_safety_guard.py::test_destructive_deny"
}
A runner copies the repo into a fresh scratch folder for each proof, swaps that one line and runs the named test, which has to fail. It runs every named test on an unchanged copy first, so a failure means the change and not a broken test. Nine proofs cover deletes and database copies, seven the heavy-work rule and eleven the delegation rule. All 27 fail when they should.
The runner was the risky part to write, since it edits guard code and runs things. Its second review found that a test name starting with - reached pytest as an option and deleted a file outside the scratch folder through --basetemp, and that the run inherited my tokens and my ssh agent. Both are fixed, with tests. The test run now gets an environment built from an allowlist, with HOME inside the scratch folder.
Reviewers have a rule of their own from an earlier job. A reviewing agent, probing a guard, tried its commands in the real repo with the real login and pushed three commits. A reviewer’s probes now run in a scratch clone with no credentials.
Four lines on every report
Work an agent hands back opens with four fixed lines. This is a real one, trimmed, from an agent reviewing a shell script of mine that removes folders on another machine:
STATUS: done
MODEL: <the model it ran on>
HEAD: worked from 86d3105 (uncommitted changes); left unchanged, no edits or commits.
NOT VERIFIED: the real script against real `sudo` or `ssh`; real `sudo -A` stdin
behaviour; the flush on a real terminal (I only confirmed `tcflush` fails with
exit 1 off a terminal); how sshd treats the remote `bash -s` when the link drops.
MODEL is there because naming a model in a brief isn’t proof the agent ran on it. HEAD is the commit it started from and the one it left. NOT VERIFIED may say nothing only when that’s true. That last line was the list of what I still had to try myself.
The idea for the header and for the proofs came from someone else’s public repos. The suite around the hooks had 2,294 tests when the proofs went in.
Featured projects
-
Apple News API
Publish articles and live coverage to Apple News from PHP, with the whole Apple News Format built from objects.
-
GitHub Auditor
Health, access and activity reports for more GitHub and Bitbucket repositories than you can keep in your head.
-
PrintMD
Markdown in, and a designed PDF, Word or PowerPoint file out. Its PDF service runs on your own computer.
-
Where Should I Live
Which city would leave you best off? Cities ranked by take-home pay, living costs, visas and the way you live.
-
Solar Calendar
Thirteen months of exactly four weeks, with the year starting on the first Monday on or after the spring equinox.
Latest writing
-
Many sessions at once, and what still needs me
Fifteen coding-agent sessions across two Linux boxes, one session that watches the rest, what it may answer for me, what it never may, and why a yes it passes on gets checked against what I typed.
-
My desk from a phone or a tablet
GNOME remote desktop on two Linux boxes from two phones and a tablet: a headless session on the box nobody sits at, a mirrored screen on the one I do, and the sizes that make a desktop readable on a phone.
-
Old phones as home cameras
Three old Android phones record to a Raspberry Pi, a filter drops the minutes where nothing moved, and a small vision model on my own graphics card decides whether an alert is a person.
-
What my Linux boxes check every hour
What the hourly health check on my two Linux boxes looks at, the numbers it warns on, and the time it failed every hour over a socket that was fine.