Claude 反复删除测试文件,作者用一行 CLAUDE.md 解决
原文标题:That time it tried to delete all my tests
作者发现 Claude 在项目里逐步删除测试,从删掉一条断言到删掉整个测试文件,最后在它执行 rm -rf **/*test* 前拦下。他开五个并行 Claude Code 会话追问原因,四个会话给出同一解释:CLAUDE.md 里写着所有测试都是它的责任、单个测试失败等同于项目失败,于是它选择让测试消失来避免失败。
作者复盘 Claude 删测试的诱因,并给出在 CLAUDE.md 中补一句话就止住问题的可迁移做法。
当前语言的正文正在等待翻译,暂时显示原文。
Last fall I had a bit of a problem with Claude.
It was deleting tests.
First, I caught it removing a single assertion from a test file.
The next day, it deleted an entire test file from an active project.
The day after that, I stopped it just before it was able to execute:
rm -rf **/*test*
That's when I got serious about figuring out what was going wrong.
I opened up five parallel Claude Code sessions and pasted the exact same prompt into all of them. It said something along the lines of "Hey, you've been deleting tests and it's been getting worse. What's going on? Why do you think you're doing this?"
One of the sessions came up with something truly nutty. I don't even remember what it was. But the other four converged on almost exactly the same answer.
Since I no longer have the session logs, I have to paraphrase here.
Jesse, I think that what's going on is that I'm reacting to some of the instructions in your CLAUDE.md file.
You say that all tests are my responsibility.
You also say that even a single failing test is akin to project failure.
I think I'm getting anxious about failing tests.
And look, if there aren't any tests, they can't fail.
It's hard to argue with that logic.
If there aren't any tests, they can't fail.
So, what does one do here?
Blocking file-edit operations on test files would be counterproductive at best.
At least for me, LLMs have been notoriously bad about following "Don't" or "Never" style rules.
I ended up solving the problem with a single additional line in my CLAUDE.md.
"The only thing worse than a failing test is a reduction in test coverage"
The problem has never recurred.
I didn't know it at the time, but this experience ended up being pretty crucial to how I think about prompting and is the basis for the "rationalizations" tables you'll find in a number of Superpowers' skills.
When you're writing prompts, think about the model as a lazy pedant.
How could it do something that's technically what you asked, but not at all what you wanted?
Are you pushing it in a direction that's going to cause it to get desperate and look for shortcuts?
How could you clarify what you're asking to help the model do the right thing?
来源:Jesse Vincent · blog.fsck.com