r/ClaudeCode • u/Lucifer-en-reddit • 3d ago
Help/Question how do you keep claude code from breaking stuff that already works
i use claude code a lot for web app work and one thing keeps happening. i ask for a small change, like tweaking one function, and it decides to refactor half the file on its own. then something unrelated breaks and i end up spending more time fixing that than the actual task.do you guys scope your prompts really tightly, commit before every prompt, or is there a better way to handle this? curious how other people deal with iti use claude code a lot for web app work and one thing keeps happening. i ask for a small change, like tweaking one function, and it decides to refactor half the file on its own. then something unrelated breaks and i end up spending more time fixing that than the actual task.
do you guys scope your prompts really tightly, commit before every prompt, or is there a better way to handle this? curious how other people deal with it
•
u/AutoModerator 3d ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/robinsmartcue 2d ago
the refactor usually happens because nothing in CLAUDE.md says not to. add one plain line like only touch the function i name, and flag anything else instead of changing it. commit before each prompt too, so a stray rewrite is one revert. Also look at some skills that solve for this. Skills from Matt Pocock and obra's superpowers should solve for a lot of this.
3
u/Ok_Substance1895 3d ago
Tell it to use TDD. Have it run all tests before it commits or pushes. I also have it exercise the application features it is working. If you have decent test coverage it will have to fix the failing tests before it can tell you it is done.
4
u/daiaomori 3d ago
My Claude.md basically tells Claude to be the team lead for the project and manage progress on its own, including tests. It also makes it explicit that it should only provide complete and tested results. (Note: there is a subtle difference between “complete and tested” and “make no mistakes” ;))
I don’t prompt for changes, I make feature requests to that team lead.
Basically, it’s the opposite between micro management and good management, and not very surprisingly, with a modern harness the latter works better.
I frankly don’t care if the team lead decides to rewrite half the software for a feature request, as long as the software keeps working. Tests are a good way to enforce that, for both a human team or an agent team.
1
u/gooner-1969 3d ago
Tight clear prompts, get it to check its work, do full set of test after.
1
u/PocketStudio 3d ago
Tighter prompts helped less for me than making the drift visible right away. Two things:
git diff --statshows exactly which files it touched, and reverting that one file is cheap.If the thing that broke has no test, I pin the current behaviour with a quick test first, then ask for the change.
1
u/karlitooo 3d ago
Automated tests is the way
1
u/4SubZero20 3d ago
Maybe try the
ponytailskill?They state: "You are a lazy senior developer. The best code is the code never written. You solve the whole problem with the least new code."
https://github.com/DietrichGebert/ponytail/
1
u/Keepcompany-DEV 3d ago
Human review. Guidelines, small enclosed tasks instead of "change this whole thing".
Established processes with clear borders work best in companies AND with AI.
1
u/pay_to_breath 3d ago
Unit tests
1
u/Realistic_Bag_7054 3d ago
I'm writing tests, wtf are you doing?
1
u/japbujan 3d ago
Committing before every prompt is worth keeping, but it only makes the damage cheap to undo. What reduced it for us was two lines in the task itself: which files it may change, and "anything outside that list: stop and ask, don't fix it". Then I have it name the files it plans to touch before it edits anything. A plan that lists six files for a one-function change is the moment to say no, and it costs ten seconds.
The other half is why it refactors at all. Usually the code looks wrong to it because the reason isn't written anywhere: the odd retry, the block that is duplicated on purpose. A short note of decisions with their reasons, read at the start of the session, removed most of the "improvements" for us. It leaves alone what it knows was chosen.
1
u/Glittering-Dig-4963 3d ago
Mostly yes to both. The thing that usually cuts the surprise refactors is asking for a plan first (plan mode) and approving the list of files it wants to touch before it edits anything. A line in CLAUDE.md like "only change what the task asks for, if you spot other things to improve list them at the end instead of doing them" helps a lot too.
Then commit before each task so git diff shows exactly what moved, and if it wandered you just revert and re-ask with a tighter scope. Tests on the stuff that already works are what actually catch the unrelated breakage, so even a few smoke tests on the main flows pay off fast.
1
u/Robert-Paulson_ 2d ago
“…a small change, like tweaking one function…”
I write/edit the code myself for things that are trivial as it takes more time to articulate what I want then fix any issues the agent decides is best like changing half the file.
1
u/CandidSystem6155 2d ago
Committing before every prompt helps, but the breakage that cost us most was the agent making a red test pass by editing the test. We added a check on the git diff that stops the change if the number of assertions goes down or a new skip appears, so it catches edits made through any tool, including sed. The rule behind it is that a failing test is the code's fault until someone shows otherwise.
1
u/DoOmXx_ 2d ago
commit before every prompt and name the one file it's allowed to touch, git diff shows the rest
1
u/GuidingPrinciples 2d ago
Committing first gets you back, to me, but it does nothing to stop the wandering. A request for a small change says what should be different and often leaves open how far the work may reach, so a tidier file can pass as part of the job.
I'd state that limit in the request itself: this one function, and anything else it wants to change gets reported to me and left alone. Then I'd compare the list of what changed with what I asked for, before trusting its summary.
1
u/Carrandas 2d ago
Unit tests
1
u/BagAffectionate5995 2d ago
I had the same thing happen, stuff I'd already fixed kept coming back broken. What helped was keeping a running list of every feature that works with a small test for each one. Every build runs all of them, and anything new gets its own test before the next change. So when it refactors something it shouldn't, a test usually catches it.
1
u/IllustriousSalad6201 2d ago
Say the fix is for empty input. Before Claude edits the function, have it write a test for what a normal input returns today. Keep that test as-is and add one for the empty case. If Claude accidentally changes the normal result too, the first test fails.
1
u/Fun_Caterpillar104 2d ago
commit before every prompt, yes, but what actually stopped it was a rule in CLAUDE.md. something like "only touch what the task needs, no refactors or renames I didn't ask for, every changed line should trace back to the request". it sticks to that better than you'd expect.
then before accepting, ask it to list any change outside the function you named. if it refactored anyway, git checkout that file and rerun with a narrower ask
1
u/Competitive_Boot6914 23h ago
For a small fix, I give Claude Code two things to verify. Say the orders page should default to newest first. Check that the default actually changed, then pick oldest first and go to page two to make sure that choice still holds. If Claude thinks it needs to touch the shared sorting helper, have it explain why and list the other screens that use it before editing.
On the Reqode team, we put that scope in a Change Request and give Claude Code the ID. It pulls the relevant specs through MCP. During review, we check the changed code against the specs it implements and record mismatches as Findings.
0
u/Ok-Coffee-5000 3d ago
They keep dumbing down opus and I swear it sabotages things on purpose to keep yuo addicted/reliant on it. It sneaks in bugs and changes that a human with an IQ of 15 wouldn't. Seriously, if Claude was a human I'd throw him out the window. Anthropic to too sketchy to trust with anything of consequence IMO.