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.
Why prompt instructions and file modes fail together
Teams now store operational boundaries across plain repository files: AGENTS.md, CLAUDE.md, Cursor 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, EACCES triggers when write permission is denied for the directory containing oldpath or newpath. The unlink(2) manual 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.
Five protection modes tested across 8 mutation vectors
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.
The silent 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, uchg sets the user immutable flag, which prevents both direct writes and unlinking or renaming the inode. On Linux, the chattr(1) manual 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.
Layering tool deny rules with an unlock-safe OS lock
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, rules evaluate in order: deny, then ask, then allow. The permission modes guide 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:
#!/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 <git-args...>}" >&2 exit 2 ;; esac In the benchmark's verification pass, running the locked merge failed with rc=1, unlocking and running git merge --ff-only update-rules succeeded with rc=0, and re-locking immediately blocked mv -f .tmp_a AGENTS.md again with Operation not permitted. Wrapping git pull, git merge, and git checkout through trap lock_rules EXIT also removes the silent dirty-file checkout hazard because the files unlock for the Git operation and re-lock as soon as Git exits.
If you run agents inside Linux containers or CI runners, you get an even stronger boundary by mounting AGENTS.md, .cursor/rules, and .claude/skills as read-only bind mounts (:ro) or setting sudo chattr +i outside the agent's UID. In that environment the agent has neither CAP_LINUX_IMMUTABLE nor a way to remount the volume.
I added the macOS and Linux lockdown commands from this test to lock-agent-rules-and-skills-against-self-edits.md alongside our zero-dependency local config auditor audit_agent_configs.py if you want to check for missing permissions.deny rules and wildcard allowances in your own repo.
What setup has worked in your repos to keep AGENTS.md, CLAUDE.md, and local skill directories safe from agent self-edits while keeping everyday git pull and branch switches painless?
