# heki

> A static checker for Python, written in Rust, that follows calls across files and enforces the boundaries your project declares.

## Quick reference

| Command | What it does |
| --- | --- |
| `heki check` | Check Python files or directories for blocking calls, mutations, imports, and broken contracts |
| `heki check --fix` | Apply `safe` + `unconditional` fixes (`OPT101`/`OPT102`) |
| `heki check --diff` | Preview fixes as a unified diff without writing files |
| `heki check --touched` | Show and fail on only the diagnostics in one of these files or with a hop of their call chain (`related`) in one, such as the files a commit or a pull request changed, and let `--fix` and `--diff` rewrite only these files; the analysis still covers PATHS (put PATHS before the option) |
| `heki check --baseline` | Ratchet baseline TOML file (line-number-independent) |
| `heki check --heuristic` | Also run the heuristic rules, whose reports can be wrong about the code, by default and for every prefix and `ALL` (`lint.heuristic = true`) |
| `heki check --output-format` | Output format: `full`, or `github` in GitHub Actions (`GITHUB_ACTIONS=true`); `HEKI_OUTPUT_FORMAT` sets it when the option is not given |
| `heki server` | Start the incremental Language Server Protocol (LSP) server |
| `heki rule` | Explain a rule, its Proof Obligations, and counterexamples (`ruff rule` style) |
| `heki snapshot` | Manage interprocedural effect summary snapshots (`MUT103`) |
| `heki assumptions` | List Assumption A1 hazards (`eval`, `exec`, `locals()`, frame introspection, functions too large to model) in a project |
| `heki inspect` | Experimental: what heki knows about one function (its effects, what it changes, keeps, and returns, whether it is pure, the functions it calls, its callers, reports, and contracts) |
| `heki recommend` | Experimental: advice on the configuration of a project, with counts from its analysis, and the keys to add (it writes no file) |
| `heki init` | Set up heki in a repository: a configuration file with the keys `heki recommend` proposes commented out, a Ratchet baseline, and the coding agents' skill and instructions, from a few questions or the options |
| `heki skill install` | Copy the Agent Skill this heki was built with into the coding agent's skills directory |
| `heki version` | Print the version of this heki, and with `--changelog` what this release changed |

| Exit code | Meaning |
| --- | --- |
| `0` | no diagnostic that fails the run (warnings and heuristic reports are reported without failing it) |
| `1` | an exact `error` diagnostic, or any diagnostic with `--fail-on warning` (`lint.fail-on`), among the Ratchet regressions when a baseline is used (or a refused `--update-baseline`, or pending `--diff` changes) |
| `2` | configuration / I/O / syntax errors |

Contract tables go in `heki.toml`, or under `[tool.heki]` in `pyproject.toml`.

| Contract table | What it declares |
| --- | --- |
| `write-owners` | who may write an attribute (`MUT201`; `MUT205` with `unresolved = "report"`) |
| `read-owners` | who may read an attribute (`MUT203`; `MUT206` with `unresolved = "report"`) |
| `call-owners` | who may call a function or construct a class (`MUT204`; `MUT207` with `unresolved = "report"`) |
| `effect-boundaries` | what a scope must not reach: I/O, writes, reads, or calls (`EFF201`) |
| `readonly-returns` | results every caller shares (`MUT202`) |
| `invalidation` | writes that must be announced (`MUT208`) |
| `import-boundaries` | which modules a module may import (experimental) (`IMP201`; `IMP202` with `unresolved = "report"`) |
| `path-boundaries` | which files below a directory a scope must not read, copy, or list (experimental) (`EFF202`) |

## Docs

