读见

Claude Code 团队如何看待验证 / claude.dev 博客

原题How the Claude Code team thinks about verification / claude.dev Blog

约 9 分钟约 2,193 字原文英文已翻译

推荐理由Claude Code 团队把端到端运行、性质测试、差分测试和独立审核串成可验证交付流程。检查由代码负责人维护、变更不能修改自己的裁判,这些具体机制帮助读者判断怎样扩展智能体并发,同时把人工注意力留给真正需要判断的失败。

读前速览

5 个要点

Claude Code 团队介绍了他们的验证框架,该框架通过端到端测试、属性测试和差分测试,借助基于证据的验证循环,让 AI 智能体能够大规模生成可靠代码。

  1. 三种核心测试构成了验证基础:端到端测试、属性测试和差分测试。

    端到端测试通过明确的通过或失败标准及详细的失败追踪,验证真实用户流程。属性测试借助 fast-check 等库生成边界情况,以暴露智能体的盲点。差分测试使用随机输入比较新实现与旧系统,在迁移过程中发现回归问题。

  2. 验证循环为智能体提供基于证据的验证能力,使其能够并行执行任务。

    通过让智能体依据评估标准验证输出,任务成功率最高可提升 10 个点。测试、基准测试和规范可以像软件一样低成本地生成并频繁运行,因此数十个智能体可以并行工作,而不必对每项改动都进行人工验证。

  3. 测试失败三轮后会升级为人工审查,代码所有者则负责编写自己的检查。

    该系统利用失败来判断何时需要人工介入。代码所有者会编写并完善检查,以标记他们认为需要注意的问题。改动不能修改自身的检查;验证规则则被写入 .claude/skills/verify 文件,并在提交前运行。

  4. 可靠性检查清单和提交门禁可确保系统地进行验证,无须依赖对过往事故的记忆。

    一份包含 39 项内容的可靠性检查清单会在出现特定差异模式时触发,例如「--resume 加载旧的转录记录」。提交门禁分为观察和强制两种模式:前者记录哪些内容会被拦截,后者阻止未经验证的提交。每次提交都会标注所采用的验证方法。

  5. 通过提取模式并重放 Bug 检测器,可将遗漏的 Bug 转化为规则。

    团队从人工代码审查评论和 PR 回滚中提取模式,识别至少出现在 2 个 PR 中的常见问题。随后,团队会针对已知事故重放 Bug 检测器;如果至少 2 个检测器找出该 Bug,就会将其转化为 REVIEW.md 中可检测的规则。如果找不到根本原因,则会删除不稳定的测试。

完整摘要

本文详细介绍了 Anthropic 的 Claude Code 团队如何构建验证体系,使 AI 智能体能够编写和审查约 90% 的代码库。团队强调三种核心测试:端到端测试通过具体的失败追踪验证真实用户流程;属性测试借助 fast-check 等库生成智能体容易遗漏的边界情况;差分测试则将新实现与旧系统进行比较。团队建立了验证循环,让智能体能够并行运行数十项测试;测试失败三轮后,会升级为人工审查。他们维护由代码差异触发的可靠性检查清单,确保检查本身无法被修改,并在提交门禁中使用观察和强制两种模式。该框架还包括由代码所有者编写的检查、基于先前上下文推进的审查轮次,以及从遗漏的 Bug 中系统提取规则以防止问题重现。

目录 · 14 节

查看原文

在 Claude Code 团队,我们绝大多数代码(约 90%)都是由 Claude 编写和审查的。在这样的规模下,如何让我们的智能体能够以证据验证自己的工作,就变得极为重要。On the Claude Code team, the vast majority of our code (~90%) is written and reviewed by Claude. At this scale, how we equip our agents to verify their work with evidence becomes extremely important.

验证循环是让你能够向 Claude 委派更多工作的基础。没有它们,你实际上一次只能运行一两个 Claude,因为所有事情都得由你亲自检查。有了它们,你就能并行运行几十个甚至几百个 Claude。Verification loops are the building block that lets you delegate more work to Claude. Without them, you can really only run one or two Claudes at a time, because you have to check everything yourself. With them, you can run dozens or hundreds in parallel.

