Claude Code 审查 skill:合并前用 code-review 检查 diff

Dmitriy Solodukha,Caprock 开发者 · 发布于

测试于 (Claude Code 和 Codex)

可用于任何智能体的 Claude Code 审查 skill:先检查 bug,再检查测试、安全性和范围。每个问题都附有 file:line 和修复方案,核实后才报告。

code-review skill 用于审查 Claude Code 的代码,也适用于 Codex、OpenCode 和 Cursor:它审查未提交的 diff,或比较分支与基准分支,优先报告正确性 bug,然后是缺少的测试、安全问题和范围蔓延。每项发现都附有文件和行号、问题原因及具体修复方法,并在写入报告前从代码中取证(追踪调用方、复现问题或运行测试)。除非你要求,否则不挑风格细节。

安装 code-review skill

每个智能体只需一条命令。skill 只有一个 SKILL.md;安装时不会运行任何内容。

将全部九个 skill 作为插件安装。在 Claude Code 中输入以下内容:

也可以只安装这个 skill,在终端中进入项目文件夹后运行:

添加 -g 即可为所有项目安装。

使用 code-review skill 的提示词

你的措辞与 skill 描述匹配时,智能体会选择它;直接指定 skill 名称更有把握。点击提示词即可复制。

code-review skill 检查什么

  1. 选择要审查的 diff:有未提交的改动就检查这些改动,否则将分支与基准分支比较(从 origin/HEAD 获取,否则用 main 或 master),并用一行说明采用了哪一种。
  2. 理解意图 来自你的请求、提交消息和项目的智能体指令;违反项目规则也算一个问题。
  3. 读取代码,不只看 diff:所在函数、调用它的代码,以及每个被修改名称的所有引用。
  4. 按顺序查找:正确性、测试、安全、范围蔓延。
  5. 核实每项发现,或将其标记为未核实,并说明如何确认。少量确实存在的问题胜过大量疑似问题。
  6. 添加 CI:先用 Caprock 的 ci_status,否则用 gh pr checks,再不行就显示“CI: not available”。

我们运行了什么,智能体做了什么

每个 skill 都于 2026年11月29日在为其专门创建的临时仓库中运行,并作为项目 skill 安装。测试使用一个小型 Python 仓库,将分支 feat/discount 与 master 比较:折扣码取最后两个字符解析后传入 os.system(shell 注入),无效折扣码会导致崩溃,一个无关的 average() 遇到空列表会除以零,这些都没有测试。

Claude Code (claude -p --model sonnet)

Review the changes on this branch before I merge them.

调用了 code-review skill(使用了项目 skill,而非 Claude Code 内置的同名 skill),报告了七个问题:shell 注入、未验证的折扣码、一位和三位数字的解析问题、每次调用都会输出一行日志、average([]) 崩溃、缺少测试,以及超出范围的 average()。每项都经过核实:五项通过复现(注入在临时文件夹中创建了一个文件),缺少测试和超出范围这两项通过阅读代码及其历史记录确认。结论:合并前修复,并附有 CI 和 Not checked 行。

Codex (0.163.0,codex exec)

Review the changes on this branch before I merge them.

读取 skill,按其格式给出了三个已验证的发现(注入、解析、测试)。它遗漏了 average() 和 CI 行;此次运行后,skill 已修改为强制包含 CI 行,未再运行 Codex。报告开头如下(链接显示为纯文本):

Reviewed: `feat/discount` vs `master`, 1 commit, 1 changed file.
Verdict: **fix before merge**
1. [security] cart.py:9: Discount codes can execute arbitrary shell commands.
   Why: `SAVE10; echo injected > marker; #10` passes percentage parsing and executes the injected command.
   Fix: Replace `os.system` with Python file I/O or logging.
   Verified: Reproduced in a temporary directory; the injected command created a marker file.

