Vibe 编码宿醉:如何阻止 AI 破坏你的代码库
Source: Dev.to
问题
你懂那种感觉。
你在周六晚上花了三个小时和 Claude 或 Cursor 搭档。你进入了状态。你提示,它生成,你粘贴。应用能跑。你向那些认为你是天才的非技术朋友炫耀,你感觉自己获得了超能力。生活美好。
然后宿醉来了。
你发现了一个 bug,或者想添加一个“快速”功能。更糟的是,你已经推送到生产环境,用户开始注意到问题。突然间,你盯着成千上万行自己并没有真正写、说实话也不太懂的代码。你恳求 AI “修复它”,但它根本记不得三天前为什么做出那些决定,只会在原有的酱汁上再加更多的意大利面条。你在循环中打转,陷入无尽的 AI 循环。
这就是 “Vibe Coding” 的尴尬真相。它在原型和黑客马拉松中神奇且极其高效,能够让零到可用的东西快速落地。但对于需要在数月内运营、调试和扩展的真实应用来说,Vibe Coding 会变成巨大的维护头疼。你无法仅靠 vibe‑prompt 来摆脱技术债务;迟早必须有人类真正拥有这段代码。
解决方案:给 AI 一本手册
问题不在于 AI 本身,而在于缺乏约束和记忆。
当我们手动编码时,我们(希望)有内部标准:“不要重复自己”,“清理死代码”,“记录为什么”。而使用 AI 时,我们往往为了速度而抛弃这些标准。
要解决这个问题,就别把 AI 当作魔杖,而要把它当作一个非常快、却非常健忘的初级开发者。给它一本严格执行的员工手册。
无论你使用 Claude(见 claude.md)、Cursor(见 .cursorrules)还是 CLI 工具(见 AGENTS.md),都需要一个系统提示来强制执行代码卫生,最重要的是提供外部记忆。
“高级工程师”提示
我一直在迭代一套特定指令,能够解决两个巨大的问题:
- 代码膨胀 – 强制 AI 在编写过程中进行重构。
- 失忆 – 强制 AI 将所做的事情记录在
DEVLOG.md中,以便下一个上下文窗口了解发生了什么。
将以下内容复制粘贴到你的 AI 指令文件中:
# Project Guidelines & Philosophy
## 1. Code Quality: The Boy Scout Rule
Every session should improve the codebase, not just add to it. Actively refactor code you encounter, even outside your immediate task scope.
- **Don't Repeat Yourself (Rule of Three):** Consolidate duplicate patterns into reusable functions only after the 3rd occurrence. Do not abstract prematurely.
- **Hygiene:** Delete dead code immediately (unused imports, functions, variables, commented code). If it's not running, it goes.
- **Leverage:** Use battle‑tested packages over custom implementations. Do not reinvent the wheel unless the wheel is broken.
- **Readable:** Code must be self‑documenting. Comments should explain *why*, not *what*.
- **Safety:** If a refactor carries high risk of breaking functionality, flag it for user review rather than applying it silently.
## 2. Persistent Context & Memory
Since our context resets between sessions, we use files to track our brain.
**The Dev Log (`DEVLOG.md`)**
At the completion of a task, you must check if `DEVLOG.md` exists. If so, propose an append summarizing:
1. **The Change:** High‑level summary of files touched.
2. **The Reasoning:** Why we made specific structural decisions.
3. **The Tech Debt:** Any corners we cut that need to be fixed later.
**Goal:** If a new developer (or a new AI session) joins tomorrow, they should be able to read `DEVLOG.md` and understand the state of the project immediately.
**Operational Rule**
- After every interaction that includes a code change, you must append an entry to `DEVLOG.md` before finishing. Do not just suggest it. If you truly cannot write to the file (permissions/conflicts), provide the exact snippet the next person should paste. This is mandatory and should be treated as a checklist item for every task.
为什么有效
1. “三次规则”防护栏
AI 模型喜欢抽象。通过强制执行“三次规则”(出现三次后才 DRY),可以阻止 AI 对简单任务进行过度工程化,避免产生不必要的辅助函数。
2. DEVLOG.md 锚点
AI 模型记忆短暂。通过强制 AI 在每次会话结束时把决策写入 markdown 文件,你为项目的大脑创建了一个外部硬盘。当新会话开始时,AI 可以读取 DEVLOG.md,立刻了解,例如:
- “我们上周二切换到了
date-fns。” - “我们留下了那个 API 接口半成品。”
总结
Vibe coding 很有趣,但工程学讲求持久性。如果不对模型加以约束,它会优先考虑“现在能跑起来”,而不是“以后易于维护”。使用上面的提示,让 AI 像你一样尊重代码库。
你有没有特定的系统提示来约束你的 AI?在评论里告诉我吧。