这是因为,为智能体提供验证其输出的方法,是提升质量的有效途径。在围绕结构化文件生成的内部测试中,让独立评分器根据评分标准检查 Claude 的工作,与标准提示循环相比,任务成功率最高提升了 10 个百分点,且最难的问题提升幅度最大。That’s because giving agents a way to verify their output is an effective way to improve quality. In internal testing around structured file generation, having a separate grader check Claude’s work against a rubric improved task success by up to 10 points over a standard prompting loop, with the largest gains on the hardest problems.

过去验证成本高昂的内容(测试、基准测试、规范以及真实的端到端运行),如今都可以低成本地交由代理完成。和代码一样,代理可以快速生成测试、基准测试和规范。生成后,这些测试和基准测试就能像软件一样运行,可以频繁执行,而无需消耗太多额外的 token。What was once expensive to verify (tests, benchmarks, specs, real end-to-end runs) is now cheap to request from an agent. Like code, agents can quickly generate tests, benchmarks, and specs. And once generated, these tests and benchmarks run as software so they can be run frequently without using many additional tokens.

你还可以利用验证结果来提升智能体在该任务上的表现,从而形成闭环。例如,如果智能体反复未能通过一项检查日志中是否包含用户数据的测试,你可以创建一个钩子,或在 CLAUDE.md 中添加一条更明确的规则,以避免今后出现类似问题。这基本上可以防止问题再次发生。You can also close the verification loop by using the results to improve the agent’s performance on that task. For example, if an agent repeatedly fails a test that checks logs for user data, you can create a hook or a more explicit rule in your CLAUDE.md to avoid future issues. This (mostly) stops issues from happening again.

那么,如果验证对于高速产出高质量代码至关重要,你应该使用哪些类型的测试,又该如何在整个流水线中应用它们?本文将回答这些问题,并分享 Claude Code 团队采用的流程。So if verification is critical to producing quality code at a high velocity, what types of tests should you use and how should you apply them across your pipeline? This article answers those questions and also shares the process used by the Claude Code team.

并非所有测试都同等重要:值得投入的 3 种测试NOT ALL TESTS ARE CREATED EQUAL: 3 TESTS WORTH INVESTING IN

无论选择哪种测试类型,都有几种需要避免的失败模式。No matter which test type you choose, there are a few failure modes to avoid.

如果测试不稳定,你就会训练智能体重新运行测试,而不是修复失败的代码。如果测试很慢,智能体在每批中运行的测试就会更少,这会让定位问题变得困难。较重的测试最好在提交更改之前运行。If your tests are flaky you’ll train the agent to run them again rather than fixing the failing code. If your tests are slow, the agent will do fewer tests across batches which can make pinpointing issues difficult. Heavier tests are best deployed before you’ve committed the changes.

现在,以下是我认为最值得投入精力的三类测试。Now, here are the three types of tests I think are most worth investing in.

端到端测试E2E tests

如果必须专注于一种测试类型,请选择端到端测试。这类测试描述真实的用户流程,例如将商品加入购物车、结账并跳转到确认页面。If you have to focus on one test type, pick end-to-end. These tests describe real user flows like adding an item to a cart, checking out, and moving to a confirmation page.

端到端测试之所以如此有效,是因为它们很难蒙混过关——功能要么整体正常运行,要么就无法正常运行。它们还有明确的完成标准,并且能具体指出哪里出了问题。The reason E2E tests are so effective is that they are difficult to fool–either the whole feature works or it doesn't. They also have definitive done criteria and can concretely tell you what’s broken.

Unit test failure vs. end-to-end test failure. Left: checkout.test.ts shows only “FAIL checkout.test.ts / Error: Timeout after 30s”, captioned “Shooting blind. No step, no trace.” Right: checkout.spec.ts shows a step trace (Register, Add item to cart, Checkout failed, See confirmation page not reached), a screenshot of shop.example/checkout with “Payment form failed to load” and a disabled “Place order” button, and “What step failed: Checkout: ‘Place order’ stayed disabled because the payment form never loaded”, captioned “Screenshots, trace, and a description of what step failed.”
FIG AA unit test may show “Error: Timeout after 30 seconds,” but an end-to-end test can show a screenshot with a description of what’s broken.

虽然单元测试运行更快,也更稳定,但即使测试通过,功能仍有可能被破坏。这是因为智能体和测试都无法理解任务的全貌。While unit tests are faster and less flaky, it’s possible to pass them and still break the feature. This is because both the agent and the test don’t understand the full task.