已测试的智能体

  • Claude Code:测试于 2026年11月29日,使用 claude -p --model sonnet,skill 作为项目 skill 安装在 .claude/skills/ 中。
  • Codex:于 2026年11月29日使用 Codex 0.163.0(codex exec)测试,skill 位于 .agents/skills/。
  • OpenCode 和 Cursor:未测试。此 skill 是以开放的 Agent Skills 格式编写的纯文本指令,但尚未在这两者上实际运行验证。

限制

  • Claude Code 内置了同名的 /code-review。测试中使用的是项目 skill;以插件方式安装时哪个优先,尚未测试。
  • 各运行一次后,Codex 报告了 3 个问题,Claude Code 报告了 7 个;Codex 漏掉了 average() 崩溃和范围问题。
  • diff 很大时,会优先审查风险最高的文件,其余文件列在 Not checked 下。
  • 没有测试通过 Caprock 的 ci_status 获取 CI 信息的部分:测试仓库没有远程仓库。

使用和不使用 Caprock 的情况

连接 Caprock MCP 服务器后,skill 调用 ci_status 获取最新的工作流运行记录及未关闭 pull request 的检查结果,并列出失败的作业。未连接时,如果 GitHub CLI 已登录,就使用 gh pr checks,否则写出“CI: not available”。Caprock 是一款免费开源应用,记录你电脑上的每个编程智能体会话。

获取 Caprock →

完整的 SKILL.md

这是 dspv/skills 中修订版本 dfce799 的文件原样。手动安装时,将它保存在项目中:Claude Code 使用 .claude/skills/code-review/SKILL.md,Codex 使用 .agents/skills/code-review/SKILL.md。

skills/code-review/SKILL.md
---
name: code-review
description: >
  Review the changes a coding agent made before they are committed or merged: the uncommitted
  diff, or the branch's diff against its base branch. Finds correctness bugs first, then missing
  or broken tests, security problems and scope creep. Every finding has file:line, why it is
  wrong and a concrete fix, and is checked against the code before it is reported. No style
  nitpicks unless asked. Use when the user says "review my changes", "review this diff",
  "review the branch before I merge", "code review", "check what the agent changed", or is
  looking for a claude code review skill that works in any agent.
license: Apache-2.0
---

# Code review

Review a diff the way a careful senior engineer would before approving it: find what will break,
prove it from the code, say how to fix it. Do not edit files unless the user asks for fixes.

## 1. Pick the diff

Use what the user named (a branch, a commit range, a file). Otherwise, in this order:

1. Uncommitted work: `git status --short`. If anything is staged, unstaged or untracked, review
   `git diff HEAD` plus the untracked files (read them whole; they are new code).
2. Otherwise the branch against its base. Find the base: the user's word, else
   `git symbolic-ref refs/remotes/origin/HEAD` (strip `refs/remotes/`), else `main` or `master`,
   whichever exists. Then review `git diff $(git merge-base <base> HEAD)...HEAD` and read
   `git log --oneline <base>..HEAD` for the stated intent.
3. If both are empty, say there is nothing to review and stop.

State the diff you reviewed in one line (for example `uncommitted, 4 files` or
`feat/x vs master, 3 commits, 6 files`).

## 2. Learn the intent

Before judging, know what the change is meant to do: the user's request, commit messages, a PR
description, a linked issue, the project's agent instructions (`CLAUDE.md`, `AGENTS.md` or the
like). Those instructions are rules too: a change that breaks one is a finding.

## 3. Read the code, not only the diff

For each changed hunk, open the surrounding function and its callers. A diff hides the line that
makes it wrong: the caller that passes `None`, the other branch of the `if`, the second place
that writes the same field. Use the project's search (`git grep`, `rg`) to find every use of a
changed name, signature, return value or config key.

## 4. Look for problems, in this order

1. **Correctness.** Logic errors, wrong conditions, off-by-one, unhandled `null`/empty/error
   cases, wrong types or units, race conditions, resource leaks, broken callers of a changed
   signature, behavior that no longer matches the intent, data loss or migrations that cannot
   be undone.