- [Overview](https://heki.koxudaxi.dev/): heki (壁, Japanese for "wall") is a static checker for Python, written in Rust
- [Installation](https://heki.koxudaxi.dev/installation/): heki is a single executable: its Python package ships only that executable and installs no Python dependencies into your environment
- [Getting Started](https://heki.koxudaxi.dev/getting-started/): This page runs heki on a project, reads a diagnostic, applies a fix, and picks the rules
- [Rules](https://heki.koxudaxi.dev/rules/): heki has 56 rules in four families
- [Design Philosophy](https://heki.koxudaxi.dev/philosophy/): heki asks nothing of your code and never runs it
- [Rule Precision](https://heki.koxudaxi.dev/precision/): heki reports a claim only when it can back it
- [Scope and Limitations](https://heki.koxudaxi.dev/scope/): This page says what heki checks, what it leaves to other tools, where its knowledge comes from, and what it does when it cannot tell
- [Experimental Features](https://heki.koxudaxi.dev/experimental/): Experimental features can still change in behavior or precision, or be removed, in any release, and each change is listed in the release notes
- [Development](https://heki.koxudaxi.dev/development/): This page covers building and testing heki, its checks against CPython, the memory budget, the documentation, releases, and the crates

## Features

- [State Ownership and Mutation](https://heki.koxudaxi.dev/ownership/): heki finds in-place changes of read-only parameters, cached results, and frozen fields, and enforces your own rules about who may change, read, or call what
- [Async and Execution Effects](https://heki.koxudaxi.dev/effects/): heki finds blocking calls in async code, database queries and clients in loops, and lock and task hazards, keeps scopes free of an effect with effect-boundaries and away from the files below a directory with path-boundaries (experimental), and reports functions that gained an effect with heki snapshot
- [Imports and Module Boundaries](https://heki.koxudaxi.dev/imports/): heki finds import cycles, import-order crashes, and test or development code loaded by a package, enforces import-boundaries contracts, and shows what a module loads with heki inspect --imports (all experimental)
- [Proven Rewrites (Performance)](https://heki.koxudaxi.dev/fixes/): heki check --fix rewrites an OPT finding only when heki proves that the rewrite keeps what the program does
- [Configuration Advice](https://heki.koxudaxi.dev/recommend/): heki recommend analyzes a project as heki check does and prints advice on its configuration, with counts from the project and the keys to add to [tool.heki]
- [Coding Agent Guardrails](https://heki.koxudaxi.dev/coding-agents/): heki gives coding agents (Claude Code, Codex, Cursor, and similar) the checks it gives people: every diagnostic says what was proven, where the evidence is, and what blocks a fix. heki inspect says what one function does, so an agent does not read its callees to find out

## Usage

- [Configuration & Contracts](https://heki.koxudaxi.dev/configuration/): heki reads [tool.heki] from pyproject.toml, or a heki.toml / .heki.toml file with the same keys without the tool.heki prefix
- [Adopting heki in a Code Base](https://heki.koxudaxi.dev/adoption/): An existing code base usually has findings you cannot fix at once
- [CLI Reference](https://heki.koxudaxi.dev/cli-reference/): Exit codes: 0 no diagnostic that fails the run (warnings and heuristic reports are reported without failing it), 1 an exact error diagnostic, or any diagnostic with --fail-on warning (lint.fail-on), among the Ratchet regressions when a baseline is used (or a refused --update-baseline, or pending --diff changes), 2 configuration / I/O / syntax errors
- [Output Formats](https://heki.koxudaxi.dev/output-formats/): Every JSON format heki writes has a JSON Schema in the repository, so that CI, governance tools, and coding agents can check what they read

## Integrations

- [Editors (LSP)](https://heki.koxudaxi.dev/lsp/): heki server (alias heki lsp) is a Language Server Protocol server on stdin / stdout: it shows heki's diagnostics and fixes while you edit, with the results heki check reports

## Default rules

- [async-blocking-call (EFF101)](https://heki.koxudaxi.dev/rules/async-blocking-call/): A call in async def that always runs a network, database, subprocess, or sleep call that waits on the event loop; its heuristic reports, a call that can reach a blocking call
- [cached-result-mutation (MUT114)](https://heki.koxudaxi.dev/rules/cached-result-mutation/): Changing in place the list, dict, or set that a functools.lru_cache / functools.cache function returned (the cache hands the same object to later calls with the same arguments), directly or through the functions that return it
- [change-lost-on-new-object (MUT119)](https://heki.koxudaxi.dev/rules/change-lost-on-new-object/): An in-place change of a builtin container applied to a new object that nothing else holds and no name keeps (list(xs).sort(), env = os.environ.copy().update(...), obj.settings[k] = v with a getter returning a new dict): the change is lost when the statement ends
- [generator-never-iterated (EFF116)](https://heki.koxudaxi.dev/rules/generator-never-iterated/): A call of a generator function whose generator nothing iterates (a statement, a value only tested for truth, a function handed to a thread, an atexit handler, or a filter predicate), so its body does not run
- [list-quadratic-concat (OPT101)](https://heki.koxudaxi.dev/rules/list-quadratic-concat/): Unconditionally profitable list quadratic concatenation (x = x + [e] -> x.append(e))
- [lock-reacquired-while-held (EFF109)](https://heki.koxudaxi.dev/rules/lock-reacquired-while-held/): A non-reentrant lock (threading.Lock, asyncio.Lock) taken again, directly or through a call, while it is held (the thread or task waits for itself)
- [loop-carried-concat (DATA101)](https://heki.koxudaxi.dev/rules/loop-carried-concat/): Loop-carried quadratic pandas.concat / numpy.vstack / numpy.append accumulation
- [loop-db-query (EFF103)](https://heki.koxudaxi.dev/rules/loop-db-query/): A loop over data whose every iteration sends a database statement; its heuristic reports, a loop that can run a database query on every iteration
- [loop-unpooled-client (EFF102)](https://heki.koxudaxi.dev/rules/loop-unpooled-client/): A loop over data whose every iteration makes an unpooled HTTP/DB/RPC client or a sessionless request; its heuristic reports, a loop that can make one on every iteration
- [nested-loop-equijoin (OPT113)](https://heki.koxudaxi.dev/rules/nested-loop-equijoin/): Size-dependent nested-loop equi-join (for a in A: for b in B: if a.k == b.k) -> dict index
- [readonly-param-mutation (MUT110)](https://heki.koxudaxi.dev/rules/readonly-param-mutation/): Handing a Sequence / Mapping / AbstractSet / ReadOnly parameter, as itself, to a function whose body (or a helper's) changes it in place at a named line
- [set-dict-quadratic-merge (OPT102)](https://heki.koxudaxi.dev/rules/set-dict-quadratic-merge/): Unconditionally profitable set/dict quadratic merge ({**d, ...} with d first)

The [rule catalog](https://heki.koxudaxi.dev/rules/) lists every rule, and `heki rule <CODE>` prints the page of one.