对于端到端测试,让你的智能体能够操作并观察实际运行的系统会很有效。你可以使用技能,通过 CDP、模拟器或 API 测试工具等工具启动应用、操作应用并检查应用。With E2E tests, it is effective to give your agent hands and eyes on the real running system. You can use a skill to boot, drive, and inspect the app with tools like CDP, simulators, or API harnesses.

功能地图可以作为代理的指南和评分标准,因为它通常会从用户的角度列出各项功能,并描述操作路径、键盘快捷键、选择器和预期结果。The feature map can serve as a guide and grading rubric for agents as they typically list features from the user’s point of view with descriptions of paths, keyboard shortcuts, selectors, and expected results.

属性测试:不可违背的约束Property tests: constraints that can’t break

很多测试都基于你认为可能出错的地方。属性测试关注的是人们容易忽略的边界情况,而这正是智能体经常失败的地方。A lot of tests are based on what you thought might go wrong. Property tests look at the edge cases that people tend to miss, which is where agents often fail.

你首先写下一条你认为不可能失败的断言,然后使用测试库(例如 JS/TS 的 fast-check、Python 的 Hypothesis、Rust 的 proptest 等)生成数千个输入,试图证明它并非如此。You start by writing a statement you think can’t fail and then use a test library (fast-check for JS/TS, Hypothesis for Python, proptest for Rust, etc.) to generate thousands of inputs that try to prove otherwise.

几乎每个软件项目中都有许多属性,例如:There are many properties in virtually every software project like:

属性测试库是让智能体不偏离事实的好方法:它们会使用边界情况,而这些情况并非由编写原始代码的智能体以同样的偏见构造出来。当规范与生产环境中的实际行为出现偏差、不再是可靠的验证依据时,属性测试库对重写也尤其有帮助。Property libraries are a great way to keep agents honest by using edge cases that aren’t created with the same bias as the agent that created the original code. They are also particularly helpful for rewrites when the spec has drifted from production behavior and is no longer a reliable source for verification.

差分测试:旧系统是判定基准Differential tests: the old system is the oracle

对于预期功能保持不变的大规模迁移场景,差分测试尤其有效。在这类测试中,使用相同的输入分别运行旧系统和新系统,然后检查两者的结果是否不同。如果结果不同,就说明存在需要修复的缺陷或破坏性变更。Differential tests are particularly effective for large-scale migration use cases where functionality is expected to remain the same. For these tests, you run both the old system and the new system with the same inputs and see if you get different results. If you do, there is a bug or breaking change that needs to be fixed.

差分测试的关键在于使用随机生成的输入,或抽取一部分生产流量作为样本。你还需要对空白字符等无关因素进行规范化处理,并维护一份已批准变更的清单,以便在新环境应与旧环境有所不同时使用。The key for differential tests is to use randomly generated inputs or a sample of production traffic. You’ll also want to normalize irrelevant factors like whitespace and keep a list of approved changes for situations where the new environment should differ from the old.

我们此前介绍过 Anthropic 的代码审查流程,但本文旨在深入探讨我们如何验证自己的工作。We have previously written about how code review works at Anthropic, but this article aims to go deeper on how we verify our work.

验证意味着运行这项更改Verification means running the change

端到端运行应用比测试更重要。你不会在模拟器里试过之前就发布 iOS 变更,Claude 也是如此。Running the app end to end matters more than tests. You wouldn't ship an iOS change without trying it in a simulator, and the same goes for Claude.

Claude 可以像人类或 QA 工程师一样测试整个应用。对于 Web 更改,可以让它使用 Claude Chrome 扩展;对于移动端,可以使用 iOS 或 Android MCP;对于桌面端,可以使用计算机操作功能;对于后端工作,则可以让它在本地运行服务。Claude can test the whole app the way a human or QA engineer would. Ask it to use the Claude Chrome extension for web changes, an iOS or Android MCP for mobile, computer use for desktop, or to run the service locally for backend work.

为了明确如何启动模拟器或运行你的应用,请创建一个名为 verify 的技能。将它放在代码仓库中尽可能靠近相关代码的 .claude/skills/ 文件夹内,并根据需要创建多个。Claude 会知道在提交更改前使用 verify 技能。To codify how to start the simulator or run your app, create a skill called verify. Put it in a .claude/skills/ folder in your repo, as close to the relevant code as you can, and add as many as you need. Claude knows to use the verify skill before committing changes.

