create-formal-verification-skill

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Create a formal verification skill

创建形式化验证 skill

A test checks the cases that someone thought to write. Formal verification checks every case in a stated scope: every order of steps in a model, every input up to a bound, or every input. This skill finds the part of the repo where that matters most, verifies one property of it, and leaves a project-local skill that lets the next agent check a change against that property. Write that skill for an agent who reads it cold, mid-change, and has never seen your work. The user must be able to trust the result and act on it without learning the verification language.
测试只会检查有人想到并编写的用例。形式化验证会检查既定范围内的每个用例:模型中步骤的每一种顺序、边界以内的每个输入,或所有输入。该 skill 会找出仓库中最需要这项验证的部分,验证其中的一项属性,并留下一个项目本地 skill,使下一个 agent 可以针对该属性检查变更。为一个初次阅读、正在修改且从未见过你的工作的 agent 编写该 skill。用户应当能够信任结果并据此行动,而无需学习验证语言。

Choose the target from the code

从代码中选择目标

Answer from the codebase, and ask the user only what you cannot observe. If the user named an area or a bug, start there.
A good target is a promise the code makes, where a break harms users and tests rarely reach the case that breaks it. Choose the strongest such target in the repo, not the one that is easiest to verify.
根据代码库作答,只有无法观察到的内容才询问用户。如果用户指明了区域或 bug,就从那里开始。
好的目标应是代码作出的一项承诺:一旦破坏就会伤害用户,而测试很少触达导致破坏的情形。选择仓库中最强的此类目标,而不是最容易验证的目标。

Let the question choose the tool

让问题决定工具

Pick the tool that answers the question at the lowest cost: a model checker, a proof assistant, an SMT-backed verifier, or another tool. Whether events can happen in a bad order usually suits a model checker over a small model of the design, and TLA+ is the default for that in any language. Whether the code itself behaves for every input, as with memory safety, overflow, or a data-structure invariant, usually suits a verifier that runs on the code. If no verifier runs on this language, say so, and test the real code with generated inputs, not a hand model of the function. A property that must hold at every size, where a bound is not enough, can need a proof. Prefer a tool that the repo's developers can install and run, and tell the user why you chose it.
选择能以最低成本回答问题的工具:model checker、proof assistant、基于 SMT 的 verifier,或其他工具。事件是否会以不良顺序发生,通常适合针对设计的小型模型使用 model checker;对于任何语言,TLA+ 都是默认选择。代码本身是否对每个输入都表现正确,例如内存安全、溢出或数据结构不变量,通常适合使用直接作用于代码的 verifier。如果某种语言没有可运行的 verifier,应明确说明,并使用生成的输入测试真实代码,而不是函数的手工模型。必须对所有规模成立的属性(仅设边界不够)可能需要证明。优先选择仓库开发者能够安装和运行的工具,并向用户说明选择原因。

What makes it good

什么让它成为优质 skill

  • True to the code. Every part of the model or proof points to code that a reviewer can open. What the verification treats as one indivisible step is one indivisible step in the code. Names come from this repo.
  • Able to fail. A check that you have never seen fail proves nothing. Show each property failing on a broken variant of the code it guards, such as a removed check or a reordered step.
  • Findings stay findings. When the current code breaks the property, report it with the counterexample in the code's terms. Do not weaken the property or the model until it passes, and do not change product code unless the user asks.
  • Honest about scope. Say what is verified, under which bounds and assumptions, and what is left out. A pass covers the model or the bound, not the whole system.
  • Readable without the tool. State the property and the harm in one plain sentence each. Tell a counterexample as a short sequence of steps in the code's words.
  • Cheap to rerun. One command, a reproducible tool version, and a run short enough that the next agent will use it on every relevant change.
  • Kept in step with the code. Record which files and functions each check covers, and the commit at which you last confirmed that the verification describes them. When the covered code changes after that commit, the next agent can see that the verification needs another look.
  • 忠于代码。 模型或证明中的每个部分都指向评审者可以打开的代码。验证将其视为不可分割步骤的每一部分,在代码中也必须是不可分割的一步。名称取自本仓库。
  • 能够失败。 一项从未见过失败的检查什么也证明不了。展示每项属性在守护它的代码的错误变体上如何失败,例如移除检查或重排步骤。
  • 发现问题就如实保留。 当前代码违反属性时,使用代码术语报告反例。不要削弱属性或模型直到通过;除非用户要求,不要修改产品代码。
  • 对范围保持诚实。 说明验证了什么、在哪些边界和假设下验证,以及未涵盖什么。通过仅覆盖模型或边界,而非整个系统。
  • 不依赖工具也能读懂。 分别用一句直白的话说明属性和危害。用代码中的措辞将反例表述为简短的步骤序列。
  • 重跑成本低。 一个命令、可复现的工具版本,以及足够短的运行时间,让下一个 agent 会在每次相关变更时使用它。
  • 与代码保持同步。 记录每项检查覆盖哪些文件和函数,以及最后确认验证与代码一致时所对应的 commit。覆盖代码在该 commit 后发生变化时,下一个 agent 就能看出验证需要重新检查。

