一、准备工作
1.1 环境检查
bash
# 检查Claude Code版本
claude --version
# 检查Node.js版本(建议18+)
node --version
# 检查pnpm/npm
pnpm --version # 或 npm --version
1.2 创建配置目录
bash
# 创建项目配置目录
mkdir -p .claude
cd .claude
# 创建hooks配置文件
touch config.json
# 创建CLAUDE.md规则文件
touch CLAUDE.md
二、第一步:创建CLAUDE.md规则文件
2.1 基础规则模板
markdown
# CLAUDE.md - 项目规则配置
## 规则
- 绝对不要重构、重命名或清理无关代码
- 所有数据库查询走 services/,不要写在组件里
- 每次改完代码运行 npx tsc --noEmit
- 每次改完跑测试,失败了先修好再继续
- 提交前缀规范:feat:、fix:、docs:、refactor:、test:、chore:
- 绝对不用 enum,用字面量联合类型代替
- 绝对不要 force push,会覆盖共享历史
- 先跑 migration 再跑测试,顺序不能反
## 从错误中学到的
- 不要在 packages/ui 里装本该装在 apps/web 的包
- 不要把两种现有模式平均一下,选一种跟到底
- 不确定文件夹结构时,先查现有文件
- stop-loss 函数必须返回数字,不是布尔值(2026-05-12 生产环境出过问题)
2.2 规则维护技巧
每次纠正Claude后,一定要加一句:
更新你的CLAUDE.md,这样你就不会再犯这个错了。
2.3 规则数量控制
研究表明12条规则、200行以内是最佳配置:
bash
# 检查规则数量
grep -E "^- " CLAUDE.md | wc -l
# 检查文件大小
wc -l CLAUDE.md
三、第二步:配置PostToolUse钩子
3.1 配置文件结构
json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write(*.ts)",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $file"
},
{
"type": "command",
"command": "npx tsc --noEmit 2>&1 | head -20"
}
]
},
{
"matcher": "Write(*.tsx)",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $file"
},
{
"type": "command",
"command": "npx eslint --fix $file"
}
]
}
]
}
}
3.2 钩子参数说明
| 参数 | 说明 | 示例 |
|---|---|---|
| matcher | 匹配模式(glob风格) | Write(*.ts) |
| type | 钩子类型 | command 或 prompt |
| command | 执行的命令 | npx prettier --write $file |
| $file | 当前文件路径占位符 | 自动替换为实际路径 |
3.3 验证钩子配置
bash
# 验证JSON格式
cat .claude/config.json | jq .
# 测试命令是否可用
npx prettier --version
npx tsc --version
npx eslint --version
四、第三步:配置Stop钩子(质量门禁)
4.1 基础配置
json
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "npm test 2>&1 | tail -5; echo \"Exit code: $?\""
}
]
}
]
}
}
4.2 带检查的完整配置
json
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "if [ \"$stop_hook_active\" = \"true\" ]; then exit 0; fi; npm test 2>&1 | tail -10; echo \"Exit: $?\""
}
]
}
]
}
}
4.3 Prompt类型钩子(高级)
json
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "prompt",
"prompt": "检查所有任务是否已完成。检查代码改动中是否有bug、遗漏的边界情况和测试覆盖。如果有未完成或有问题的地方,继续工作。如果一切正常,确认完成。"
}
]
}
]
}
}
4.4 关键注意事项
必须检查 stop_hook_active,否则会陷入无限循环:
bash
# 错误示例(会导致无限循环)
npm test
# 正确示例
if [ "$stop_hook_active" = "true" ]; then exit 0; fi
npm test
五、第四步:配置PreToolUse钩子
5.1 输入过滤配置
json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash(cat *log*)",
"hooks": [
{
"type": "command",
"command": "grep -n 'ERROR\\|WARN' $file | head -50"
}
]
},
{
"matcher": "Write(**/.env*)",
"hooks": [
{
"type": "command",
"command": "echo 'BLOCKED: Cannot write to .env files' && exit 1"
}
]
}
]
}
}
5.2 安全拦截规则
| 操作 | 处理方式 | 原因 |
|---|---|---|
| 读取.env | 拦截 | 敏感信息 |
| 写入.env | 拦截 | 配置安全 |
| rm -rf | 拦截 | 危险操作 |
| git push | 拦截 | 防止误操作 |
| cat日志 | 过滤 | 只显示错误行 |
六、第五步:配置自动重试模式
6.1 重试提示词模板
修复这些失败的测试。最多尝试3次。
每次尝试之后:
1. 跑测试
2. 如果通过,完成
3. 如果失败,读错误信息,换一种思路
4. 如果3次都失败,说明你尝试了什么、还有什么没修好
不要尝试同一个修法两次。
6.2 配合Stop钩子的闭环流程
┌─────────────────────────────────────────────────────────────┐
│ 自动重试闭环 │
├─────────────────────────────────────────────────────────────┤
│ │
│ Claude写代码 │
│ ↓ │
│ PostToolUse钩子:自动格式化 + 类型检查 │
│ ↓ │
│ Claude说"完成" │
│ ↓ │
│ Stop钩子:运行测试 │
│ ↓ │
│ ┌─────────────────────┐ │
│ │ 测试通过? │ │
│ └─────────┬───────────┘ │
│ Yes │ No │
│ ↓ ↓ │
│ 完成 重试(最多3次) │
│ ↓ │
│ 重新分析错误 │
│ ↓ │
│ 重新写代码 │
│ ↓ │
│ 回到PostToolUse钩子 │
│ │
└─────────────────────────────────────────────────────────────┘
七、第六步:配置跨会话记忆
7.1 手动添加记忆
bash
# 查看已记住的内容
/memory
# 添加项目配置规则
/memory add "这个项目用 pnpm,不是 npm"
/memory add "auth 模块在5月15日重构了,新模式在 src/lib/auth/v2/"
/memory add "数据库连接字符串在 secrets.json 中"
7.2 开启Dreaming功能
bash
# 在Claude中开启
/dreaming enable
# 检查状态
/dreaming status
7.3 三层记忆组合
| 层级 | 内容 | 更新方式 |
|---|---|---|
| CLAUDE.md | 项目级规则 | 手动添加 |
| /memory | 会话级学习 | 手动/自动 |
| Dreaming | 后台分析 | 自动 |
八、完整配置文件
8.1 最终config.json
json
{
"permissions": {
"allow": [
"Read", "Glob", "Grep", "LS", "Edit", "MultiEdit",
"Write(src/**)", "Write(tests/**)",
"Bash(npm test *)", "Bash(npx tsc *)", "Bash(npx prettier *)",
"Bash(npx eslint *)", "Bash(git add *)", "Bash(git commit *)"
],
"deny": [
"Read(**/.env*)", "Write(**/.env*)",
"Bash(rm -rf *)", "Bash(git push *)"
],
"defaultMode": "acceptEdits"
},
"hooks": {
"PostToolUse": [
{
"matcher": "Write(*.ts)",
"hooks": [
{ "type": "command", "command": "npx prettier --write $file" },
{ "type": "command", "command": "npx tsc --noEmit 2>&1 | head -20" }
]
},
{
"matcher": "Write(*.tsx)",
"hooks": [
{ "type": "command", "command": "npx prettier --write $file" },
{ "type": "command", "command": "npx eslint --fix $file" }
]
}
],
"PreToolUse": [
{
"matcher": "Bash(cat *log*)",
"hooks": [
{ "type": "command", "command": "grep -n 'ERROR\\|WARN' $file | head -50" }
]
}
],
"Stop": [
{
"hooks": [
{ "type": "command", "command": "npm test 2>&1 | tail -10; echo \"Exit: $?\"" }
]
}
]
}
}
8.2 目录结构
your-project/
├── .claude/
│ ├── config.json # 钩子配置
│ └── CLAUDE.md # 规则文件
├── src/
│ └── ...
├── tests/
│ └── ...
├── package.json
└── tsconfig.json
九、验证配置效果
9.1 测试步骤
bash
# 1. 启动Claude Code
claude
# 2. 创建测试文件
touch src/test.ts
# 3. 让Claude写一段有错误的代码
# 在Claude中输入:写一个简单的TypeScript函数
# 4. 观察PostToolUse钩子自动格式化
# 5. 让Claude完成任务
# 在Claude中输入:完成
# 6. 观察Stop钩子自动运行测试
9.2 预期效果
| 阶段 | 预期行为 |
|---|---|
| 写完.ts文件 | 自动执行prettier + tsc检查 |
| 写完.tsx文件 | 自动执行prettier + eslint修复 |
| 说"完成" | 自动运行npm test |
| 测试失败 | 自动继续修复,最多重试3次 |
十、企业级集成
10.1 API聚合平台接入
对于企业级应用,可以通过API聚合平台(如weelinking等)统一管理Claude的API调用:
python
from openai import OpenAI
client = OpenAI(
base_url="https://api.weelinking.com/v1",
api_key="YOUR_API_KEY"
)
# 批量配置企业级规则
response = client.chat.completions.create(
model="claude-3-opus",
messages=[
{"role": "user", "content": "根据以下企业规范生成CLAUDE.md规则:\n- 使用微服务架构\n- 遵循企业编码标准\n- 使用指定的技术栈"}
]
)
10.2 团队共享配置
bash
# 创建团队配置仓库
mkdir team-claude-config
cd team-claude-config
# 创建配置文件
cp your-project/.claude/config.json .
cp your-project/.claude/CLAUDE.md .
# 初始化Git
git init
git add .
git commit -m "Initial team config"
git remote add origin https://github.com/your-team/claude-config.git
git push origin main
# 团队成员安装
git clone https://github.com/your-team/claude-config.git ~/.claude/team-config
cp ~/.claude/team-config/* ~/.claude/
十一、配置前后对比
11.1 配置前
• Claude写代码,你跑测试,4个失败
• 你逐一解释每个失败
• Claude修了3个,顺带又引入1个新bug
• 你再次解释
• 每个功能来回45分钟
• 同样的错误明天还会出现
11.2 配置后
• Claude写代码,钩子自动格式化并检查每个文件
• Claude说完成,测试自动跑
• 测试失败,Claude读错误继续修
• CLAUDE.md阻止昨天的错误重演
• 记忆系统阻止上周的错误重演
• 每个功能10分钟,大部分时间不需要盯着
十二、常见问题排查
12.1 钩子不生效
bash
# 检查配置文件格式
cat .claude/config.json | jq .
# 检查Claude版本
claude --version
# 检查命令是否存在
which prettier tsc eslint
12.2 无限循环
原因 :Stop钩子没有检查stop_hook_active
解决方案:
json
{
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "if [ \"$stop_hook_active\" = \"true\" ]; then exit 0; fi; npm test"
}
]
}
]
}
12.3 权限问题
bash
# 检查权限配置
cat .claude/config.json | jq '.permissions'
# 确保需要的命令在allow列表中
十三、总结
通过这6步配置,你可以打造一个全自动的开发流程:
| 步骤 | 配置项 | 作用 |
|---|---|---|
| 1 | CLAUDE.md | 项目级规则约束 |
| 2 | PostToolUse | 实时格式化和类型检查 |
| 3 | Stop | 质量门禁,自动测试 |
| 4 | PreToolUse | 输入过滤,安全拦截 |
| 5 | 自动重试 | 最多3次自动修复 |
| 6 | 跨会话记忆 | 防止重复犯错 |
这套配置的核心价值在于将开发者从重复的错误解释中解放出来,让AI真正学会从错误中学习。
#ClaudeCode #自动修复 #开发效率 #AI编程
📖 推荐阅读
如果这篇对你有帮助,以下文章你也会喜欢: