elinvar 0.1.0

Find the holes in a design before you build it: a relational model finder, its command line, and an MCP server
Documentation
---
name: elinvar
description: Find the holes in a design before it is built. Describe the things, the links between them, the rules, and a promise; Elinvar searches every small case and returns the steps that break the promise, or says it holds up to a size. Use it for data models, permissions, workflows, protocols, and any rules that interact.
---

# Elinvar

Elinvar checks designs, not code. A design has things (sets), links (relations), rules
(facts), and promises (assertions). Elinvar tries every case up to a size and either
finds one that breaks a promise, a counterexample, or says the promise holds up to
that size.

Reach for it when rules interact: a permission and a revocation, a state machine and
its steps, a hierarchy and its invariants. A hole there is in how rules meet, which
reading them one at a time does not find.

## The loop

1. Write the design as an Alloy 6 model (`.als`), in the subset Elinvar reads (below).
   Or work directly on a SQL schema (a `.sql` file, a `pg_dump --schema-only` dump, or
   a `migrations/` directory of ordered `*.sql` files): `elinvar import schema.sql`
   turns tables, keys, `CREATE TYPE ... AS ENUM`, `ALTER TABLE` / `DROP` migrations,
   `CHECK` constraints, `CREATE VIEW` / parameterized `-- name:` `SELECT` queries (as
   Alloy `fun`s, and automatically expanded when referenced in `FROM`/`JOIN` inside
   triggers, queries, or other views), and triggers (`--temporal`, `--from-empty`, with retired-row non-reuse
   and `strftime`/date guards) into an Alloy 6 model; add `--strict` to fail on any
   unread rule, or `--tables a,b` with optional `--with-referencing` and `--only-ruled`
   to narrow large schemas. Keep promises and queries in sidecar files and pass them
   with `--with promises.als --with queries.sql` on `check`, `run`, `tests`, `accepts`,
   `diff`, `fuzz`, `discover`, `audit`, and `eval` without editing generated models.
2. `elinvar check model.als --json` (or `elinvar check schema.sql --with promises.als`,
   or `-` to read from stdin). One call per check; nothing is kept between calls.
   Useful flags: `--explain` (minimal unsat core of facts and step guards, cheap-first
   and bounded by `--budget`, plus rules unused by any check), `--induction [K]` (or
   `by induction [k] [using Q1, Q2]` in the file, to prove `always P` state or 1-step
   transition invariants for any number of steps within scope), `--climb --budget N`
   (climb scopes automatically until a hole appears or budget runs out), `--timeout DUR`
   (wall-clock time limit alongside `--budget`, distinguishing which limit ended an
   `unknown` search), `--summary` (one compact table across all checks), and `--brief`.
3. Read the verdict and any `warnings` (`als.int_wrap`, `als.unframed_var`,
   `als.empty_by_type`, `model.trivial_holds`, `model.vacuous_premise`). On a
   counterexample, read its `story`: the steps that break the promise and the failing
   quantified witness, already shrunk to the fewest atoms. Render a result with
   `elinvar render result.json --mermaid` or replay it on SQLite with
   `elinvar render result.json --sql` (with optional `--values table.col=v1,v2`), or
   draw a model's signatures and steps with `elinvar render model.als --mermaid`.
4. Verify and explore SQL schemas end to end:
   - `elinvar fuzz schema.sql --against sqlite3 [--require-coverage]` runs seeded
     dependency-ordered SQL scripts on both the real database and Elinvar's model,
     reporting per-side allowed/refused counts, per-table inserts, rule refusals, and
     rules that never fired (exiting `2` with `--require-coverage` if any table got 0
     rows or any read rule never fired), counting unread-rule refusals separately, and
     shrinking the first unexplained disagreement.
   - `elinvar discover schema.sql [--show-rejected]` discovers and proves common
     invariants by induction (append-only rows, FK delete protection, immutable /
     set-once columns, uniqueness, composite at-most-one-open row) in plain English with
     the triggers and rules each rests on.
   - `elinvar audit schema.sql --data dump.sql --with promises.als` checks every table
     rule and single-state promise directly on a real SQL dump without invoking the SAT
     solver.
   - `elinvar ci [elinvar.toml] [--timeout 60s] [--junit | --sarif | --json]` runs a
     repository manifest under one shared timeout and writes Markdown, JUnit, SARIF, or
     JSON.
   - `elinvar tests model.als --sql` generates step-reachability and per-guard refusal
     tests (with `--tables`, `--with-referencing`, `--values`, `--allow-dead`, and
     `-- expect-dead:` comments).
   - `elinvar accepts model.als trace.sql` checks real traces or SQL DML scripts against
     the model (with sorted ordered values, real calendar date shifts, rollback on
     refusal, `AFTER` triggers, FKs, defaults, and exact refused line/rule reporting).
   - `elinvar check schema.sql --with promises.als [--from-any]` checks SQL schemas from
     empty tables by default (pass `--from-any` for any allowed state), `elinvar import`
     accepts `--with` `.sql` and `.als` sidecars, `elinvar run model.als --all [--sql]`
     enumerates all non-isomorphic small instances (or `--shrink` for one minimal
     instance), and `elinvar diff old.sql new.sql` (default `--from-any` in 2 steps,
     optional `--from-empty`, `--migration m.sql`, `--of FUN`, `--ignore-unread`)
     compares two revisions and refuses to claim `equivalent` if a changed rule was
     never reached.

