How do you stop AI coding tools from editing the wrong files?

Whenever I give a task to an AI agent, it usually does the job well, but sometimes it decides to clean up or change files I never asked it to touch.

Lately, I started writing a quick list of rules at the top of my prompt just to tell it which folders to ignore. It saves me a lot of time fixing broken code later.

What kind of notes or rules do you write before letting an agent run a task?

6 views

Add a comment

Replies

Best

Rules at the top of the prompt help, but an agent can still forget them halfway through. What works better for me is splitting it in two. The CLAUDE.md files explain what I want, and the hard limits live in hooks and deny rules it can't skip, about 90 hooks and 25 deny rules right now. One example: it isn't allowed to delete or overwrite through a symlink, so it has to check first.

A list at the top of each prompt works, but you have to paste it every time, and when it's missing, it's usually when it matters. Two things tend to hold up better:

1. Put the list in the project's rules file ( in Claude Code, or the equivalent rules file in other tools). It's read at the start of every session, so "leave /legacy and /generated alone" no longer depends on remembering to paste it. A line like "change only the files the task names; point out other problems instead of fixing them" also covers the surprise cleanups.

2. For folders that must never change, don't rely on a rule the model reads. In Claude Code, a PreToolUse hook on Edit and Write can check the file path and exit 2 with a reason, which stops the edit and tells the agent why. A permission deny rule on the same path is the stricter lock.

One habit after each task: run git diff --stat before accepting. Every touched file shows up on one screen, so an edit outside the task is easy to spot.