Invariex
by sudo-py-dev
README.md
<p align="center">
<img src="assets/logo.svg" alt="Invariex" width="460">
</p>
<p align="center">
<b>A neurosymbolic bug hunter — it proves the bug before it reports it.</b>
</p>
<p align="center">
<a href="https://github.com/sudo-py-dev/invariex/actions/workflows/ci.yml"><img src="https://github.com/sudo-py-dev/invariex/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
<img src="https://img.shields.io/badge/license-Apache--2.0-blue.svg" alt="License: Apache-2.0">
<img src="https://img.shields.io/badge/python-3.10%2B-3776AB?logo=python&logoColor=white" alt="Python 3.10+">
<a href="https://github.com/astral-sh/uv"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv"></a>
<a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff"></a>
<img src="https://img.shields.io/badge/MCP-ready-7c6cff" alt="MCP ready">
<img src="https://img.shields.io/badge/tests-79%20passing-brightgreen" alt="Tests">
</p>
---
Invariex infers what your code is *supposed* to do, writes it as a small formal
model with invariants, bounded-checks that model for an execution that breaks an
invariant, and then **proves every finding with a real failing test** before it
reaches the report. **No test, no finding.**
It targets the bugs that slip past linters and unit tests: concurrency and
distributed-systems defects — data races, thread-safety violations, check-then-act
(TOCTOU) races, lease/consensus mistakes, lost updates, and double-charge bugs.
```text
intent → check → triage → prove → fix
│ │ │
└── revise model ─────┘ re-check ──┘
```
<table>
<tr><td width="50%" valign="top">
### ✦ Why it's different
- **Proof, not suspicion** — the report only contains bugs with a failing repro test.
- **Honest bounds** — every finding states its checked bound; never a false "proven everywhere".
- **Language-agnostic core** — reasons over an abstract interleaving model, not source text.
- **Driveable anywhere** — CLI, MCP server, or importable library.
- **Resumable** — job state in libSQL/Turso (local, remote, or embedded replica).
</td><td width="50%" valign="top">
### ✦ What it catches
- Data races & thread-safety violations
- Check-then-act (TOCTOU) races
- Lost updates (incl. lost money)
- Lease / consensus revocation bugs
- Double-charge / double-spend
- …any invariant you can state
</td></tr>
</table>
> **Status:** alpha. The orchestrator, Turso-backed job store, agent tool-use
> loop, bounded checker, prove-it gate, SARIF/Markdown reporting, and the MCP
> server all run today. Formal lowering (code → model) and language-specific repro
> harnesses are wired as optional, gracefully-degrading backends.
---
## 🚀 Quick start
[`uv`](https://docs.astral.sh/uv/) is the project manager.
```bash
git clone https://github.com/sudo-py-dev/invariex
cd invariex
uv sync # create the env + install dependencies
uv run invariex check toctou # bounded-check a built-in example (no API key)
```
You'll see a concrete counterexample schedule — the exact interleaving that
breaks the invariant.
## 👤 For users
```bash
uv run invariex specs # list built-in concurrency specs
uv run invariex check <spec> # bounded-check one spec
uv run invariex languages # supported languages + repro backends
uv run invariex languages --detect . # detect a repo's languages
# Scan a repo (offline example — uses a built-in model):
uv run invariex scan . --hint "double charge on lease revoke" \
--spec invariex.specs.lease_double_charge --out .invariex/out
uv run invariex jobs # resumable job list
uv run invariex report <job_id> # re-emit SARIF + Markdown
```
With `ANTHROPIC_API_KEY` set and the LLM extra installed (`uv sync --extra llm`),
the agent drives `scan` on real code; otherwise a deterministic offline agent
checks the given `--spec`, so the whole pipeline runs with no network.
### Connect it to your AI agent
Invariex is an MCP server — plug it into Claude Code, Cursor, and others:
```bash
claude mcp add invariex -- invariex mcp
```
Full setup for **Claude Code, Claude Desktop, Cursor, Codex, Windsurf, Cline, and
Zed** is in [`docs/INTEGRATIONS.md`](docs/INTEGRATIONS.md).
### Storage (Turso / libSQL)
Job state lives in libSQL. Three modes, chosen by environment — no code change:
| Mode | How |
|---|---|
| Local file *(default)* | nothing to set |
| Remote Turso | `TURSO_DATABASE_URL` + `TURSO_AUTH_TOKEN` |
| Embedded replica | the above **+** `INVARIEX_DB_SYNC=1` |
## 🛠 For developers
```bash
uv sync
uv run ruff format invariex tests
uv run ruff check invariex tests
uv run pytest
```
**How it works**
1. **Intent** — write the code's intended behavior as a state-machine model with invariants.
2. **Check** — bounded model checking: exhaustively explore every interleaving *up to a size limit*; return the first invariant violation as a trace.
3. **Triage** — a counterexample is a *lead*, not a bug (misread → revise the model; real → candidate).
4. **Prove** — write a deterministic test that reproduces it. No test, no finding.
5. **Fix** — propose a minimal patch, then re-run the check and the test.
**Project layout**
```text
invariex/
config.py env-driven Settings + DbConfig (local / remote / replica)
models.py Job, Candidate, Finding, Severity, ImpactClass, Phase
serialize.py domain <-> JSON wire format
store.py resumable job store on libSQL / Turso
checker.py dependency-free bounded interleaving enumerator
orchestrator.py drives the loop; owns the job record
agent/ tool-use agent: base protocol, LLM loop, offline agent
tools/ read_file, grep, write_lean, lean_build, run_check,
write_test, run_test, write_patch
report/ sarif.py + markdown.py
specs/ runnable corpus (lost_update, lease_double_charge,
toctou, bank_overdraft)
languages.py language registry + repro backends + repo detection
mcp/ zero-dependency stdio JSON-RPC MCP server
cli.py scan / check / specs / languages / jobs / report / mcp
```
The checker is language-agnostic, so adding a concurrency pattern is just a new
`ConcurrencyModel` in `invariex/specs/`. Run it with `uv run invariex check <name>`.
## License
[Apache-2.0](LICENSE).