然后投入精力,让你的验证技能变得快速可靠。让 Claude 反复迭代并对其进行压力测试。它是你的冒烟测试框架,所以你要确保它坚如磐石。Then invest in making your verify skill fast and reliable. Ask Claude to iterate on it and stress test it. It's your smoke test harness, so you want it rock solid.

当你为 Claude 提供一种端到端冒烟测试其更改的方法时,得到的结果会好得多。你会看到它像人一样不断迭代,并更快地发现真正的错误。这就是我们在 Anthropic 测试几乎所有更改的方式。When you give Claude a way to smoke test its changes end to end, you get much better results. You'll see it iterate and catch real mistakes faster, the way a human would. This is how we test nearly all changes at Anthropic.

失败的测试需要人工处理Failing tests require human attention

没人能阅读每一项更改,因此我们会把失败作为判断哪些工作需要人工介入的一种依据。Claude 会运行检查,并尝试修复所有未通过的项目。如果经过三轮后仍有检查未通过,Claude 就会停止并请人工介入。Nobody can read every change, so we use failures as one way to decide what needs a person. Claude runs the checks and tries to fix anything that fails. If a check is still failing after three rounds, Claude stops and brings in a human.

代码所有者编写自己的检查项Code owners write their own checks

代码的所有者编写相应的检查,并不断完善,直到这些检查能够标记出所有者会标记的问题。当某项检查需要审查者介入时,应说明原因,这样他们就不必从差异中自行判断。The people who own the code write the checks for it, and refine them until they flag what the owner would flag. When a check pulls in a reviewer, it should say why, so they don't have to work it out from the diff.

可靠性检查会在差异发生时触发Reliability checks trigger on the diff

我们维护着一份包含 39 项的可靠性检查清单,每项都包括一个可从 diff 中识别的触发条件、一项检查,以及相关的事故或 PR。其中一项是“--resume 会加载旧的对话记录”。当会话存储方式或消息类型发生变化时,我们会确认新增字段能否安全加载,因为旧的对话记录中不会包含该字段。We maintain a reliability checklist with 39 items, each with a trigger that can be read from a diff, a check, and the incidents or PRs behind it. One is “--resume loads old transcripts.” When session storage or message types change, we ask whether a new field loads safely, since older transcripts won’t have it.

Claude 会读取差异,并只检查触发条件匹配的条目(通常为 3 到 10 项),这样就没人需要记住过去的服务中断。Claude reads the diff and checks only the items whose trigger matches–usually 3 to 10–so nobody has to memorize past outages.

一位审稿人,多轮审稿One reviewer, multiple rounds

我们不会通过增加审查人员来提升审查质量。在主要审查者之外再添加一位成本更高的审查者,往往不如调整主要审查者的做法。相反,我们会编写一份 REVIEW.md,说明审查者在审查这段代码时应重点关注什么。We don't try to improve reviews by stacking on more reviewers. Adding a second, more expensive, reviewer on top of the primary one is often a worse approach than tuning the primary. Instead, we have a REVIEW.md that says what the reviewer should pay attention to for this code.

我们还认为,审查和验证的每一轮都应建立在前几轮的基础上。我们会让每一轮参考前几轮的上下文和过去的失败案例,而不是每次都从头重新检查所有内容。这样成本更低,也能发现更多问题。We also think review and verification rounds should build on each other. We point each pass at context from earlier rounds and past failures rather than re-checking everything from scratch. It’s cheaper and finds more.

更改不能修改自身的检查项。Changes can't modify their own checks

如果某项更改能够修改用于评判它的检查,那么这个检查就失效了。Code Review 会从基准分支或默认分支读取 REVIEW.md 文件,而不会读取拉取请求中的更改。Claude Code 团队确保用于把关更改的规则位于一个无法被该更改修改的分支上。If a change can modify the check that judges it, the check is broken. Code Review reads the REVIEW.md file from the base or default branch, never from the pull request's changes. The Claude Code team ensures the rules that gate changes are on a branch that the change can't modify.

