AAAEC Resources返回官网
AAAEC Resources

PR 自动 review

用 Claude Code + Cursor + Devin 实现 CI 触发的自动 review + 自动修复,7×24 守住代码质量。

本文用 1 个 PR 自动 review 案例讲 4 件事:

  • 适用对象 5-50 人软件开发团队,把 AI agent 嵌入 PR 生命周期
  • 核心痛点 review 排队 / CI 失败定位 / 跨 repo 迁移 / on-call 半夜 / 新人 onboarding 慢
  • Agent 组合 Claude Code + Cursor + Devin + TRAE SOLO / WorkBuddy — 主力 + IDE + 跨 repo + 国内
  • 关键纪律 agent 不能直接 merge / --allowedTools 显式限制 / 跨 repo 不要漏 config / Hook 配错要 dry-run

1. 这是什么场景 + 用户 + 挑战

场景:5–50 人的软件开发团队,把 AI agent 嵌入到 PR 生命周期里,让 agent 自动 review、自动修简单问题、自动响应 CI 失败、辅助跨 repo 迁移和 on-call。

典型痛点(基于 Ken 用户反馈 + 模块主页数据):

  • review 排队 —— 5–50 人团队里 PR review 是协作的"卡脖子"环节,senior 一周可能要看 20+ 个 PR,其中 60% 都是 lint / 命名 / 简单 bug,senior 价值被严重稀释。
  • CI 失败后修复成本高 —— agent 能写代码,但 CI 报错往往藏在 build log 第 200 行,人工定位 + 复现 + 重跑成本高。
  • 跨 repo 统一迁移 —— 库升级(dep upgrade / API breaking change)涉及 10+ repo,人工一个一个改不现实。
  • 半夜/周末紧急响应 —— 值班人手有限,on-call alert 经常深夜吵醒,PagerDuty → 人工 → 修 PR 链路很长。
  • 新人 onboarding 慢 —— 新人不知道团队代码规范,前 30 个 PR 可能 20 个被 review 打回。

为什么 agent 是合理答案:agent 在"中等风险 + 高度重复 + 需要上下文检索"的场景里 ROI 最显著。Claude Code GitHub Action 在 PR 创建时自动 review + 留 comment 已成行业默认做法(Anthropic 官方 claude-code-action);Devin 的 on-call incident 自动响应是企业级杀手场景(Sentry/PagerDuty → Devin session 自动定位 + 修 + 开 PR)。来源:Claude Code GitHub Actions 官方文档 https://code.claude.com/docs/en/github-actions ✅;Devin Webhook 自定义 ticket 官方 https://docs.devin.ai/use-cases/gallery/api-webhook-custom-ticketing ✅。


2. 完整流程

2.1 PR 提交自动 review

