返回列表

博客

使用 Tactic Remote 自动化代码审查

搭建一个 AI 驱动的代码审查工作流,在代码进入团队的 Pull Request 队列之前,提前发现 Bug、安全问题和质量缺陷。

发布于 标签: 上手教程 / code-review / workflow / quality
博客

继续查看该栏目下的产品与工程更新。

浏览此分类

代码审查对软件质量至关重要,但也非常耗时。据估计,开发者每周花费 6-8 小时审查同事的代码——这些时间本可以用来开发功能。AI 驱动的代码审查并不取代人工审查者,而是处理机械性的工作(发现 Bug、风格违规、缺少错误处理),让人工审查者可以专注于架构、设计和业务逻辑。

本教程将向你展示如何使用 Tactic Remote 搭建一个 AI 驱动的代码审查工作流。

审查工作流

以下是我们要搭建的工作流:

  1. 开发者在功能分支上完成工作。
  2. 在创建 Pull Request 之前,他们从手机上启动一个 Claude Code 审查会话。
  3. Claude Code 分析与主分支的差异,生成一份全面的审查报告。
  4. 开发者在手机上处理审查发现(批准修复或记录例外)。
  5. 清理后的代码被推送,并创建一个包含 AI 审查摘要的 PR。

这可以提前捕获 60-80% 在人工审查中才会发现的问题,大幅缩短审查周期。

步骤 1:准备审查提示词

在项目根目录创建一个名为 .claude-review-prompt.md 的文件。它作为 AI 审查的指令集:

# Code Review Instructions
 
Review all changes on the current branch compared to the `main` branch.
 
## What to Check
 
### Critical Issues (must fix before merge)
 
- **Logic errors**: Code that doesn't do what it claims
- **Security vulnerabilities**: SQL injection, XSS, auth bypasses, credential exposure
- **Data loss risks**: Missing transactions, incorrect cascade deletes, race conditions
- **Breaking changes**: API contract violations, backwards-incompatible modifications
 
### Warnings (should fix, but not blocking)
 
- **Missing error handling**: Uncaught exceptions, unhandled promise rejections
- **Performance concerns**: N+1 queries, unnecessary re-renders, missing memoization
- **Missing validation**: User input not validated before processing
- **Test coverage gaps**: New code paths without corresponding tests
 
### Suggestions (nice to have)
 
- **Readability improvements**: Unclear variable names, complex logic that could be simplified
- **Documentation gaps**: Public APIs without JSDoc/TSDoc comments
- **Potential refactoring**: Duplicated logic, overly long functions
 
## Output Format
 
For each finding, provide:
 
1. **Severity**: CRITICAL / WARNING / SUGGESTION
2. **File**: Exact file path and line numbers
3. **Description**: What the issue is, in one sentence
4. **Explanation**: Why it matters, with context
5. **Fix**: Concrete code suggestion to resolve the issue
 
At the end, provide:
 
- Summary: X critical, Y warnings, Z suggestions
- Overall assessment: Is this branch ready for human review?
 
## What NOT to Flag
 
- Style preferences covered by our linter (ESLint/Prettier handle this)
- Import ordering (automated)
- Whitespace or formatting (automated)
- Type annotations that TypeScript can infer

这个提示词是刻意写得具体的。模糊的审查指令只会产生模糊的审查结果。具体的指令产生可操作的发现。

步骤 2:从手机运行审查

当你准备审查某个分支时,打开 Tactic Remote 并启动一个指向项目目录的会话。发送:

Checkout branch feature/user-dashboard.
Follow the review instructions in .claude-review-prompt.md.

Claude Code 将会:

  1. 切换到该分支
  2. 计算与 main 分支的差异
  3. 按照你的审查标准分析每个变更文件
  4. 输出一份结构化的审查报告

这通常需要 3-8 分钟,取决于差异的大小。完成后你会收到通知。

步骤 3:审查发现的问题

审查输出会显示在你的终端视图中。以下是一个典型发现的样子:

🔴 CRITICAL — src/api/routes/users.ts:47-52