Claude Code 团队也会为提交预留审核时间。如果有人在审核期间继续推送提交,那么审核结果就不再有参考价值。对过时提交的审核即使通过,也毫无意义。The Claude Code team also reserves reviews for commits. If someone's pushing while the review is happening, then the review result is no longer useful. A passing review on an out-of-date commit means nothing.

团队还会先运行成本低的检查。如果智能体在本地测试前就提交了更改,那么本可以自行发现的 bug,就会占用大家的评审和 CI 时间。在我们团队,Claude 会在创建拉取请求前运行测试、构建和代码检查。你可以通过设置一个在 Claude 完成后运行测试的钩子来强制执行这一流程,也可以将测试套件通过设为 /goal。The team also runs cheap checks first. If an agent pushes before testing locally, it has spent shared review and CI time when it could have caught the bug itself. On our team, Claude runs the tests, build, and linter before opening a pull request. You can enforce this with a hook that runs the tests when Claude finishes, or a /goal of a passing test suite.

以观察模式启动一个新的 gateStart a new gate in observe mode

在我们自己的代码仓库中,我们会为每次提交标注其验证方式。标注中甚至会包含“none”,这样我们就能查询未经验证的提交。In our own repos, we stamp each commit with how it was verified. The stamp even includes "none" so we can query for commits that weren't verified.

我们还创建了一个提交门禁:如果会话中没有出现 /code-review 或 /verify,就可以阻止提交。我们为它设置了 off、observe 和 enforce 三种模式。We've also created a commit gate that can block a commit if the session did not contain /code-review or /verify. We've set it up with off, observe, and enforce modes.

观察模式会记录原本会被拦截的内容,以便我们检查误报,并确保不影响任何人。Observe mode will log what would have been blocked, so that we can review any false positives and ensure no one is impacted.

强制模式会阻止提交。每次提交都可以在提交信息中说明未通过验证的原因。如果本次会话中提交已被阻止两次,那么下一次提交将被放行,以免 Claude 陷入困境——Claude Code 并非完美无缺。Enforce mode will block the commit. Any commit can carry an explanation in its message as to why it didn't get verified. If commits have been blocked twice in the session, the next one will be allowed through so Claude doesn't get stuck – Claude Code isn't perfect.

针对真正重要的内容测试你的检查项Test your checks on what actually matters

Claude Code 团队也会定期评估审查者的表现。我们会跟踪已合并的更改中有多少被回滚、维护者在合并前拒绝了多少更改,以及一组规模相当且一直未出问题的更改。The Claude Code team also regularly reviews the reviewers. We track how many merged changes were reverted, how many changes a maintainer rejected before merging, and a size-matched subset of changes that have stayed clean.

也可以通过正式评估来实现。在我们的代码审查文章中,我们介绍了代码审查团队如何基于以往的事故和回滚搭建评估。他们会针对每个示例,将代码库还原到审查时的状态,并提供一句话的答案说明 bug 所在。只有准确描述了相关 bug 的发现才能得分。他们会使用已合并且稳定的变更作为对照,以避免过度标记。他们的调优集与用于评估系统的固定测试集是分开的。This can also be done with a formal evaluation. In our code review post, we described how the Code Review team has set up an evaluation based on previous incidents and reverts. They recreate the repo to the state at the time of review for each example, and provide a one-sentence answer key to what the bug was. Only findings that correctly describe the bug in question get points. They use merged and stable changes as a control to prevent over-flagging. Their tuning set is split out from the fixed test set they use to evaluate their system.

他们还分别评估了原始发现、验证后结果和最终审查结果的召回率。这样他们就能看出系统是否遗漏了先前已经发现的错误。若将这些环节合并为一个端到端分数进行评估,就无法体现这一细微差别。They also evaluated the recall rate for raw findings, post-verification, and final review separately. This lets them see if the system is losing bugs it had already found. Any evaluation that treats all these as one end-to-end score won't capture this nuance.

除了有效性之外,我们关注的另一个维度是这项修改有多重要。优秀的审查者会发现大多数技术上正确的问题,但人们也在意某个 bug 究竟是必须修复、应该修复,还是只是小瑕疵。In addition to validity, the other dimension we care about is how important it is to make the change. A good reviewer will catch most things that are technically correct, but people care about whether a given bug is a must-fix, a should-fix, or a nit.