开发者 push 代码 → 触发 GitHub Action(anthropics/claude-code-action)→ agent 读取 diff + 走 4 层 verification gate(Plan → Implement → Verify → Adversarial subagent 复审,来源:Claude Code 官方 best practices https://www.anthropic.com/engineering/claude-code-best-practices ✅) → 在 PR 上留 inline comment 列出问题。Step hook 配 exit 2 强制 agent 把 typecheck / unit test / lint 串成确定性门禁,任一不过强制 agent 修。

关键能力:12 个 Hook 事件(PreToolUse / PostToolUse / Stop / SubagentStart 等,来源:Claude Code 官方 https://code.claude.com/docs/en/hooks ✅)+ Plan mode + Extended thinking budget(/effort high)+ AGENTS.md / CLAUDE.md 项目规则自动加载。

2.2 自动修复简单问题

agent review 发现 代码规范问题 / 命名不一致 / 缺少 test / 明显 bug → 自动 fork branch → 应用 fix patch(Apply PatchEdit tool) → 跑 pre-commit hook 验证 → 自动 push 到 PR branch 并 comment "已自动修复 lint / 命名问题,请 reviewer 重点看 X 文件逻辑改动"。HITL 兜底:AcceptEdits mode 自动批准 Edit / Write,但 Bash 命令仍逐条 prompt(来源:Claude Code Permission Modes 官方 https://code.claude.com/docs/en/permission-modes ✅)。

2.3 CI 失败自动定位 + 修复

CI 失败 → PagerDuty / GitHub Action webhook → 触发 agent session(Devin 的 app.devin.ai/settings/webhooks 或 Claude Code anthropics/claude-code-actionfailure event 下启动)→ agent 拉 CI log → 用 grep / Bash 定位失败行 → reproduce → fix → push 新 commit → 在 PR 上 comment 详细说明。关键工具:Devin Interactive Browser mode 处理登录态 CI dashboard(来源:Devin 官方 https://docs.devin.ai/ ✅)+ BashOutput 工具读长 log。

2.4 跨 repo 统一迁移

库升级 / API breaking change 涉及 N 个 repo → Devin Enterprise MultiDevin 一个 manager Devin 拆 task → N 个 worker Devin 各负责一个 repo,通信走 GitHub PR / issue(来源:Devin Enterprise 官方 https://www.devin.ai/enterprise ✅)。或用 Claude Code Fan-out 模式:for repo in repos; do (claude -p "Migrate $repo to v2 API" --allowedTools "Read,Edit,Bash(npm install)" &) done(来源:Claude Code headless 官方 https://code.claude.com/docs/en/headless ✅)。Antigravity /teamwork preview 6 角色并发(Orchestrator / Explorer / Implementer / Reviewer / Stress Tester / Auditor)适合"单一 prompt 构建大型迁移",来源:Antigravity 官方 https://blog.google/products/gemini/gemini-3/ ✅。

2.5 值班 / 半夜紧急响应

PagerDuty alert → Devin webhook 触发 session(7 大集成覆盖 Linear / Jira / Slack / Datadog / Sentry / PagerDuty / GitHub,来源:Devin 官方 https://docs.devin.ai/use-cases/gallery/api-webhook-custom-ticketing ✅)→ agent 自动 reproduce → 修 → 开 PR → 在 Slack 群里通知 reviewer。Channel 协议(Claude Code 4 平台:Telegram / Discord / iMessage / Webhook,来源:Claude Code channels 官方 https://code.claude.com/docs/en/channels ✅)让 on-call 在手机上直接接管。


3. 需要的核心能力(链 modules)

模块子能力用途关键官方 doc
模块 1:工作模式Plan(代码改动规划)+ Take over(关键变更)大型重构先 plan 再执行,合并前人工 take overhttps://code.claude.com/docs/en/permission-modes
模块 2:触发与自动化Hook(PreToolUse 拦截危险改动)+ Schedule(每日安全扫描)+ Webhook(GitHub PR 触发)+ Channel(Slack 通知)12 事件 Hook + GitHub Action + Slack 通知https://code.claude.com/docs/en/hooks ✅ · https://code.claude.com/docs/en/github-actions
模块 3:记忆与上下文Project memory(AGENTS.md / .claude/rules)+ KB(代码风格指南)团队规范自动注入每个 PR reviewhttps://code.claude.com/docs/en/memory ✅(CLAUDE.md / AGENTS.md)
模块 4:工具与协议MCP(GitHub / GitLab / Slack)+ Code Interpreter(运行测试)+ Browser(看 PR 评论)5 大 MCP 能力 + Bash tool + Chrome MCP(看 PR)https://code.claude.com/docs/en/mcp ✅ · https://modelcontextprotocol.io/
模块 5:执行与沙箱Permission model(Default + AcceptEdits + Auto)+ Sandbox(本地 Seatbelt / 云 VM)Default 起步,长跑切 Auto,Bypass 仅 sandboxhttps://code.claude.com/docs/en/sandboxing
模块 6:推理与思考Reflection(自检代码质量)+ TIR(调工具查类型 / 运行 test)+ 4 层 verification gatePlan-Execute + Stop hook + Adversarial subagenthttps://www.anthropic.com/engineering/claude-code-best-practices
模块 8:协作与多 AgentHITL(reviewer 关键决策)+ Subagent(并行 review 多个 PR)+ Manager-Worker(Devin MultiDevin)Plan 审批 + PR review 不阻塞主对话 + 多 repo 并行迁移https://code.claude.com/docs/en/sub-agents ✅ · https://www.devin.ai/enterprise

4. 推荐 Agent 组合

Agent角色优势适用场景官方 doc
Claude Code主力12 事件 Hook + AGENTS.md + Routines + 4 平台 Channel + GitHub Code Review AppPR 自动 review / CI 修复 / on-call 通知https://code.claude.com/
Cursor 2主力(IDE 内 review)Composer Plan + .cursorrules + 最多 8 agent 并行 git worktree + Bugbot PR review本地 IDE 写代码时实时 reviewhttps://cursor.com/blog/2-0 ✅ · https://cursor.com/blog/agent-best-practices
Devin辅助(跨 repo 迁移 + on-call)MultiDevin 多周多 repo + Interactive Browser + 7 大集成(Linear/Jira/Slack/Datadog/Sentry/PagerDuty/GitHub)+ Scheduled dependency updates跨 repo 库升级 + 半夜 on-call 自动响应https://docs.devin.ai/ ✅ · https://www.devin.ai/enterprise
TRAE SOLO辅助(国内团队)PlanAgent 强制人工 review + 4 角色子 Agent(架构师/开发/测试/运维)+ 国内云生态国内团队 0→1 项目搭建 + 强制 HITL 场景https://www.trae.ai/docs/solo-mode
WorkBuddy国内版对应5 平台 Claw(企业微信/微信/QQ/飞书/钉钉)+ SkillHub 2.2 万 + 7×24 数字员工(企业版)国内 IM 通知 + 企业微信集成场景https://www.codebuddy.cn/work/
  1. Claude Code 做主力:Stop hook exit 2 强制 typecheck/lint/test 通过才能 merge,这是 PR review 自动化的核心闭环(来源:Claude Code hooks 官方 ✅)。
  2. Cursor 2 补 IDE 内实时 review:Claude Code 是 CLI 形态,Cursor 2 在 IDE 写代码时 Composer Plan + 最多 8 个 sub-agent 并行 git worktree 实时检查,适合写代码当下发现问题(来源:Cursor 2 官方 ✅)。
  3. Devin 补跨 repo + on-call:Claude Code 在单 repo 强,但跨 10+ repo 库升级 + 半夜 PagerDuty 响应需要 Devin 的 MultiDevin + 7 大集成(来源:Devin Enterprise 官方 ✅)。
  4. TRAE SOLO / WorkBuddy 补国内生态:Slack / Discord 在国内不可用,WorkBuddy 5 平台 Claw 是国内 IM 集成最成熟的(企业微信/微信/QQ/飞书/钉钉);TRAE SOLO 的 PlanAgent 强制人工 review 是国内团队的 HITL 偏好。
  5. 不选 Sierra / Agentforce:Sierra 无个人版,纯客服场景;Agentforce 是 Salesforce 生态客服场景,不直接适合 PR review。

5. 实战 tips

  • Tip 1:先在 .github/workflows/ 加 Claude Code action 自动 review:anthropics/claude-code-action 官方 market action,PR 开 / push / label 触发,--allowedTools 白名单限制到 Read,Grep,Glob,跑完留 inline comment(来源:Claude Code GitHub Actions 官方 ✅)。
  • Tip 2:危险操作(git push --force / rm -rf / drop table)用 PreToolUse hook 拦截:.claude/settings.json{"PreToolUse": [{"matcher": "Bash", "hooks": [{"type": "command", "command": "echo 'Blocked: dangerous op' && exit 2"}]}]},exit 2 强制 Claude 改方案(来源:Claude Code hooks-guide ✅)。
  • Tip 3:AGENTS.md / CLAUDE.md 沉淀项目约定:把团队的代码规范 / commit message 模板 / review checklist / 命名约定写进 CLAUDE.md,新 PR 自动 follow,新人 onboarding 提速 2-3x(来源:Claude Code Memory 官方 ✅)。
  • Tip 4:跨 repo 迁移用 Manager-Worker 拓扑(Devin MultiDevin):一个 manager Devin 拆 task → N 个 worker Devin 各领一个 repo,通过 GitHub PR / issue 通信;或用 Claude Code Fan-out 模式 for repo in repos; do (claude -p "Migrate $repo" --allowedTools "Read,Edit,Bash(npm install)" &) done(来源:Claude Code headless ✅ + Devin Enterprise ✅)。
  • Tip 5:简单 review(代码规范)完全自动,人工 review 留给架构决策:Claude Code AcceptEdits mode 自动批准 Edit / Write,Bash 仍 prompt;Agentforce / Devin 默认 autonomous,人在关键 Plan 节点审批(来源:Claude Code Permission Modes ✅)。
  • Tip 6(补充):Channel 协议 + Slack / 飞书通知:Claude Code 4 平台 Channel(Telegram/Discord/iMessage/Webhook)+ WorkBuddy 5 平台 Claw(企业微信/微信/QQ/飞书/钉钉),PR review 完成 / CI 修复完成自动 push 通知 reviewer(来源:Claude Code channels 官方 ✅ + WorkBuddy codebuddy.cn ✅)。
  • Tip 7(补充):Routines 电脑关着也跑定时安全扫描:/schedule 配 cron 表达式每日凌晨跑 npm audit / pip check / secret scan,电脑关着 Anthropic 托管基础设施也在跑(来源:Claude Code Routines 官方 ✅)。

6. 常见坑

  • 坑 3:agent 的代码建议必须 spot check,不能全信:Anthropic 官方明示"reviewer 是独立 subagent 在 fresh context"防 self-bias,但 agent 仍可能 hallucinate API 名字或库版本,人工 review 仍要做(来源:Anthropic best practices ✅)。
  • 坑 4:跨 repo 迁移容易遗漏 config 文件:agent 默认只改 .ts / .js / .py,但 package.json / pyproject.toml / tsconfig.json / Dockerfile / CI workflow 经常被遗漏,prompt 显式列出"包括 config 和 CI 文件"(来源:Devin 跨 repo 实战)。
  • 坑 5:Hook 配置错误会让 agent 完全失效,先用 dry-run:Claude Code PreToolUse hook 配错会 exit 2 拦截所有 Bash 命令,agent 直接卡死;先用 claude --debug 看 hook 是否触发,再启用(来源:Claude Code hooks-guide ✅)。
  • 坑 6(补充):Auto mode 连续 3 次 block 自动 fallback:Claude Code Auto mode 连续 3 次或累计 20 次 block 会自动降级 default mode,用户可能误以为 agent 在 Auto 跑实际已经 fallback,要监听 permission_decisions 字段(来源:Anthropic Auto mode 工程 blog ✅)。
  • 坑 7(补充):Hook 配置 CRLF 导致解析失败:Windows 上 .claude/settings.json 用 CRLF 可能 hook 解析失败;.gitattributes 强制 *.md text eol=lf(来源:Claude Code 官方 + 用户反馈 ⚠️)。
  • 坑 8(补充):Take over 状态不持久:Devin 明确"Take over 状态不在内部 state 持久",下次 step 不一定记得;敏感操作如 login / payment 必须 Take over,但不要依赖 Take over 状态跨步骤传递(来源:Devin 官方 ✅)。

7. 延伸阅读

  • 模块 1:工作模式 / 交互模式 → /docs/ai-agent/01-work-mode(Plan mode / AcceptEdits / Auto mode / Take over / Step-in)
  • 模块 2:触发与自动化 → /docs/ai-agent/02-trigger(12 事件 Hook / GitHub Action / Webhook / Channel / Routines)
  • 模块 3:记忆与上下文 → /docs/ai-agent/03-memory(AGENTS.md / CLAUDE.md / Memory)
  • 模块 4:工具与协议 → /docs/ai-agent/04-tool(MCP 完整支持 + Channel 协议 + Bash + Edit + Glob + Grep)
  • 模块 5:执行与沙箱 → /docs/ai-agent/05-sandbox(6 种 Permission mode + Seatbelt / bubblewrap / Anthropic VM)
  • 模块 6:推理与思考 → /docs/ai-agent/06-reasoning(4 层 verification gate + Plan-Execute + Reflection)
  • 模块 7:验证与监控 → /docs/ai-agent/07-verification(Audit log + Eval + Trace grading)
  • 模块 8:协作与多 Agent → /docs/ai-agent/08-collaboration(HITL + Subagent + Devin MultiDevin + Channel handoff)

关键官方文档


On this page