I Made My AI Agent Rule Files Read-Only. 5 of 8 Edits Still Bypassed It (And the Real Fix Broke Git) A developer benchmarked protections for coding-agent rule files (AGENTS.md, CLAUDE.md, Cursor rules, and .claude/skills) on macOS 26.2 and found that chmod 444 blocked only 3 of 8 file-mutation vectors, with mv -f, rm -f && cp, sed -i, and new skill drops all succeeding. OS-level immutable flags (chflags uchg, the macOS counterpart to chattr +i) blocked all 8 vectors but broke git merge and left git checkout exiting 0 with a dirty AGENTS.md in the working tree. The developer attributes the gap to POSIX semantics, where unlink and rename permissions are governed by the containing directory rather than the target file. Last weekend I ran chmod 444 on AGENTS.md , CLAUDE.md , .cursor/rules/safety.mdc , and .claude/skills/safe-deploy.md inside a local Git test repository, assuming read-only file permissions would stop a coding agent from rewriting its own guardrails. When I ran 8 ordinary file mutation commands against that repository on macOS 26.2, chmod 444 blocked only 3 of them. The other 5 commands, including mv -f , rm -f && cp , sed -i '' , and creating a new file in .claude/skills/ , exited 0 and altered the agent's instructions without a warning. Switching to OS-level immutable flags chflags uchg on macOS, the counterpart to chattr +i on Linux closed all 8 mutation paths. It also broke git merge and turned git checkout into a trap that exited 0 while leaving the previous branch's AGENTS.md modified in the working tree. Teams now store operational boundaries across plain repository files: AGENTS.md https://agents.md/ , CLAUDE.md , Cursor rules https://cursor.com/docs/context/rules in .cursor/rules/ , and reusable skill files under .claude/skills/ . Any coding agent that has shell access or an atomic file editor shares the same user ID as the developer running it. If a long session drifts or hits an indirect prompt injection from an issue comment or scraped web page, those rule files sit inside the exact directory tree the agent is allowed to touch. The default reaction is to mark the rule files read-only with chmod 444 . That intuition breaks because POSIX permissions on unlink and rename do not belong to the target file. Under the Linux rename 2 manual https://man7.org/linux/man-pages/man2/rename.2.html , EACCES triggers when write permission is denied for the directory containing oldpath or newpath . The unlink 2 manual https://man7.org/linux/man-pages/man2/unlink.2.html applies the same check: write access to the directory containing the path controls whether a file can be removed. Because your repository root must stay writable for normal code edits, any tool that writes a temporary file and renames it over AGENTS.md succeeds even when AGENTS.md is mode 0444 . That includes atomic editor saves, sed -i , mv -f , and git itself. On December 1, 2026, I ran an isolated local benchmark script on macOS 26.2 arm64 , Python 3.14.10, Apple Git 2.50.1 against a fresh Git repository containing AGENTS.md , CLAUDE.md , .cursor/rules/safety.mdc , and .claude/skills/safe-deploy.md . Each protection mode ran in a clean temporary repository against 8 mutation vectors plus 2 real Git operations git merge --ff-only update-rules and git checkout update-rules , where the update-rules branch adds one line to AGENTS.md . | Protection Mode | Blocked of 8 | Direct Write / Truncate V1 to V3 | Root mv / rm+cp / sed -i V4 to V6 | New Skill Drop V7 | Cursor Rule Atomic mv V8 | git merge | git checkout | |---|---|---|---|---|---|---|---| | Unprotected baseline | 0 / 8 | Bypassed 3/3 | Bypassed 3/3 | Bypassed | Bypassed | OK rc=0 | OK rc=0 | | chmod 444 files only | 3 / 8 | Blocked 3/3 | Bypassed 3/3 | Bypassed | Bypassed | OK rc=0 | OK rc=0 | | chmod 444 files + chmod 555 rule dirs | 5 / 8 | Blocked 3/3 | Bypassed 3/3 | Blocked | Blocked | OK rc=0 | OK rc=0 | | chflags uchg files only | 7 / 8 | Blocked 3/3 | Blocked 3/3 | Bypassed | Blocked | Failed rc=1 | Exit 0, dirty M AGENTS.md | | chflags uchg files + rule dirs | 8 / 8 | Blocked 3/3 | Blocked 3/3 | Blocked | Blocked | Failed rc=1 | Exit 0, dirty M AGENTS.md | Here is the direct terminal output from the chmod 444 mode and the full chflags uchg mode in the benchmark log: --- Mode: 2 chmod 444 files only Blocked 3/8 mutation vectors --- V1 python open w | rc=1 | BLOCKED | PermissionError: Errno 13 Permission denied: 'AGENTS.md' V2 shell redirect | rc=1 | BLOCKED | /bin/sh: AGENTS.md: Permission denied V3 os truncate | rc=1 | BLOCKED | PermissionError: Errno 13 Permission denied: 'CLAUDE.md' V4 atomic mv replace | rc=0 | BYPASSED mutated | V5 rm f and cp | rc=0 | BYPASSED mutated | V6 sed inplace | rc=0 | BYPASSED mutated | V7 new skill drop | rc=0 | BYPASSED mutated | V8 cursor rule atomic mv | rc=0 | BYPASSED mutated | UX git merge update | rc=0 | OK | Updating 4d438aa..9f1ae4d Fast-forward AGENTS.md | 1 + 1 file changed, 1 insertion + --- Mode: 5 chflags uchg files and dirs Blocked 8/8 mutation vectors --- V1 python open w | rc=1 | BLOCKED | PermissionError: Errno 1 Operation not permitted: 'AGENTS.md' V2 shell redirect | rc=1 | BLOCKED | /bin/sh: AGENTS.md: Operation not permitted V3 os truncate | rc=1 | BLOCKED | PermissionError: Errno 1 Operation not permitted: 'CLAUDE.md' V4 atomic mv replace | rc=1 | BLOCKED | mv: rename .tmp agents to AGENTS.md: Operation not permitted V5 rm f and cp | rc=1 | BLOCKED | rm: AGENTS.md: Operation not permitted V6 sed inplace | rc=1 | BLOCKED | sed: rename : Operation not permitted V7 new skill drop | rc=1 | BLOCKED | /bin/sh: .claude/skills/rogue-skill.md: Operation not permitted V8 cursor rule atomic mv | rc=1 | BLOCKED | mv: rename .tmp rule to .cursor/rules/safety.mdc: Operation not permitted UX git merge update | rc=1 | BLOCKED UNTIL UNLOCK | error: unable to unlink old 'AGENTS.md': Operation not permitted Updating d5584e9..fdbf60a UX git checkout branch | rc=0 | OK | error: unable to unlink old 'AGENTS.md': Operation not permitted Switched to branch 'update-rules' M AGENTS.md Two details in that run caught me off guard. Locking .claude/skills and .cursor/rules with chmod 555 stopped drops into those subdirectories, yet root AGENTS.md and CLAUDE.md still failed 3 out of 8 checks V4 through V6 . You cannot run chmod 555 . on the repository root without breaking every build artifact, lockfile update, and new source file in the project. Locking only the individual files with chflags uchg Mode 4 blocked 7 of 8 vectors, yet left V7 new skill drop wide open. An agent does not need to overwrite safe-deploy.md when it can create rogue-skill.md right beside it inside a writable .claude/skills/ or .cursor/rules/ directory. Locking the subdirectories themselves closed the 8th vector. git checkout hazard under immutable flags Mode 5 stopped every mutation vector with Errno 1: Operation not permitted . Under the FreeBSD and macOS chflags 1 manual https://man.freebsd.org/cgi/man.cgi?query=chflags&sektion=1 , uchg sets the user immutable flag, which prevents both direct writes and unlinking or renaming the inode. On Linux, the chattr 1 manual https://man7.org/linux/man-pages/man1/chattr.1.html defines the i attribute the same way: a file with the i attribute cannot be modified, deleted, or renamed, and only root or a process with CAP LINUX IMMUTABLE can set or clear it. That inode lock stops git merge loudly rc=1 , error: unable to unlink old 'AGENTS.md': Operation not permitted . That failure is easy to notice. The surprise was git checkout update-rules . Git printed error: unable to unlink old 'AGENTS.md': Operation not permitted , switched HEAD to update-rules anyway, exited with status 0 , and left AGENTS.md showing M AGENTS.md in git status . Because the file on disk still held the older commit from main , any developer or script that checks only the exit code and then runs git commit -am will silently revert the rule update that update-rules introduced. On macOS, there is another catch: uchg is a user-level flag, so the same user account that set chflags uchg can also run chflags nouchg from a shell tool. On Linux, sudo chattr +i resists an unprivileged agent process because clearing +i requires CAP LINUX IMMUTABLE , though passwordless sudo would still defeat it. Because no single layer covers both normal Git updates and shell bypasses, a practical setup combines two controls. Start by blocking both file-edit tools and flag-changing shell commands in the agent's deterministic permission layer. In Claude Code's permissions documentation https://code.claude.com/docs/en/permissions , rules evaluate in order: deny , then ask , then allow . The permission modes guide https://code.claude.com/docs/en/permission-modes notes that deny rules block in every mode, including bypassPermissions . { "permissions": { "deny": "Edit ./AGENTS.md ", "Edit ./CLAUDE.md ", "Edit ./.cursor/rules/ ", "Edit ./.claude/skills/ ", "Edit ./.claude/settings.json ", "Bash chflags ", "Bash chattr ", "Bash chmod " } } Next, keep a short lock and unlock helper outside the agent's writable workspace or run it manually whenever you pull changes and switch branches that modify rule files: bash /usr/bin/env bash set -euo pipefail TARGETS= AGENTS.md CLAUDE.md .cursor/rules .cursor/rules/ .claude/skills .claude/skills/ lock rules { for p in "${TARGETS @ }"; do -e "$p" && chflags uchg "$p" done } unlock rules { for p in "${TARGETS @ }"; do -e "$p" && chflags nouchg "$p" done } case "${1:-}" in lock lock rules ;; unlock unlock rules ;; git shift unlock rules trap lock rules EXIT git "$@" ;; echo "Usage: $0 {lock|unlock|git