Code Review 正是这个意思,并会将问题标记为“重要”“小问题”或“已有问题”。“已有问题”标签表示,审查者不应让作者为原本就存在的 bug 负责。Code Review has this exact idea, and it will tag things as Important, Nit or Pre-existing. The "pre-existing" tag means that the reviewer shouldn't hold the author responsible for something that was already a bug.

最后,Claude Code 团队会将召回率调到这样一个程度:再进一步提升,就会变成针对评测集过拟合,而不是真正发现 bug。Finally, the Claude Code team tunes recall up to the point where the next improvement would be overfitting to the eval rather than actually catching bugs.

把失误转化为规则Turn misses into rules

在 Claude Code 仓库中,我们有两种将遗漏案例转化为规则的方法。第一种是从人工代码审查意见和 PR 回滚中提取模式,以识别常见问题。这些问题必须至少出现在两个 PR 中,并且能够从差异中识别出来,才会被视为潜在规则。三个智能体会尝试推翻这些规则,之后由人工通过 PR 审查并批准。In the Claude Code repo, we have two methods of turning missed cases into rules. The first is extracting patterns in human code review comments and PR reverts to identify common issues. These must occur in at least two PRs and be identifiable from a diff to be considered a potential rule. Three agents will try to disprove them, and they will be reviewed and approved by a human in a PR.

第二种方法是针对与已知事故相关联的 PR,重新运行三个漏洞检测工具。如果至少有两个工具发现了该漏洞,就说明这是一个可检测的漏洞。如果在较小的差异中漏掉了该漏洞,那么这就是一个值得在 REVIEW.md 中转化为规则的缺口。The second method is replaying three bug finders against a PR that is associated with a known incident. If at least two find the bug, it's a detectable bug. If the bug is missed on a small diff, this is a gap that is worth turning into a rule in REVIEW.md.

不稳定测试也按同样的方式处理。Claude 不能跳过它们、添加等待时间,或反复重试直到通过。如果找不到根本原因,就删除该测试并记录原因。Flaky tests get the same treatment. Claude can't skip them, add sleeps or retry until they pass. If it can't find the root cause, it deletes the test and writes down why.

规则也需要清理。测试套件、验证步骤和 .md 文件只会越积越多,除非你定期整理它们。Rules need cleaning up too. Test suites, verification steps and .md files only grow if you let them, so we tidy them up on a routine.

优质测试,优质结果GOOD TESTS, GOOD RESULTS

我们发现,为智能体提供一种借助强反馈闭环验证自身工作的方式,能带来巨大的不同。像对待其他系统一样对待你的验证层:让它不受智能体所评判的改动影响,依据那些对你真正重要的错误来评估它们;当它们漏掉问题时,记下经验教训,避免同样的事再次发生,并定期精简这些经验,确保它们始终值得执行。We’ve found that giving your agent a way to verify its work with a strong feedback loop makes all the difference. Treat your verification layer like you would any other system. Keep it out of reach of the changes they judge, grade them against the bugs that mattered to you, and write down lessons when they miss something so that it doesn’t happen more than once, and prune those lessons so they stay worth running.

这样一来,你的工作就会从阅读每一处差异,转变为定义“完成”的标准并审查证据,这样你就能同时运行多个 Claude。Do that and your job shifts from reading every diff to defining what “done” looks like and reviewing the evidence, which is what lets you run many Claudes at once.

值得记下

  1. 验证循环是让你能够把更多工作委派给 Claude 的基础。没有它们,你实际上一次只能运行一两个 Claude,因为所有事情都得自己检查。有了它们,你就能并行运行数十个甚至数百个。
  2. 如果测试不稳定,你就会把智能体训练成反复运行测试,而不是修复失败的代码。如果测试速度很慢,智能体在每批任务中运行的测试就会变少,这会让问题更难定位。
  3. 端到端测试非常有效,因为它们很难被糊弄——整个功能要么能用,要么不能用。它们也有明确的完成标准,能够具体指出哪里出了问题。
  4. 改动不能修改自身的检查。如果一项改动能够修改用于评判它的检查,那么这个检查就失效了。
  5. 我们发现,为智能体提供一套强有力的反馈循环,让它能够验证自己的工作,会带来巨大改变。应当像对待其他系统一样对待验证层。

← 回到本期 · 第 1 条