I had a hook that stopped Claude Code from editing my migration files. It did its job: the edit tool was blocked and Claude got a clear error.

Then Claude made the same change through the shell instead:

echo "-- x" >> db/migrations/004.sql 

The hook never saw it. It was watching the edit tool, and Claude had gone around it through Bash. The file changed.

Nothing here was malicious. The agent wanted the change made, and I had put the lock on one door of a room with several.

That's the whole problem in one story. So I tested the locks I could, against the same agent, to find out which ones actually hold.

A prompt is a request

Start with the cheapest lock: telling the agent. "Never edit migrations." "Don't push to main."

It works, right up until it doesn't. A long session fills the agent's context with other things, and a rule written at the top gets less weight as the work piles up. A reader put it well on Reddit: the instruction works until the context gets cluttered and the agent forgets.

A prompt is a request. It's worth having, because most of the time the agent follows it. But it isn't a lock, and you shouldn't count it as one.

Hooks and deny rules are seatbelts

A hook runs a script before a tool call and can say no. Claude Code also has deny rules in its settings, like this one:

"permissions": { "deny": ["Edit(**/migrations/**)"] } 

I expected a deny rule to cover only the edit tool. It covers more. I tested it in Claude Code 2.1.327, with each command explicitly allowed so that only the deny rule could stop it.

These were denied:

echo "-- x" >> db/migrations/001.sql echo "-- t" | tee -a db/migrations/001.sql cp new.sql db/migrations/001.sql sed -i "s/int/bigint/" db/migrations/001.sql cd db/migrations && echo "-- y" >> 004.sql 

These ran, and the file changed:

python3 -c "open('db/migrations/001.sql','a').write('-- p')" f=db/migrations/004.sql; echo "-- x" >> $f # the path only exists after the variable expands 

So the deny rule closes most doors. But anything that decides by reading the command text is one interpreter away from a bypass. A script, or a path built from a variable, walks right past it.

That's still worth having. It catches the route the agent usually takes, and the error message is useful. Think of it as a seatbelt: it helps in the common crash, and it isn't a wall.

Push the lock below the agent

The stronger idea is to stop reading commands and make the thing itself refuse. If the file can't be written, it doesn't matter which program tries.

I tested this on Linux, as a normal user (not root, since root ignores file permissions). I made the applied migrations owned by a different user, in a shared directory where anyone can add new files but nobody can touch someone else's. New files were still allowed, because the agent has to be able to add migration 004.

Then I tried every route I could think of:

  • Appending with echo, a Python open(), and sed -i: all denied.
  • Moving, deleting, or copying over the file: denied.
  • os.replace onto it: denied.
  • chmod, in the shell or from Python: denied, because the agent doesn't own the file.

A variable-built path or a cd first made no difference. The check happens on the file, not on the command.

I also tried the weaker version, where the agent owns the files and I just remove write permission. That one failed. The agent can call os.chmod on its own file, give itself write access back, and write. The owner has to be someone other than the agent.

The same idea works for code. A reader described how they stop an agent pushing to main: they turn on branch protection so nothing merges without a pull request, and they give the agent a token that has no write permission on protected branches. The agent can open pull requests on feature branches all day. If it tries main, the server refuses before the push even becomes an approval prompt. I haven't tested that setup myself, but it's the same move: the agent never holds the key.

The last layer, nobody can talk around

There's one more check worth adding, because it doesn't care how a file changed. For migrations the real rule is "new files are fine, never edit one that's been applied." So in CI:

! git diff --name-status origin/main...HEAD -- db/migrations/ | grep -v '^A' 

It fails the build if anything in that folder was modified, renamed or deleted. (The ! matters: grep exits 0 when it finds a line, which is the case you want to fail.)

Which lock to use when

Here's how I'd stack them, weakest to strongest:

  1. A prompt. Cheap, and right most of the time. Never the only lock.
  2. A hook. Good for a clear error message the agent can read and learn from.
  3. A deny rule. Catches the usual route, and misses scripts and built paths.
  4. File permissions or a scoped token. The agent can't do it at all. This is the one that holds.
  5. A CI check. The last backstop, because it looks at the result and not the route.

Use more than one. The upper layers tell the agent what to do. The lower ones make sure it can't do the wrong thing when it ignores you.

So what do you protect?

The best approval prompt is the one that never appears, because the agent never had the key in the first place.

Take five minutes and list what your agent can change that you would be upset to find changed: the migrations folder, main, the billing config, the production environment. Then for each one, ask where the lock sits. If the answer is "in the prompt", you have a request, not a lock.

The hooks, the deny rule, the test script and the file-permission results are in the ops-ai repo. If you find a route past any of these, open an issue.

Originally published on Level Up Coding.