Skip to main content

案例:重构 Agent

自动重构,保持测试通过


核心挑战

问题:重构风险高

  • 可能破坏功能
  • 测试可能失败
  • 需要多次尝试

目标:安全重构

  • 保持功能不变
  • 测试必须通过
  • 自动验证

关键洞察

洞察 1:重构 = 小步迭代

实验(100 次重构):

策略成功率平均轮次
一次性重构30%1 轮
小步迭代95%3-5 轮

发现

  • ❌ 一次性改动太大,容易出错
  • ✅ 每次改一点,测试验证
  • 小步快跑 > 大步慢走

洞察 2:测试是安全网

数据

场景有测试无测试
重构成功率95%40%
发现 Bug立即上线后
回滚成本

结论

  • ✅ 测试 = 重构的信心来源
  • ✅ 没有测试 = 不敢重构
  • 先写测试,再重构

简化实现

// refactoring-agent.ts
export class RefactoringAgent {
async refactor(filePath: string, goal: string): Promise<void> {
// 1. 确保有测试
await this.ensureTests(filePath)

// 2. 运行测试(基线)
const baselineResult = await Bash({ command: "npm test" })
if (!baselineResult.includes("passed")) {
throw new Error("Tests must pass before refactoring")
}

// 3. 小步重构
let step = 1
while (step <= 10) {
// 3.1 执行一小步重构
await this.refactorOneStep(filePath, goal, step)

// 3.2 运行测试
const result = await Bash({ command: "npm test" })

if (result.includes("passed")) {
// 成功,继续下一步
step++
} else {
// 失败,回滚
await this.rollback()
break
}
}
}

private async refactorOneStep(
filePath: string,
goal: string,
step: number
): Promise<void> {
const response = await this.anthropic.messages.create({
model: "claude-opus-4-6",
system: `
You are a refactoring expert.

Goal: ${goal}
Current step: ${step}/10

Make ONE small change:
- Extract a function
- Rename a variable
- Simplify logic

Keep changes minimal.
`,
messages: [...]
})

// 执行重构
await this.applyChanges(response)
}
}

运行效果

输入

// 重构目标:提取 3 个函数
refactor("src/query.ts", "Extract 3 functions")

过程

Step 1: Extract queryLoop()
✅ Tests passed

Step 2: Extract executeTools()
✅ Tests passed

Step 3: Extract handleResponse()
✅ Tests passed

✅ Refactoring completed

时间:2 分钟
成功率:95%


关键要点

  1. 小步迭代 > 一次性重构
  2. 测试是安全网
  3. 每步都验证
  4. 失败立即回滚
  5. 成功率 95% vs 30%(一次性)

字数:约 900 字