Generate the verify skill

生成 verify skill

Write one project-local skill in the active host's project skill root, per the host-path table, with frontmatter that names the area and says when to use it. Without frontmatter, the skill never registers. From the skill alone, the next agent must be able to tell whether a change touches the verified code, run the check, tell a real bug from a modeling error, and update the verification when a change is intended. Put the coverage record and the sync commit in the skill, and tell the next agent to move the sync commit forward only after it confirms that the verification describes the code at the new commit. Use this repo's real commands, paths, and nouns, and leave no placeholders.
If the check is a TLA+ model, tell the next agent to read every result with the pstack
/statechart
skill. It shows each invariant violation the checker finds as its shortest path in plain words. When the check passes,
/statechart
draws a chart of how the system moves between states. Refer to
/statechart
by name. Its commands, options, and file paths change between releases and machines, so copy none of them into the generated skill or its scripts.
在当前 host 的项目 skill 根目录中,根据 host 路径表 编写一个项目本地 skill,并添加 frontmatter,注明区域和何时使用。缺少 frontmatter,skill 就不会注册。仅凭该 skill,下一个 agent 必须能够判断变更是否触及已验证代码、运行检查、区分真实 bug 与建模错误,并在变更是有意为之时更新验证。将覆盖记录和同步 commit 写入 skill,并告知下一个 agent:只有在确认验证在新 commit 处仍准确描述代码后,才能推进同步 commit。使用本仓库真实的命令、路径和名词,不留任何 placeholder。
如果检查采用 TLA+ 模型,告知下一个 agent 使用 pstack
/statechart
skill 读取每个结果。它会用直白语言展示检查器发现的每个不变量违规的最短路径。当检查通过时,
/statechart
会绘制系统在不同状态之间转换的图表。按名称引用
/statechart
。它的命令、选项和文件路径会随版本和机器变化,因此不要将其中的任何内容复制到生成的 skill 或脚本中。

Prove it before you hand off

交付前证明它有效

Follow the generated skill once, as the next agent would, on a scratch change that breaks the property in the real code. It must catch the break. Then drop the scratch change and confirm that the check is back where it started: passing, or failing only on the findings you will report. A verify skill that was never run is a draft. If the skill misses the break or a step does not run, fix the skill or the verification, not the scratch change.
按照生成的 skill,以下一个 agent 的方式,在一处破坏真实代码中该属性的试验性变更上运行一次。它必须捕获该破坏。然后撤销试验性变更,并确认检查恢复到初始状态:通过,或者仅因将要报告的发现而失败。从未运行过的 verify skill 只是草稿。如果 skill 漏掉破坏或某一步无法运行,应修复 skill 或验证,而不是试验性变更。

Hand off

交付

Tell the user what you verified, which tool you used and why, the bounds, what the check found in plain words, how to run it, and what it does not cover. Suggest a second target only if you found a close one. Point the user at
/maintain-formal-verification-skill
for bringing the verification back in line after changes that did not recheck it.
告知用户验证了什么、使用什么工具及原因、边界、检查以直白语言发现了什么、如何运行,以及不覆盖什么。只有在发现另一个紧密相关目标时,才建议第二目标。指向
/maintain-formal-verification-skill
,以便在变更后未重新检查时使验证恢复一致。