elinvar 0.1.2

Find the holes in a design before you build it: a relational model finder, its command line, and an MCP server
Documentation
<div align="center">

# <img src="https://raw.githubusercontent.com/rustborg/elinvar/main/docs/logo.svg" alt="" width="38" height="38" align="absmiddle">&nbsp;Elinvar

[![crates.io](https://img.shields.io/crates/v/elinvar.svg?label=crates.io&color=e6733c)](https://crates.io/crates/elinvar)
[![docs.rs](https://img.shields.io/docsrs/elinvar?label=docs.rs)](https://docs.rs/elinvar)
[![CI](https://github.com/rustborg/elinvar/actions/workflows/ci.yml/badge.svg)](https://github.com/rustborg/elinvar/actions/workflows/ci.yml)
[![license](https://img.shields.io/crates/l/elinvar?color=2f6fb0)](https://github.com/rustborg/elinvar#license)
[![Ask DeepWiki](https://img.shields.io/badge/Ask-DeepWiki-5a67d8)](https://deepwiki.com/rustborg/elinvar)

**Find the holes in a design before you build it.**

[Book](https://rustborg.github.io/elinvar/) |
[Quick start](https://rustborg.github.io/elinvar/guide/quickstart.html) |
[Examples](https://rustborg.github.io/elinvar/guide/examples.html) |
[API docs](https://docs.rs/elinvar) |
[Releases](https://github.com/rustborg/elinvar/releases)

</div>

Elinvar is a fast, single-binary relational model finder and SQL schema verifier in pure
Rust, inspired by [Alloy](https://alloytools.org/). You describe the things in your
system, the links between them, the rules, and a promise that must always hold. Elinvar
exhaustively checks every small case and either tells you the promise holds up to that
size, or hands you the smallest counterexample that breaks it, double-checked by an
independent referee.

Built for both engineers and AI coding agents: zero Java, zero `unsafe`, zero runtime
dependencies, deterministic across platforms. Use it from the command line, inside Rust
`cargo test`, or as a stateless MCP server (`elinvar serve --mcp`).

## What it helps you with

Every rule can look right on its own while the interaction between two rules hides a bug:

- **Permissions and revocation:** Can someone still access or annotate an item after their
  group membership or follow is removed?
- **Workflows and state machines:** Is there a sequence of valid steps that reaches an
  illegal state?
- **Database schemas and migrations:** Import SQL `CREATE TABLE`, `CHECK`, `VIEW`, and
  `TRIGGER` rules directly (`import`), prove invariants (`discover`), diff migrations
  (`diff --migration`), audit real dumps (`audit`), and fuzz against SQLite or Postgres
  (`fuzz`).
- **Design changes:** Compare two revisions of a design with `elinvar diff` to see a
  minimal case allowed by one and forbidden by the other.
- **Contradictory rules:** Unlike a naive checker, Elinvar distinguishes `holds` from
  `vacuous` (your rules contradict each other and allow no case at all) and explains
  which rules did the work (`--explain`).

## Quick start

Install the `elinvar` binary from crates.io:

```sh
cargo install elinvar
```

Or build from a local checkout:

```sh
cargo install --path crates/elinvar
```

A tagged release also has archives for Linux, macOS, and Windows on the
[releases page](https://github.com/rustborg/elinvar/releases), each with the binary,
`SKILL.md`, the licenses, and a SHA-256 checksum.

Check a design right from stdin (no temp file needed). Here, each employee has at most one
manager, nobody is their own manager, and we promise everyone reaches someone at the top:

```sh
elinvar check - <<'EOF'
sig Employee { manager: lone Employee }
fact NoSelfManager { no e: Employee | e = e.manager }
assert EveryoneHasATop {
  all e: Employee | some top: Employee | top in e.*manager and no top.manager
}
check EveryoneHasATop for 3
EOF
```

Elinvar immediately finds the smallest bug, a two-employee cycle:

```text
check EveryoneHasATop: COUNTEREXAMPLE with Employee: 2
  Employee = {Employee0, Employee1}
  manager = {Employee0 -> Employee1, Employee1 -> Employee0}
  witness e = Employee1
  searched every case up to Employee: 3.
```

Fix the rule (`no e: Employee | e in e.^manager`), and pass `--explain` to confirm which
rules actually matter:

```text
check EveryoneHasATop: holds
  searched every case up to Employee: 4; a larger case might still break it.
  used rules: field manager, fact Acyclic
```

## Catching multi-step bugs over time

Take a sharing app with three rules:

1. A reader shares an item with a follower.
2. The reader can end the follow.
3. Someone an item is shared with can leave a note on it.

Promise: nobody leaves a note on an item they can no longer open.

```sh
elinvar check corpus/holes/sharing.als NotesNeedAccess
```

Elinvar finds the 3-step hole automatically (share, unfollow, note) and shrinks it to 2
people and 1 item:

```text
check NotesNeedAccess: COUNTEREXAMPLE in 4 states, with Person: 2, Item: 1 [expect 1: met]
  start
    Person = {Person0, Person1}
    Item = {Item0}
    owner = {Item0 -> Person1}
    follows = {Person0 -> Person1}
  step 1  share(Item0, Person0)  shared gains {Item0 -> Person0}
  step 2  unfollow(Person0, Person1)  follows loses {Person0 -> Person1}
  step 3  note(Item0, Person0)  notes gains {Item0 -> Person0}
  step 4  nothing changes, back to state 3
  broken at state 2, as step 3 begins (for i = Item0, p = Person0)
  searched every case up to Person: 3, Item: 3, steps: 6.
```

## Everyday CLI workflows

```sh
# Check a model (as stories, or structured JSON)
elinvar check corpus/holes/sharing.als
elinvar check corpus/holes/sharing.als --json

# Explain which rules established `holds` or caused `vacuous`, or why a run
# found no instance
elinvar check model.als --explain
elinvar run model.als --explain

# Import a SQL DDL schema (or a migrations directory) into an Alloy 6 model
elinvar import schema.sql > schema.als

# Does the schema allow this SQL script? Does a real database agree with the model?
elinvar accepts schema.sql script.sql
elinvar fuzz schema.sql --against sqlite3

# Prove invariants of a schema, check a dump against its rules, or vet a migration
elinvar discover schema.sql
elinvar audit schema.sql --data dump.sql
elinvar diff schema.sql --migration add_guard.sql

# Run every check a repository's elinvar.toml lists, under one timeout
elinvar ci --junit

# Compare two design revisions (equivalent, tightened, loosened, or diverged)
elinvar diff v1.als v2.als

# Render a counterexample as Mermaid or Graphviz DOT
elinvar check model.als --json | elinvar render - --mermaid

# Inspect any expression on a saved result without re-solving
elinvar eval corpus/holes/sharing.als result.json 'Item.canOpen' --state 2

# Self-discover error codes, result JSON Schema, and the embedded agent guide
elinvar explain arity.mismatch
elinvar explain schema:result
elinvar explain skill
```

Pass `-` for any single file argument to read from stdin.

## Using Elinvar in Rust (`cargo test`)

Put design checks right next to your code in `cargo test` using `elinvar::prelude::*`.
The sharing example is 32 lines in
[crates/elinvar/tests/readme.rs](crates/elinvar/tests/readme.rs), and `assert_holds()`
fails with the shrunk 3-step story:

```text
check NotesNeedAccess: COUNTEREXAMPLE in 4 states, with Person: 2, Item: 1
  start
    owner = {Item0 -> Person1}
    follows = {Person0 -> Person1}
  step 1  share(Item0, Person0)  shared gains {Item0 -> Person0}
  step 2  unfollow(Person0, Person1)  follows loses {Person0 -> Person1}
  step 3  note(Item0, Person0)  notes gains {Item0 -> Person0}
  broken at state 2, as step 3 begins
```

See [Checking designs in Rust](docs/guide/rust.md) for a full walkthrough.

## Using Elinvar with AI coding agents

Writing code is now cheap; catching subtle rule interactions before code is written is
harder. Elinvar gives coding agents a fast, stateless formal oracle with versioned JSON
Schemas, stable exit codes, and `vacuous` detection so an agent cannot accidentally pass
a broken spec by contradicting its own rules.

- **MCP server:** point Claude Code, Cursor, or any MCP client at `elinvar serve --mcp`.
  It exposes 11 tools (`check`, `run`, `diff`, `import_sql`, `render`, `eval`, `tests`,
  `accepts`, `discover`, `audit`, `explain`), server instructions on `initialize`, and
  embedded resources (`skill://elinvar/SKILL.md` and every JSON Schema).
- **CLI agents:** any agent with shell access can run `elinvar explain skill` to load the
  embedded guide directly from the binary, then pipe models with `elinvar check - --json`.

See [Using Elinvar with coding agents](docs/guide/agents.md) and
[skills/elinvar/SKILL.md](skills/elinvar/SKILL.md).

## What Elinvar does not do

- **Not an unbounded proof over arbitrary sizes:** `holds` means no counterexample exists
  up to the scope you searched (`--induction` / `by induction` proves `always P` for any
  number of steps within that scope, returning `proved`). Small scopes catch most bugs,
  and you can raise `--scope` when needed.
- **Checks designs and schemas, not source code:** Elinvar verifies models of your rules
  and SQL schemas rather than your Rust or application implementation directly,
  complementing code-level verifiers such as Verus or Kani.

## Elinvar and Alloy

Elinvar reads a documented subset of Alloy 6, and its answers are checked against Alloy
6.2.0's: the 224 commands that Alloy answers in its example models and in Elinvar's
planted holes, and 183 commands written to try one construct of the language each, give
the same verdict in both. See
[Alloy, and what Elinvar takes from it](docs/guide/alloy.md) and
[docs/CORPUS.md](docs/CORPUS.md).

## Documentation

The guides and specifications are published as a book at
<https://rustborg.github.io/elinvar/>.

| Guide | Contents |
|---|---|
| [Quick start]docs/guide/quickstart.md | install, run your first check, read verdicts, fix a hole |
| [Practical examples]docs/guide/examples.md | permissions, state machines, SQL schemas, design diffs, vacuous rules |
| [Command-line reference]docs/guide/cli.md | every verb, flag, stdin piping, and exit code |
| [Checking designs in Rust]docs/guide/rust.md | writing models in Rust and asserting invariants in `cargo test` |
| [Using Elinvar with coding agents]docs/guide/agents.md | MCP setup, CLI agent loop, and prompt ideas |
| [Modeling dated history tables]docs/guide/history.md | append-only, timestamped tables with `util/history` |
| [Finding holes in a design]docs/guide/finding-holes.md | conceptual introduction, no background required |
| [Alloy and Elinvar]docs/guide/alloy.md | what Elinvar shares with Alloy and where it differs |
| [Architecture overview]docs/guide/architecture.md | the crates and how a model becomes an answer |

For internals and specifications: [docs/DESIGN.md](docs/DESIGN.md),
[docs/LOGIC.md](docs/LOGIC.md), [docs/CORPUS.md](docs/CORPUS.md),
[docs/CODES.md](docs/CODES.md), [REQUIREMENTS.md](REQUIREMENTS.md),
and [AGENTS.md](AGENTS.md). Run `cargo x docs` to read the book
locally.

## The name

Elinvar is an alloy whose springiness stays the same as it warms and cools. The name is
short for the French for "invariable elasticity". This tool checks what must stay the
same: invariants.

## Credit

Alloy is by Daniel Jackson and the Alloy team at MIT. Kodkod is by Emina Torlak. The time
extension of Alloy 6 comes from the Electrum project and its engine, Pardinus. Elinvar is
not affiliated with any of them.

## Citing

If you use Elinvar in your work, cite it with [CITATION.cff](CITATION.cff); GitHub's
"Cite this repository" button gives it as APA or BibTeX.

## License

Elinvar is licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option. See [NOTICES.md](NOTICES.md).