2. **Tests.** New behavior with no test, a test that cannot fail (asserts nothing, mocks the
   thing under test), a test changed to pass instead of the code being fixed, deleted or
   skipped tests. If the project has a fast test command, run it and report the result.
3. **Security.** Injection (SQL, shell, path, HTML), secrets or tokens in code or logs, missing
   authorization checks, unsafe deserialization, user input reaching the file system or a
   network call unchecked.
4. **Scope creep.** Changes the task did not ask for: unrelated refactors, renamed files,
   reformatting, new dependencies, debug prints, commented-out code, edits to generated or
   lock files. Name them so the user can drop them; they are findings only when they risk
   something or hide the real change.

Skip style, naming and formatting unless the user asked for them or a project rule requires it.

## 5. Verify every finding

Before a finding goes in the report, prove it:

- Re-read the exact lines and the code they depend on. Confirm the input that breaks it can
  actually reach it (trace the caller, the type, the validation upstream).
- Where it is cheap, show it: run the failing test, a one-line script, or the project's check.
- If you cannot prove it, either drop it or keep it marked **unverified** with what would
  confirm it. Never present a guess as a bug.

Drop a finding that the code already handles elsewhere. Fewer real findings beat many maybe ones.

## 6. CI results (optional)

If the branch has a pull request or CI runs, include the result:

- When the Caprock MCP tools are available (server `caprock`), call `ci_status` (optional
  argument `repo`: a directory inside the repository). It returns the newest run of each
  workflow on the default branch and the checks of the open pull request, with failing jobs
  named. Caprock (<https://caprock.dev>, free, open source, runs locally) provides that tool.
- Otherwise, if the GitHub CLI is installed and signed in, `gh pr checks` shows the checks of
  the current branch's pull request.
- If neither is available, write `CI: not available` in the report and move on.

A failing check related to the diff is a finding; read its log before blaming the diff.

## 7. Report

Use this format, most severe first. Nothing else before it except the one-line diff summary.

```
Reviewed: <what diff>
Verdict: <ready to merge | fix before merge | needs discussion>

1. [bug] path/to/file.py:42: <what is wrong, one sentence>
   Why: <the input or path that triggers it, and what happens>
   Fix: <the concrete change: code or exact steps>
   Verified: <how: test run, traced caller at file:line, reproduced> | unverified: <what would confirm>

2. [test] ...
3. [security] ...
4. [scope] ...

CI: <checks passed | failing: job names | not available>
Not checked: <anything you could not read or run>
```

Severity tags: `bug`, `test`, `security`, `scope`, and `rule` for a broken project instruction.
If there are no findings, say so in one line and give the verdict; do not invent findings to
fill the report.

## Stop conditions

- Stop after the report. Apply fixes only if the user asks; then re-run the tests you ran.
- If the diff is too large to read whole, say so, review the riskiest files first (logic over
  config, config over docs), and list the files you did not review under `Not checked`.

关于 code-review skill 的问题

有没有能用于其他智能体的 Claude Code 审查 skill?
code-review 就是一个例子:一份普通的 SKILL.md,Claude Code、Codex、OpenCode 和 Cursor 都能读取。它于 2026年11月29日在 Claude Code 和 Codex 0.163.0 上测试过。
这个代码审查 skill 与 Claude Code 内置的审查功能有何不同?
它在各个智能体中的工作方式相同,按固定格式报告,每项发现都先从代码中取证,再写入报告。Claude Code 内置的 /code-review 命令与它同名。测试中使用的是安装为项目 skill 的版本;通过插件调用时,使用 `/caprock-skills:code-review`(未测试这种调用方式)。
code-review skill 会修改我的代码吗?
不会。它给出报告后就停止。只有你要求时才修复,然后重新运行之前运行过的测试。
它会评论代码风格吗?
除非你要求,或项目规则有要求,否则不会。默认跳过风格、命名和格式问题。

来源

应用本身是英文界面。