Description: SQL query constructed with string concatenation, vulnerable to injection.

Explanation: The search query parameter is interpolated directly into the SQL
string without parameterization. An attacker could craft a search input that
modifies the query logic, potentially extracting or modifying data.

Fix: Use parameterized query:
  // Before (vulnerable)
  const results = db.query(`SELECT * FROM users WHERE name LIKE '%${search}%'`)

  // After (safe)
  const results = db.query('SELECT * FROM users WHERE name LIKE ?', [`%${search}%`])

对于每个发现,你可以在手机上选择三种操作:

应用修复: 提示 Claude Code 实施建议的修复:

Apply the fix for the SQL injection issue in users.ts:47.

跳过: 如果你不同意该发现或认为是误报,记录下来稍后处理,继续下一个。

讨论: 如果你不确定,向 Claude Code 请求更多上下文:

Explain the SQL injection risk in more detail. Is this exploitable given
our authentication middleware that runs before this route?

步骤 4:自动应用修复

对于有大量发现的审查,你可以批量应用修复:

Apply fixes for all CRITICAL and WARNING findings from the review.
For each fix, run the relevant tests to verify the fix doesn't break anything.
Report which fixes were applied and which tests passed.

Claude Code 会系统地处理所有发现,逐一应用修复并运行测试。这又是一个你可以放下手机等待通知的任务。

重要提示: 在提交之前审查已应用的修复。AI 生成的修复通常是正确的,但"通常"对于生产代码来说还不够好。使用终端视图检查差异:

Show me the complete git diff of all changes made during this review.

步骤 5:生成 PR 摘要

修复应用并验证完成后,生成一份可用于 PR 的摘要:

Create a pull request description that includes:
1. What this branch does (based on the commit history)
2. Summary of the AI review findings and how they were addressed
3. Any remaining suggestions that were intentionally deferred
4. Test results

将此内容包含在你的 PR 描述中。它让人工审查者相信代码已经经过了预审,并告诉他们应该关注什么。

进阶:持续审查会话

对于长期运行的功能分支,定期运行审查而不是只在 PR 时才进行:

Review all commits since the last review marker.
After completing the review, create a git tag 'last-ai-review' at HEAD.

通过每天运行审查,你可以在问题引入时就尽早发现。这比在开发一周后才发现问题要容易修复得多。

与 CI/CD 集成

对于想要正式化 AI 审查的团队,你可以在分支推送时自动触发审查会话。工作流如下:

  1. CI webhook 触发 Tactic Remote Mac 伴侣应用。
  2. 伴侣应用在更新的分支上启动一个 Claude Code 会话。
  3. Claude Code 运行审查提示词。
  4. 结果作为评论发布到 Pull Request 上。

这需要在 Mac 端编写脚本(伴侣应用暴露了会话管理 API),详细内容请参阅我们的高级集成文档。

衡量效果

采用 AI 代码审查后,跟踪以下指标:

  • 审查耗时: 应减少 30-50%,因为人工审查者在机械性检查上花费的时间更少了。
  • 审查中发现的问题 vs. 生产环境发现的问题: 应该左移——更多问题在 AI 审查中被发现,更少的问题逃逸到生产环境。
  • 审查周期时间: 从 PR 创建到审批的时间应该缩短,因为 PR 在提交时已经过预审。
  • 开发者满意度: 审查者应报告更少的枯燥感,能更多地专注于有意义的架构反馈。

AI 代码审查的目的不是取代人类判断。而是确保人类判断用在最重要的问题上——架构、设计和业务逻辑——而不是用来发现拼写错误和缺失的空值检查。

从单个项目开始,根据结果优化你的审查提示词,校准方法后再推广到团队。在一个好的审查提示词上的投入,会在每次使用它的审查中带来回报。

立即体验 Tactic Remote

用手机远程控制编程 Agent

连接 Mac、Windows 或 Linux 电脑上的 Claude Code、Codex 等 Agent,在 iPhone 或 iPad 上查看进展并发送下一条要求。