Metadata-Version: 2.4
Name: tanny
Version: 0.2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Dist: temporal-typing>=0.6.0
License-File: LICENSE
Summary: Check Python's temporal typing: a model checker for temporal-typing specifications, in Rust, on Manyan.
Keywords: tla+,formal-methods,model-checking,verification,typing
Author-email: Ju Lin <soasme@gmail.com>
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/soasme/tanny

# tanny

[![CI](https://github.com/soasme/tanny/actions/workflows/ci.yml/badge.svg)](https://github.com/soasme/tanny/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/tanny)](https://pypi.org/project/tanny/)
[![Python](https://img.shields.io/pypi/pyversions/tanny)](https://pypi.org/project/tanny/)
[![License: MIT](https://img.shields.io/github/license/soasme/tanny)](https://github.com/soasme/tanny/blob/main/LICENSE)

**tanny** is a static model checker for Python specifications written with [temporal-typing](https://github.com/soasme/temporal-typing).

`temporal-typing` expresses state-machine specifications—invariants, actions, preconditions, postconditions, fairness, temporal properties, and refinements—as ordinary Python type annotations. `tanny` parses those declarations directly from Python source without executing user code, translates each `Model` into TLA⁺ with Python semantics, and model-checks it in process using [Manyan](https://github.com/soasme/manyan).

`tanny` is written in Rust and distributed as a self-contained CLI and Python package with no JVM dependency.

## Features

- **Static source analysis:** Parses Python files with Ruff's parser without importing or executing user modules.
- **Full specification coverage:** Implements the [temporal-typing checker specification](https://github.com/soasme/temporal-typing/blob/main/docs/checker.md), checking assumptions, invariants, action invariants, temporal properties under weak and strong fairness, method-body conformance, frame conditions, assertions, function contracts, deadlocks, and refinements.
- **Python-level counterexamples:** Reports failing traces in terms of Python actions, arguments, and state variables.
- **In-process TLA⁺ model checking:** Embeds [Manyan](https://github.com/soasme/manyan) natively with deterministic multi-worker state exploration (`--workers`).
- **CI-friendly output:** Supports human-readable output, `--quiet` filtering, structured `--json` reports, and standard exit codes.
- **Inspectable translation:** Emits the generated TLA⁺ modules and model configuration via `tanny translate`.

## Installation

Prebuilt wheels are published to PyPI for Linux (`x86_64`, `aarch64`) and macOS (`arm64`, `x86_64`), requiring Python 3.10 or later.

Install globally with [uv](https://docs.astral.sh/uv/):

```sh
uv tool install tanny
```

Or run without installing:

```sh
uvx tanny check examples/
```

Or install into a Python environment with `pip`:

```sh
pip install tanny
```

When installed in a Python environment, `tanny` can also be invoked as a module:

```sh
python -m tanny check examples/
```

### Building from source

Building from source requires a Rust toolchain (1.98 or later) and a C11 compiler for the Manyan submodule:

```sh
git clone --recursive https://github.com/soasme/tanny.git
cd tanny
cargo build --release
```

The compiled binary is written to `target/release/tanny`.

## Quick Start

Run `tanny check` on one or more Python files or directories containing `temporal_typing.Model` definitions:

```sh
tanny check examples/
```

Example output:

```text
PASS examples/bank_account.py::small_account_model  884 states; deposit.amount ∈ 1..3, withdraw.amount ∈ 1..3; bounded by state_constraint
FAIL examples/die_hard.py::impl_model  invariant not_solved
    1. init: small=0, big=0
    2. fill_big(): small=0, big=5
    3. pour_big_to_small(): small=3, big=2
    4. empty_small(): small=0, big=2
    5. pour_big_to_small(): small=2, big=0
    6. fill_big(): small=2, big=5
    7. pour_big_to_small(): small=3, big=4
```

Each `Model(...)` discovered in the input paths receives one verdict (`PASS`, `FAIL`, or `ERROR`).

## Usage

`tanny` provides two subcommands: `check` and `translate`.

### `tanny check`

Check every `Model` in the given Python files and directories:

```sh
tanny check [OPTIONS] <PATHS>...
```

| Option | Description |
| --- | --- |
| `--root <DIR>` | Search `<DIR>` for imports before each file's own directory. May be repeated. |
| `-m, --model <NAME>` | Check only models matching `<NAME>`. May be repeated. |
| `--json` | Emit verdicts and diagnostics as JSON. |
| `-q, --quiet` | Suppress `PASS` verdicts in text output. |
| `--workers <N>` | Number of worker processes expanding states per check (default: `1`). |

Examples:

```sh
# Search src/ for imports when checking specs under tests/specs/
tanny check --root src/ tests/specs/

# Check a single model with 4 workers
tanny check -m impl_model --workers 4 examples/die_hard.py

# Emit machine-readable JSON output for CI
tanny check --json examples/
```

### `tanny translate`

Print the TLA⁺ modules and `Main.cfg` configuration generated for a specific model:

```sh
tanny translate [--root <DIR>] <PATH> <MODEL>
```

Example:

```sh
tanny translate examples/die_hard.py impl_model
```

## Verdicts and Exit Codes

`tanny check` assigns one verdict per model as defined in the [checker specification](https://github.com/soasme/temporal-typing/blob/main/docs/checker.md):

| Verdict | Meaning |
| --- | --- |
| `PASS` | Every obligation holds for all reachable states within the model's bounds. Prints the distinct state count, parameter domains, state constraint, and symmetry sets. |
| `FAIL` | A specification obligation was violated. Prints the failure kind, failing clause or action name, detail, and a counterexample trace in Python terms. |
| `ERROR` | The model could not be checked (invalid declaration, unsupported construct, vacuous initial state, or internal error). Prints a `file:line` diagnostic. |

**Failure kinds:** `invariant`, `action-invariant`, `property`, `assumption`, `refinement`, `contract`, `deadlock`, `conformance` (method body violating its `Action`, `Ensures`, frame, or return-type `Where`), `assertion` (failing Python `assert`), and `runtime` (evaluation error such as out-of-range indexing).

### Exit codes

| Code | Meaning |
| --- | --- |
| `0` | Every checked model returned `PASS`. |
| `1` | At least one model returned `FAIL` (and no model returned `ERROR`). |
| `2` | At least one model returned `ERROR`, or CLI/translation failed. |

### JSON output

Passing `--json` writes a JSON object containing `verdicts` and top-level `diagnostics`:

```json
{
  "diagnostics": [],
  "verdicts": [
    {
      "detail": "884 states; deposit.amount ∈ 1..3, withdraw.amount ∈ 1..3; bounded by state_constraint",
      "file": "examples/bank_account.py",
      "kind": null,
      "model": "small_account_model",
      "name": null,
      "trace": [],
      "verdict": "PASS",
      "warnings": []
    }
  ]
}
```

## Architecture

`tanny` is organized as a Cargo workspace with six crates:

| Crate | Responsibility |
| --- | --- |
| [`tanny`](https://github.com/soasme/tanny/tree/main/crates/tanny) | Executable entry point; orchestrates checking and formats verdicts and traces. |
| [`tanny-cli`](https://github.com/soasme/tanny/tree/main/crates/tanny-cli) | Command-line interface definitions (`check` and `translate`). |
| [`tanny-frontend`](https://github.com/soasme/tanny/tree/main/crates/tanny-frontend) | Static parser and analyzer (checker spec §1–3, §6) built on `ruff_python_parser`. |
| [`tanny-translate`](https://github.com/soasme/tanny/tree/main/crates/tanny-translate) | Translates Python specifications, method bodies, and models into TLA⁺ (checker spec §4–5). |
| [`manyan`](https://github.com/soasme/tanny/tree/main/crates/manyan) | Safe Rust wrapper for running Manyan in process and decoding results and traces. |
| [`manyan-sys`](https://github.com/soasme/tanny/tree/main/crates/manyan-sys) | Raw FFI bindings to Manyan's C API, compiled from the `crates/manyan-sys/manyan` submodule. |

### Translation model

- **Method bodies:** Lowered to pure transformations over a record holding state variables and local bindings; loops are lowered to recursive TLA⁺ operators.
- **Obligations:** Invariants, action invariants, assumptions, and temporal properties map directly to TLA⁺ `INVARIANT`, `ASSUME`, and `PROPERTY` clauses. Operational obligations—conformance of a method body to its `Action`, `Ensures`, frame (`Modifies`), return-type `Where`, Python `assert`, and runtime errors—are encoded as tagged `Assert` checks so counterexamples identify the exact failure kind.
- **Refinements:** Translated using a TLA⁺ `INSTANCE` of the abstract specification's module under the declared mapping, verifying initial states, steps, fairness clauses, invariants, and temporal properties individually.

## Development

Clone the repository with submodules (which pin the Manyan C engine and the `temporal-typing` conformance suite):

```sh
git clone --recursive https://github.com/soasme/tanny.git
cd tanny
```

Common tasks are provided via the `Makefile`:

```sh
make check        # Run formatting, clippy, unit tests, and the conformance suite
make lint         # Run cargo fmt --check and cargo clippy (-D warnings)
make test         # Run cargo test --workspace
make conformance  # Run scripts/conformance.py against temporal-typing's suite and examples
make release      # Build the release binary
```

To run `tanny` through `uv` against the bundled examples:

```sh
uv run tanny check tests/temporal-typing/examples
```

### Conformance suite

`tanny` is tested against [temporal-typing](https://github.com/soasme/temporal-typing)'s official checker conformance suite (`tests/temporal-typing/tests/checker` and `tests/temporal-typing/examples`, pinned as a Git submodule). [`scripts/conformance.py`](https://github.com/soasme/tanny/blob/main/scripts/conformance.py) verifies every expected verdict and warning against `tanny check --json`.

See [CHANGELOG.md](https://github.com/soasme/tanny/blob/main/CHANGELOG.md) for release history.

## Documentation

- [Manual](https://github.com/soasme/tanny/blob/main/docs/manual.md) — installation, CLI reference, import resolution, verdicts, diagnostic codes, JSON schema, exit codes, and CI setup.
- [Architecture](https://github.com/soasme/tanny/blob/main/docs/architecture.md) — workspace crates, front-end pipeline, Python-to-TLA⁺ translation (`Py.tla`, body lowering, tagged `Assert`s, refinements, contracts), and conformance testing.
- [Manyan Rust Crates](https://github.com/soasme/tanny/blob/main/docs/manyan-crate.md) — embedding Manyan in process from Rust via `manyan` and `manyan-sys`.
- [Checker Specification](https://github.com/soasme/temporal-typing/blob/main/docs/checker.md) — upstream `temporal-typing` specification defining what a checker must accept, reject, check, and report.

## Related Projects

- [temporal-typing](https://github.com/soasme/temporal-typing) — Temporal specification annotations for Python and the checker specification.
- [Manyan](https://github.com/soasme/manyan) — Fast, embeddable TLA⁺ model checker written in C11.

## License

MIT License. See [LICENSE](https://github.com/soasme/tanny/blob/main/LICENSE) for details.

