bundled-skills/invariant-guard/SKILL.md
Correctness-first: forces writing the function contract, loop invariant, termination argument, and edge cases BEFORE code. Catches Boyer-Moore, leftmost binary search, QuickSelect traps.
npx skillsauth add FrancoStino/opencode-skills-antigravity invariant-guardInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
Use invariant-guard when writing or reviewing algorithms where the obvious implementation is subtly wrong:
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.
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.
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:
result contains the sum of a[0..i)."lo ≤ target_position ≤ hi."seen contains every element processed so far; dups contains every element that appeared at least twice."If you cannot write the invariant in one sentence, you have not designed the loop yet.
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 − lo strictly decreases each iteration."i increases by 1 and is bounded above by n."stack.length strictly 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:
len(xs), hi − lo, depth, n).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:
[], "", null, undefined, None).[x]).-0, denormals (for floats).The cases that apply must each have a one-phrase expected behavior written down.
Make illegal states unreachable, not just unhandled. Prefer encoding constraints in types and structure so the wrong state cannot be constructed:
Loading | Loaded(data) | Error(msg) not {loading, data, error}).UserId vs OrderId).If the language cannot encode it, write the invariant as a comment and assert it at the boundary.
Before producing non-trivial code that has loops, recursion, or non-trivial state, your message must contain — in this order:
If any of 1–6 is missing, do not emit code.
This is the canonical "the trap is in the contract, not the loop body" case.
Naive baseline (what gets shipped without the skill):
function findMajority(arr: number[]): number | null {
if (arr.length === 0) return null;
let candidate = arr[0], count = 0;
for (const x of arr) {
if (count === 0) candidate = x;
if (x === candidate) count++; else count--;
}
return candidate; // BUG: returns the candidate even when no majority exists
}
This implementation fails on [1,2,3] (returns 3, expected null) and [2,2,1,1] (returns 1, expected null). The voting loop is correct; the postcondition is wrong.
Why the protocol catches it. Writing step 1 (function contract) forces the postcondition in plain language:
Returns
xiffcount(x, arr) > arr.length / 2; elsenull.
Then writing step 2 (loop invariant) forces the invariant of the voting pass:
If a strict majority element exists in
arr, it equalscandidatewhen the loop exits.
These two statements are not equivalent. The loop invariant guarantees "if a majority exists, it is the candidate" — not "the candidate is a majority." Once you write both down, the gap is visible: you need a second pass to verify, or the postcondition is unmet.
Correct implementation that survives the protocol:
function findMajority(arr: number[]): number | null {
if (arr.length === 0) return null;
// Pass 1: vote.
let candidate = arr[0], count = 0;
// inv: if a strict majority exists in arr, it equals candidate at every count===0 reset.
for (const x of arr) {
if (count === 0) candidate = x;
if (x === candidate) count++; else count--;
}
// Pass 2: verify — the voting invariant is strictly weaker than the postcondition.
let tally = 0;
// inv: tally = count of candidate in arr[0..i).
for (const x of arr) if (x === candidate) tally++;
return tally * 2 > arr.length ? candidate : null;
}
Pattern to generalize. The same trap appears in:
In every case: write the postcondition first; write the loop invariant second; check that the second implies the first. If not, you are missing a pass, a check, or an auxiliary state.
Most "I know binary search" implementations are written for "find any match." The trap is the postcondition.
Problem. Given a sorted array with duplicates, return the index of the leftmost occurrence of target, or -1.
function leftmost(a: number[], target: number): number {
let lo = 0, hi = a.length - 1;
while (lo <= hi) {
const mid = (lo + hi) >> 1;
if (a[mid] === target) return mid; // returns ANY occurrence
if (a[mid] < target) lo = mid + 1; else hi = mid - 1;
}
return -1;
}
// leftmost([1,2,2,2,3], 2) → may return 2, not 1
The loop invariant ("target lies in a[lo..hi] if anywhere") is satisfied. But the postcondition ("returned index is the smallest i with a[i] === target") is strictly stronger. The loop body's early return abandons the search before reaching the leftmost.
function leftmost(a: number[], target: number): number {
// contract:
// pre: a is sorted ascending
// post: returns smallest i with a[i] === target, or -1 if absent
let lo = 0, hi = a.length; // half-open [lo, hi)
// inv: every index < lo has a[i] < target; every index ≥ hi has a[i] > target OR is past leftmost match
// term: hi - lo strictly halves each iteration
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (a[mid] < target) lo = mid + 1; else hi = mid;
}
// exit: lo === hi, and by invariant lo is the leftmost index where a[lo] >= target
return lo < a.length && a[lo] === target ? lo : -1;
}
Same loop shape. The difference is the contract was written first — and the loop body was chosen to maintain an invariant that implies the postcondition.
| Loop / algorithm shape | Canonical invariant | Termination |
|---|---|---|
| Linear scan accumulating | acc = f(a[0..i)) at top | i increases by 1, bounded by n |
| Two-pointer (sorted) | target (if any) lies in a[lo..hi] | hi − lo strictly decreases |
| Binary search | target (if present) ∈ a[lo..hi] and a[lo..hi] non-empty | hi − lo strictly halves |
| Sliding window | window [l..r) satisfies the constraint; answer ≥ best so far | r advances at least once per outer iter |
| BFS | every node at distance < d has been popped; queue contains some at distance d | strict node count decrease per pop |
| DFS / recursion on tree | result for subtree rooted at v = combine(children results) | depth (or remaining nodes) strictly decreases |
| Divide and conquer | result on a[lo..hi] = combine(results on the two halves) | hi − lo strictly halves |
| Greedy with priority queue | extracted item is globally optimal for the remaining problem | heap size strictly decreases per extract |
| Union-Find op | find(x) always returns the canonical root of x's component | tree height bounded by O(log n) (with rank) |
| In-place partition | a[0..i) < pivot; a[i..j) ≥ pivot; a[j..n) unseen | n − j strictly decreases |
| Input shape | Cases to check | |---|---| | Array / list | empty, singleton, all-equal, sorted, reversed, with duplicates | | String | empty, single char, all whitespace, unicode (surrogates, combining), bytes vs code points | | Integer | 0, 1, −1, MIN, MAX, MAX − 1, near overflow in arithmetic, division by 0 | | Float | 0.0, −0.0, NaN, ±Inf, denormal, exact comparison should be ε-based | | Map / dict | empty, missing key (default vs error), key collision semantics | | Tree / graph | empty, single node, cycle (if undirected), self-loop, multigraph, disconnected | | Stream / iterator | empty, infinite, single yield, exception mid-iteration | | Time / date | DST transition, leap second/day, timezone offset, epoch boundary | | Concurrent | empty contention, single thread, max contention, cancellation mid-op |
Code you emit must:
// inv: or # inv:).lemmaly first; pick the algorithm, then state its invariants here.mathguard; invariants for approximate algorithms include ε-bounds, not equality.| Excuse | Reality | | --- | --- | | "I know this algorithm — single pass, done." | Knowing the loop ≠ knowing the contract. The trap usually lives in the postcondition the loop does not enforce. | | "I traced it in my head, it works." | Mental tracing skips edge cases. Write the invariant; check it implies the postcondition. | | "Edge cases are obvious." | Then write them down in 30 seconds. If they are obvious, the table is cheap. If they are not, the table just saved you. | | "Tests will catch it." | Tests catch the examples you thought of. The trap is the example you did not. Postconditions catch all examples. | | "The postcondition is implied." | If it were, the natural loop invariant would equal it. When they differ (Boyer–Moore, leftmost search, QuickSelect), you need a second pass, an extra check, or auxiliary state. | | "Adding a verification pass feels redundant." | Boyer–Moore voting + verification is still O(n). "Feels redundant" is the rationalization that ships the bug. |
while (...) without having stated what is true on entry.if (i === n − 1) or if (i === n) — boundary suspicious, restate the invariant.// TODO: handle empty — handle it now or change the type so empty is impossible.== on floats.Before claiming the function is correct:
// inv: comment in code.Cannot check every box? The code is example-correct, not behavior-correct. Either fill the gap or downgrade the function's claimed contract.
Tests verify examples. Invariants verify behavior. AI assistants ship example-correct, behavior-wrong code by default. invariant-guard makes them reason about behavior first.
lemmaly — algorithm choice must be settled before invariants; load lemmaly first if the algorithm family is unclear.mathguard — ε-bounded postconditions for approximate / randomized algorithms.complexity-cuts — if 3+ optimization transformations have failed tests, the bug is a missing contract, not a missing optimization — escalate here.development
Fetch YouTube transcripts, search videos, browse channels, and extract playlists via TranscriptAPI — no yt-dlp, no Google API key, works from any cloud server.
development
Passive income portfolio analysis — activate when user asks about dividend yields, Treasury rates, REIT income, monthly passive income goals, or portfolio yield optimization. Scans 4 asset classes, ranks by risk-adjusted return, and builds allocations targeting a specific monthly income.
devops
End-to-end production QA, build verification, and launch-readiness checklist for fullstack Next.js apps. Covers TypeScript, linting, tests, build, SEO tags, route regression, and sitemap validation.
development
Safe production cleanup and hardening for vibe-coded fullstack apps (Next.js, React, Node.js, etc.). Removes dead imports, unused files, and broken references without breaking routes or APIs.