Agent skill · software engineering · sickn33
invariant-guard
Correctness-first: forces writing the function contract, loop invariant, termination argument, and edge cases BEFORE code. Catches Boyer-Moore, leftmost binary search, QuickSelect traps.
Why this skill is useful
Adds a structured approach to writing function contracts, loop invariants, and termination arguments that the AI wouldn't reliably generate on its own.
What it needs
About 12k tokens when loaded. Last updated 2026-08-07. 44,571 stars on the source repository.
What this skill does
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 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 1. Every loop gets a one-line invariant. Before writing any loop, state in one sentence what is true at the top of every iteration. …
How to use it
Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:
@skills sickn33/invariant-guard