Ask questions about a counterexample without solving again:
`elinvar eval model.als result.json 'Item.shared' --state 2` (also works on `.sql`
schemas and migration directories with `--with` and `--tables`).

## Verdicts

| Verdict | Means | Exit status |
|---|---|---|
| `holds` | no counterexample up to the scope searched. Evidence, not proof: a larger case might break it | 0 |
| `proved` | proved for any number of steps within the scope by k-induction (`by induction` / `--induction`) | 0 |
| `found` | `run` found an instance | 0 |
| `counterexample` | a case that breaks the promise, refereed and shrunk | 1 |
| `none` | `run` found no instance up to the scope | 1 |
| `vacuous` | the rules allow no case at all, so nothing can break the promise. The rules are wrong, not the promise right. Never report this as "holds" | 2 |
| `unknown` | the budget ran out; `note` says what is known | 2 |
| errors | the model has errors; each has `code`, `place`, and `fix` | 3 |

`elinvar explain CODE` explains any error code; `elinvar explain schema` lists the
versioned JSON Schemas for every verb (`schema:result` for one check or run result,
`schema:results` for a file with several commands, `--summary`, or `run --all`, `schema:diff`, `schema:import`,
`schema:render`, `schema:eval`, `schema:tests`, `schema:accepts`, `schema:fuzz`,
`schema:discover`, `schema:ci`, `schema:audit`, and `schema:usage` for a bad flag
or unreadable input); `elinvar explain skill` prints this
guide directly from the binary.

## Writing a model

```alloy
sig Person { var follows: set Person }
sig Item { owner: one Person, var shared: set Person }

fun canOpen: Item -> Person { owner + (shared & owner.~follows) }

pred share[i: Item, p: Person] {
  i.owner in p.follows
  shared' = shared + i->p
  follows' = follows                 -- say what stays the same
}

fact { no shared  always (share or ...) }

assert NoLeak { always all i: Item | i.shared in i.canOpen }
check NoLeak for 3 but 6 steps
```

- A step must say what it leaves unchanged (`follows' = follows`). Forgetting that is
  the most common mistake: an unconstrained relation can change to anything.
- `var` marks what changes; facts hold in the first state unless wrapped in `always`.
- Scope: `for 3` is at most 3 of each signature; `but 6 steps` limits traces to 6
  states. Small scopes find most holes.
- Give commands names, or use `name: check {...}`, to call one with
  `elinvar check model.als NAME`.

The subset: signatures (`abstract`, `one`, `lone`, `some`, `extends`, `in`, `var`,
`enum`), fields with multiplicities, arrows, sequences (`seq T`, `seq/Int`, `for N seq`),
and type-resolved overloading, facts, predicates and functions, assertions, `run` and
`check` with scopes, bitwidths (`for 5 Int`), steps, `by induction [k] [using Q1, Q2]`,
and `expect` (exit status follows `expect` when set); the operators of
relational logic (`. -> + - & ++ <: :> ~ ^ * []`), bounded wrapping integers (`#`, `Int`,
`int`, `sum`, comparisons, `int/max`, `int/min`, `int/next`, `int/prev`, `prevs`,
`nexts`, and `util/integer`), sequences (`util/sequniv` operations: `elems`, `inds`,
`first`, `last`, `rest`, `butlast`, `add`, `delete`, `insert`, `setAt`, `subseq`,
`append`, `hasDups`, `isEmpty`, `idxOf`, `lastIdxOf`, `indsOf`, `afterLastIdx`,
`maxIdx`, `minIdx`), quantifiers, `let`, comprehension, `open` (`util/ordering`,
`util/relation`, `util/graph`, `util/boolean`, `util/history`, `util/sequniv`, and local `.als` modules
beside the file), and the temporal operators (`always eventually after before
historically once until releases since triggered` and `'`).
Not read: strings, unbounded `1.. steps`, `sum disj`, recursive predicates or functions,
and other `util/` library modules. Each is an `als.unsupported` error with its line,
never a wrong answer.

## What not to claim

- "Holds" holds up to the scope. Say "no counterexample up to 3 of each and 6 steps".
- A vacuous check proves nothing. Report the contradiction in the rules.
- An unknown verdict is not a pass. Raise `--budget`, or shrink the scope.