invariant-guard
它能做什么
这个工具可以帮你编写更可靠的代码。在动手写实际代码之前,它会让你先描述规则(也就是不变式)、代码如何结束,以及各种特殊情况。
适用场景
- ✓编写可能存在隐蔽漏洞的算法时。
- ✓处理循环或递归调用时。
- ✓检查代码,确保它在各种情况下都能正常运行时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/invariant-guard/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个函数,找出列表中最大的元素,但先定义循环不变式、终止性论证和边界情况。”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会要求你正式写明代码(比如循环或递归)每一步执行之前、执行过程中和执行之后必须成立的条件。这有助于避免那些常常被忽视的常见错误。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
invariant-guard — Correctness-First Coding
The model knows what a loop invariant is. It knows recursion needs a base case. It knows about empty lists, integer overflow, and the difference between < and ≤. It just does not write these down before producing code, so it ships subtle correctness bugs that tests do not catch.
invariant-guard fixes the behavior. State the invariants. State the base case. State the termination argument. State the edge cases. Then write the code — and verify that the code maintains what you stated.
Violating the letter of these rules is violating the spirit of the skill. "I know this algorithm" is the exact rationalization that ships off-by-one and missing-postcondition bugs.
When to Use This Skill
Use invariant-guard when writing or reviewing algorithms where the obvious implementation is subtly wrong:
- Postcondition stronger than the loop's natural invariant: Boyer–Moore majority, Floyd's cycle detection, leftmost vs any binary search, QuickSelect partition.
- In-place mutation with read+write pointers: dedup-in-place, partition, rotate.
- Recursion with multiple parameters or accumulator state.
- Off-by-one suspects with duplicates, empty inputs, boundary values.
- Iterative refinements that must terminate: fixed-point, Newton, EM.
- Any function where you catch yourself thinking "I know this algorithm" — the trap is usually in the contract, not the loop body.
Pairs with lemmaly (picks the algorithm) and mathguard (picks the math). Load invariant-guard after the algorithm has been chosen and before the loop body is written.
The Iron Law
NO LOOP OR RECURSION WITHOUT A WRITTEN INVARIANT AND TERMINATION ARGUMENT
If you cannot write the invariant in one sentence, you have not designed the loop. Write code anyway and you are coding by guess — and the bug will be in the case you did not enumerate.
Non-negotiable rules
Every loop gets a one-line invariant. Before writing any loop, state in one sentence what is true at the top of every iteration. Examples:
- "At loop top:
resultcontains the sum ofa[0..i)." - "At loop top:
lo ≤ target_position ≤ hi." - "At loop top:
seencontains every element processed so far;dupscontains every element that appeared at least twice."
If you cannot write the invariant in one sentence, you have not designed the loop yet.
- "At loop top:
Every loop gets a one-line termination argument. Name the quantity that strictly decreases (or strictly increases toward a bound) on every iteration. Examples:
- "
hi − lostrictly decreases each iteration." - "
iincreases by 1 and is bounded above byn." - "
stack.lengthstrictly decreases each pop; nothing pushes inside this branch."
No termination argument, no loop.
- "
Every recursion gets an explicit base case and a measure. Before writing a recursive function, state:
- The base case(s) — the smallest inputs that return without recursing.
- The measure — a non-negative integer that strictly decreases on every recursive call (e.g.
len(xs),hi − lo,depth,n). - The combination — how the recursive results combine into the answer.
No base case + measure, no recursion. (Mutual recursion: state the measure across the cycle.)
List edge cases before writing, not after. For every function operating on a collection or number, list which of these apply and how they behave:
- Empty input (
[],"",null,undefined,None). - Singleton (
[x]). - All-equal elements.
- Already-sorted / reverse-sorted input.
- Duplicates (when uniqueness is assumed).
- Negative numbers, zero, exactly the boundary value.
- Integer overflow / underflow at the type max/min.
- NaN, ±Infinity,
-0, denormals (for floats). - Off-by-one boundaries: index 0, index n−1, index n, length 0, length 1.
- Concurrent modification while iterating.
The cases that apply must each have a one-phrase expected behavior written down.
- Empty input (
Make illegal states unreachable, not just unhandled. Prefer encoding constraints in types and structure so the wrong state cannot be constructed:
- Sum type over boolean flag soup (
Loading | Loaded(data) | Error(msg)not{loading, data, error}). - Newtype for IDs that must not be swapped (
UserIdvsOrderId). - Non-empty list type when the function requires at least one element.
- Sum type over boolean flag soup (
…
常见问题
什么是“循环不变式”?
它是指在循环每次重复开始时都始终为真的一条陈述。例如,“变量 X 始终保存已处理元素的总和”。
什么是“终止性论证”?
它是对循环或递归函数如何结束的解释。通常会说明某个量在每一步都会朝着某个边界递减或递增。
为什么要先列出“边界情况”?
因为很多漏洞都出现在不寻常的情况下,比如空列表、只有一个元素的列表或包含重复值的列表。提前考虑这些情况可以避免错误。
详情
作者的原始描述
Correctness-first: forces writing the function contract, loop invariant, termination argument, and edge cases BEFORE code. Catches Boyer-Moore, leftmost binary search, QuickSelect traps.
热度
- GitHub 星标
- 48k
- 排名
- #4965
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。