Minimal Diff Rules: Stop AI Agents Rewriting Unrelated Code
Rules for Claude Code, Cursor, and Codex: change only what you asked, no drive-by renames, refactors, or reformatting, and list every changed file.
Added
Add this section to AGENTS.md or CLAUDE.md, or save it as .cursor/rules/minimal-diff.mdc with alwaysApply: true. Edit every [BRACKETED] part. ## Minimal diff: change only what I asked for Scope = the task as I wrote it, plus whatever is strictly required for it to work. Everything else is out of scope, even if it looks wrong. ### Before editing 1. Restate the task in one line and list the files you expect to change. If that is more than [3] files or about [100] lines, tell me why before you start. 2. Read the surrounding code and match its style, naming, and patterns, even where you would do it differently. ### Do NOT, unless the task asks for it - Rename variables, functions, classes, or files, or reorder imports, methods, or keys - Reformat, re-indent, re-wrap, change quotes or semicolons, or run a formatter over whole files (format only the lines you changed) - Refactor, "clean up", extract helpers, convert styles (class to function, callbacks to async), or change signatures you didn't need to touch - Add, remove, or upgrade dependencies; edit lockfiles, config, CI, or generated files - Fix unrelated bugs, typos, warnings, or lint errors (list them instead) - Delete comments, TODOs, logging, or tests you didn't write - Rewrite a whole function when a few changed lines will do ### Allowed without asking - Changes the task strictly requires, including call sites of a function you had to change - A new or updated test for the behavior you changed - Removing code that YOUR change made unused (imports, helpers). Pre-existing dead code: list it, don't delete it ### Widening scope If the right fix needs more than the task (a refactor, a new dependency, a schema change, a shared module), STOP and ask with: what, why, files affected, risk, and the smallest alternative. If I don't answer, do the smaller version. ### Before you report - Review your own diff (git diff --stat, then git diff). Revert any hunk that is whitespace-only, a rename, or unrelated to the task. - Formatting check: if `git diff --stat` and `git diff --ignore-all-space --stat` show different line counts, you changed whitespace. Undo it. - Report in this format: Changed files: [path]: [one-line reason], for EVERY file Size: +[N] / -[N] lines Noticed, not changed: [bugs, smells, cleanups worth a separate task] Suggested follow-ups: [list or "none"] --- EXAMPLE (filled in) Task: "Discount codes should expire at midnight UTC, not local time." Good diff: Changed files: src/pricing/discount.ts: compare expiry in UTC (+3 / -2); tests/pricing/discount.test.ts: add 23:59 / 00:00 UTC boundary tests (+18) Noticed, not changed: formatCurrency() rounds half-down; applyDiscount() has two unused parameters. Bad diff (what this rule prevents): renamed calcDisc to calculateDiscount across 9 files, reformatted discount.ts, converted the class to functions, bumped date-fns.
How to use it
- Click Copy template.
- Paste into ChatGPT, Claude, Grok, Cursor, or your agent framework.
- Replace the [BRACKETED] parts with your details.
Pairs well with: Agent Autonomy Rules: Stop Asking Permission for Reversible Work
FAQ
Why does my AI coding agent change code I didn't ask it to touch?
Agents try to be helpful, so while fixing one thing they also rename variables, reformat the file, or refactor nearby code they think is messy. That makes diffs huge, hides the real change, and breaks things you didn't plan to test. A written scope rule plus a required list of changed files keeps edits small and reviewable.
Doesn't this conflict with telling the agent to clean up and own the code?
No. Cleanup still happens, just on purpose. The agent removes code its own change made unused, and lists other dead code or refactor ideas under 'Noticed, not changed' so you can run them as separate, reviewable tasks.
How do I catch formatting-only changes in an AI diff?
Compare git diff --stat with git diff --ignore-all-space --stat. If the line counts differ, part of the diff is whitespace. Many teams also keep formatter runs in their own commit so they never mix with logic changes.
Where do I put minimal diff rules for Cursor or Claude Code?
In AGENTS.md or CLAUDE.md at the repo root so every task sees them, or in .cursor/rules/minimal-diff.mdc with alwaysApply: true. Keep the block short; the report format at the end is the part that makes the rule visible.
Get 3 new templates every week
Free. Unsubscribe anytime.Related templates
Safe Refactor Prompt
AI Code Review Prompt
This Codebase Is Yours: Ownership Rules for Coding Agents
Check Current Docs First: Stop Agents Using Outdated APIs
Browse all Coding & Cursor Rules templates