Why your AI agent keeps reverting your fix
Your agent sees the code, not why it's written that way. Put the reason where it reads, add a test that fails on a revert, and block what must never change.
On this page
Your AI agent keeps reverting your fix because it sees the code, not the reason the code is that way. A fix that looks odd, a workaround, a check that seems redundant: to an agent with no history, each one looks like something to clean up. The cure is a ladder. Write the reason down where the agent reads it. Add a test that fails if the fix is undone. And for the few things that must never change, block the edit outright.
Claims about Claude Code and Cursor below are as of November 2026, from their own docs, linked inline.
| Rung | What it does | Stops a revert? | Best for |
|---|---|---|---|
| A comment at the fix | Tells the agent why, right where it edits | Only if read and believed | Local tricks and workarounds |
| A decision record | Tells every session why, before it starts | Only if read and believed | Choices that span files |
| A test or lint rule | Fails the moment the fix is undone | Yes, if the agent runs it | Any behaviour you can check |
| A hook or a reviewer | Refuses the edit, or catches it before merge | Yes | Files and choices that must never change |
Why does the agent undo working code?
An agent starts every session from nothing. It reads your files and your instructions, and it reasons from what’s in front of it. If your fix is a hand-rolled parser where a library would be the obvious choice, the agent sees an improvement waiting to happen. Nothing in the code says “on purpose”.
It gets worse inside a long task. The agent fixes bug A, which breaks B. You point at B, and the quickest way to make B pass is to undo the fix for A. The agent isn’t being careless. It doesn’t know that A was ever a problem, or it lost that detail when the conversation was summarized (see why Claude Code forgets after compaction).
Rule out one thing first: a tool bug. On Cursor’s forum, one report of AI edits reverting on specific files turned out to be a bug with the Agent Review tab, not the model. If the file changes back with no agent turn in between, check the editor, file sync and formatters before you blame the agent.
Rung 1: put the reason where the agent reads it
Most reverts stop once the agent knows why. Where you write the reason depends on how far it reaches.
A comment beside the fix is the cheapest and often the best. The agent reads it in the same moment it’s tempted to change the line. “Hand-written on purpose: the date library misreads dates in some of our users’ locales” does more than any rule file. For a trick that lives in one function, nothing beats this, Memorinth included.
An instruction file reaches every session. Claude Code loads CLAUDE.md, Cursor loads project rules from .cursor/rules. Cursor’s rules have types: some always apply, some apply when a file matches a pattern, some only when the agent judges them relevant. A rule the agent doesn’t load can’t help, so put a “never undo this” rule on always, or scope it to the files it protects. Note too that Cursor’s docs say rules “do not impact Cursor Tab”.
A decision record is for choices that span files and need a reason longer than a line. It holds the choice, why it was made and what it costs. What a decision record is, and how Memorinth keeps them, is covered in decisions and facts for agents. The point here is narrower: the reason is the part that stops a revert. “We use our own date parser” invites a cleanup. “We use our own date parser because the library broke on our users’ locales” doesn’t.
Why the agent ignores what you wrote
Instructions are advice to the model, not a lock. Claude Code’s memory docs say it plainly: Claude treats CLAUDE.md and auto memory “as context, not enforced configuration”. The same page adds that the more specific and concise your instructions, the more consistently Claude follows them.
So if a written reason didn’t hold, check three things:
- Was it loaded? A nested file or a rule scoped to other paths may not be in context when the agent edits.
- Is it specific? “Be careful with dates” loses to a failing test. “Keep
parse_datehand-written; the library misreads dates in our users’ locales” doesn’t. - Is it drowned out? A long instructions file buries one line among hundreds.
When it still slips, climb a rung.
Rung 2: make the revert fail a check
A test is the strongest thing short of a block, because the agent doesn’t have to believe it. Write one that reproduces the bug your fix solved. If the agent undoes the fix, the test fails, and an agent that runs the tests sees it at once.
Name the test after the reason, not the function: parses_dates_in_every_supported_locale tells the agent what it broke. Ask for this whenever an agent fixes a bug: the fix and a test that would have caught it, in the same change.
Where a test can’t express the rule, a lint rule often can. “Don’t import this library”, “don’t call this function outside this module”: most linters can ban an import or a call, and the error message can carry the reason.
A check only works if the agent runs it. Say in your instructions file which command runs the tests, and that it must pass before the work is done.
Rung 3: block the edit, or review it
Some things must never change without you: migration files once applied, a vendored file, a security check. For those, don’t rely on the agent’s judgment at all.
In Claude Code, a PreToolUse hook runs before every tool call and can refuse it. Exit with code 2 and the call is blocked, with your message shown to Claude. A hook that refuses edits to applied migrations (it uses jq to read the file path from the call):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | grep -q 'migrations/' && { echo 'Applied migrations are never edited. Add a new one.' >&2; exit 2; } || exit 0"
}
]
}
]
}
}The message is the reason, so the agent learns what to do instead, not just that it can’t.
The last rung is you. Read the diff before it merges, and look hardest at deletions near code you fixed. A revert shows up as your own lines coming back out.
The opposite failure: an agent that won’t let go
Writing reasons down has a cost. An agent follows a written decision for as long as it stands, including after you’ve changed your mind. A line in an instructions file from six months ago that says “never use the library” will still be obeyed when the library has fixed the bug. Now the agent is protecting a fix that should go.
Two habits prevent this:
- Retire a decision explicitly. Don’t just delete the line: say it changed and why, so no one restores it. A decision record that’s superseded rather than erased does this for you.
- Let the agent object, not overrule. Should an agent be allowed to overrule a decision record? It should be able to say “this decision looks wrong, here’s the evidence”. It shouldn’t quietly work around one. Ask for the first in your instructions; you decide, and the record changes.
How Memorinth handles both
Memorinth keeps decisions as notes an agent reads and writes over MCP. Each has a status: accepted, superseded or deprecated. The briefing at the start of every session lists the accepted ones, so the agent knows your choices and their reasons before it touches code. Search ranks accepted decisions higher and hides superseded ones unless the agent asks for history.
That covers rung 1 across every agent you connect, and the stale-decision problem: when you change your mind, the agent records a new decision with supersedes, or calls deprecate_decision with the reason. The old one stays, marked, so the next agent sees both the change and why.
It doesn’t cover rungs 2 and 3. A memory layer can tell an agent why; only a test or a hook can stop it. If you use one tool and have a handful of decisions, a few lines in CLAUDE.md plus good tests are enough, and you don’t need Memorinth for this.
To write decisions agents follow well, read Writing decisions. Decisions covers how they’re stored and superseded, and How agents use it how the briefing reaches the agent.