Metadata-Version: 2.4
Name: punt-oo-ratchet
Version: 0.8.0
Summary: Must-improve OO quality ratchet for Python repos: OO metrics, coupling/cohesion, and lint-suppression count gates.
Author: Punt Labs
License-Expression: MIT
Requires-Dist: openai>=2,<3
Requires-Dist: punt-observe==0.1.1
Requires-Python: >=3.13
Project-URL: Homepage, https://github.com/punt-labs/python-oo-ratchet
Project-URL: Repository, https://github.com/punt-labs/python-oo-ratchet
Project-URL: Bug Tracker, https://github.com/punt-labs/python-oo-ratchet/issues
Description-Content-Type: text/markdown

# oo-ratchet

> Quality gates for Python code that fail when a change makes it worse.

[![License](https://img.shields.io/github/license/punt-labs/python-oo-ratchet)](https://github.com/punt-labs/python-oo-ratchet/blob/main/LICENSE)
[![CI](https://img.shields.io/github/actions/workflow/status/punt-labs/python-oo-ratchet/ci.yml?label=CI)](https://github.com/punt-labs/python-oo-ratchet/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/punt-oo-ratchet)](https://pypi.org/project/punt-oo-ratchet/)
[![Python](https://img.shields.io/badge/python-%E2%89%A53.13-blue)](https://pypi.org/project/punt-oo-ratchet/)

`oo-ratchet` is a command line tool that runs five checks over a Python
package. Most of them are ratchets: a ratchet compares the code with a
baseline recorded earlier and fails if quality got worse. A baseline is a
JSON file, committed to the repository, that holds the scores of each file.
The tool is the shared copy of gates that vox, quarry, lux and z-spec each
used to keep in their own `tools/` directory.

## Quick Start

The tool needs Python 3.13 or newer.

```bash
uv add --dev punt-oo-ratchet
```

Record today's scores for a package as the first baseline:

```bash
uv run oo-ratchet oo src/pkg/ --update
```

Commit `.punt-labs/oo-ratchet/oo-baseline.json` and merge it to `main`. The check compares your
branch with the merge base of `origin/main` and `HEAD`, so it needs that
baseline on `origin/main` and a fetched `origin/main`. Without them it exits 1
with `cannot resolve merge-base`. On a branch, check a change:

```bash
uv run oo-ratchet oo src/pkg/ --check
```

<details>
<summary>Other ways to install</summary>

With pip:

```bash
pip install punt-oo-ratchet
```

Without installing, with uvx:

```bash
uvx --from punt-oo-ratchet oo-ratchet --help
```

</details>

<details>
<summary>Verify the install</summary>

```bash
uv run oo-ratchet doctor
```

It checks Python, git, `uv`, Claude Code, this repository's configuration and the
advisor key. It exits 1 when a required check fails.

</details>

## Features

- **OO metrics** --- scores each file on encapsulation, parameter counts,
  complexity, module size and a few conventions, and fails when any score
  gets worse on a touched file or when no score improves anywhere in the change
  (a change that only adds files meeting the targets passes, and so does a
  change whose touched files already meet every target, since there is
  almost nothing left to improve)
- **Coupling and cohesion** --- counts imports between modules, import
  cycles and how little the methods of a class share their fields, and fails
  on any regression
- **Suppression count** --- counts `noqa`, `type: ignore`, `pylint: disable`
  and `pyright: ignore` comments and fails when the total rises
- **Layer rules** --- checks that modules import only from their own layer or
  one nearer the core, with a baseline of known violations that can only
  shrink
- **Design check** --- reports design smells such as classes that are only a
  bag of functions; opt in to fail on a small set of reliable rules
- **Audited exceptions** --- loosening a baseline needs a named command and
  leaves a row in an audit log
- **Fails closed** --- an unreadable baseline, a git error or an unresolvable
  base commit fails the run

## What It Looks Like

A branch adds a class with an explicit `__init__` and one `noqa` comment. The
`oo` gate reports the regressions and exits 1. Rows for metrics that passed
are left out here:

```text
$ oo-ratchet oo src/shop/ --check --base-ref origin/main --require-base
File                                     Metric                       Baseline    Current    Delta     Status
------------------------------------------------------------------------------------------------------------
src/shop/application/checkout.py         encapsulation_ratio             1.000      0.000   -1.000  REGRESSED
src/shop/application/checkout.py         init_violations                 0.000      1.000   +1.000  REGRESSED
src/shop/application/checkout.py         public_attr_violations          0.000      1.000   +1.000  REGRESSED
FAIL: baseline out of date -- run make update-oo: src/shop/application/checkout.py
FAIL: regression detected
FAIL: no metric improved on any touched file
```

The `suppressions` gate fails on the same branch:

```text
$ oo-ratchet suppressions src/shop/ --check --base-ref origin/main --require-base
Baseline total: 0
Current total:  1

FAIL: suppression count increased by 1 (after waivers)

Files with new or increased, unwaived suppressions:
  src/shop/application/checkout.py: +1 unwaived (0 -> 1)
```

## Commands

Run `oo-ratchet <gate> <package-dir> [flags]`. The package directory is the
folder that holds `__init__.py`.

| Gate | What it does | Main flags |
|------|--------------|------------|
| `oo` | Must-improve: no metric regresses on a touched file and at least one improves, unless every touched file already meets all eight targets | `--check`, `--update`, `--relax FILE --justify`, `--rebaseline-files --reason`, `--json` |
| `coupling` | Regression-only: import counts, cycles and cohesion must not get worse | `--check`, `--update`, `--rebaseline-files --reason`, `--json` |
| `suppressions` | Regression-only: the total of lint and type-ignore comments must not rise | `--check`, `--update`, `--rebaseline --reason`, `--relax FILE --justify` |
| `layers` | A baseline of imports that break the layer rules; it may only shrink | `--check`, `--update`, `--suggest`, `--json`, `--allow-config-change` |
| `design` | Advisory report of design smells; exits 1 only with `--enforce` | `--base-ref REF`, `--enforce`, `--layers FILE`, `--json` |
| `goal` | How far the package is from the target in `[tool.oo-ratchet.goal]`, and how it moved over a session | `show [--base-ref REF]`, `summary --since SHA`, `guide`, `--json` |

Flags that apply to most gates:

- `--check` compares the code with the baseline and exits 1 on failure.
- `--update` records the current scores and never loosens them.
- `--base-ref REF --require-base` compares against a git commit and fails if
  it cannot be found. In a pull request, pass the merge-base commit.
- `--tripwire` is for the run after a merge to `main`. The one adoption
  merge, which adds the first baseline, passes.
- `--namespace NAME` scans several directories as one package.

A gate runs in `enforce` mode unless its table says otherwise. With
`mode = "advise"` in `[tool.oo-ratchet.oo]`, `coupling`, `suppressions` or
`layers`, `--check` exits 0 on a measured failure and still exits 1 when it
could not decide (an unresolvable base, a corrupt baseline). The mode on the
base commit binds, so a change cannot turn its own gate off. Any other value
exits 2.

### Goal: Summary and Guide

`oo-ratchet goal summary SRC --since SHA` reports how the code moved since the
commit `SHA`. It checks out `SHA`, and each later commit on the path from `SHA`
to `HEAD` (first parents only), in a throwaway directory, and measures every
target declared in `[tool.oo-ratchet.goal]` there. It prints the gap count of
each check, by level, after each commit; the cause each commit worked, when the
counts show one; any trade (a commit that cleared a cause and added other
gaps); and the net change from `SHA` to the last commit. It writes no file and
leaves your work tree and the repository's refs unchanged. `--json` prints one
JSON object instead.

| Exit | Meaning |
|------|---------|
| 0 | The table was printed |
| 1 | An enforce-mode target is not met at the last commit |
| 2 | `SHA` does not resolve or is not on the first-parent path to `HEAD`, `HEAD` has no goal table, a source directory or `pyproject.toml` is missing at a commit, `--since` was not given, or a checkout failed; the message names the commit |

### Goal: the goalpost guard

`oo-ratchet goal show SRC --base-ref REF` compares the goal tables at the merge
base of `REF` and `HEAD` with the work tree and prints
`WARNING: target changed since <sha>: allow rose: suppressions 0 -> 3` when an
`allow` rose, a target was removed, `enforce` became `advise`, a `design`
target stopped counting a rule, or `trade-cap` rose. `--json` adds `target_changed`. It is a warning: exit codes do not change,
and an unresolvable `REF` exits 2. Details:
[Goal Guard](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/CONFIGURING.md#goal-guard-goal-show---base-ref-ref).

### Goal: the advisor

`oo-ratchet goal propose SRC` asks a language model what to do next. It sees
every finding, even those within an allowance, and chooses the next step or few
steps, large structure first, then plans them with the source open; not a full
plan, with a guessed (never checked) score impact. `--cause ID` plans one cause.
It writes no file; it sends source only with `OPENAI_API_KEY` set. [Goal Advisor](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/CONFIGURING.md#goal-advisor-goal-propose).

### Claude Code plugin

`install.sh` installs the CLI and the plugin, which adds eight slash commands, from
`/oo-ratchet:setup` to the `/oo-ratchet:improve` loop (`oo-ratchet goal guide` prints
its instructions); see the [plugin guide](https://github.com/punt-labs/python-oo-ratchet/blob/main/plugin/README.md).

A ring is a named layer of the package: `entry`, `adapters`, `application` or
`domain`. `design --base-ref REF` scans the code at the merge base and again at the
working tree, then reports which findings were added or removed.
`oo-ratchet <gate> --help` lists every flag.

## Setup

`/oo-ratchet:setup` does a repository's first run. By hand, follow
[Configuring a Repo](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/CONFIGURING.md): gates and their order,
`[tool.oo-ratchet]` settings, layer rules, `Makefile` targets, CI, files to commit.

## Documentation

[Configuring a Repo](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/CONFIGURING.md) |
[Reference](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/REFERENCE.md) |
[Layers Gate](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/LAYERS.md) |
[Design Check](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/DESIGN.md) |
[Formal Model](https://github.com/punt-labs/python-oo-ratchet/blob/main/docs/ratchet.tex) |
[Policy](https://github.com/punt-labs/punt-kit/blob/main/lang-rules/python/python-oo-adoption.md) |
[Contributing](https://github.com/punt-labs/python-oo-ratchet/blob/main/CONTRIBUTING.md) |
[Changelog](https://github.com/punt-labs/python-oo-ratchet/blob/main/CHANGELOG.md)

## License

MIT
