Metadata-Version: 2.5
Name: rulepack
Version: 0.14.0
Summary: Run ast-grep and import-linter checks from shared, versioned rule packs in any repo.
Project-URL: repository, https://github.com/justmatias/rulepack
Author-email: Matias Gimenez <matiasgimenez.dev@gmail.com>
License: MIT
License-File: LICENSE
Requires-Python: >=3.12
Requires-Dist: ast-grep-cli>=0.51
Requires-Dist: pydantic>=2
Requires-Dist: pyyaml>=6
Requires-Dist: tomli-w>=1
Description-Content-Type: text/markdown

# rulepack

Run ast-grep checks from shared, versioned rule packs in any repo.

## Why

Agents write more of the code now, and reviewing it means correcting the same preferences again and again: a naming habit, a test layout, an exception style. If an agent writes the code and you review it, it should write the code the way you would.

Prose rules in Markdown help, but an agent follows them only some of the time. rulepack turns those preferences into static, deterministic checks wherever a check can express them, so a broken rule fails the same way every time and the agent fixes it before you see the diff. Packs make the rules portable: write the checks once in a source repo, version them, and select them from any repo. Each repo pins the commit it runs in a lockfile, so a check changes only when the repo updates the pin.

## Integrate rulepack in your repository

This guide adds shared packs to a repo, runs them on every commit through pre-commit and on every pull request in CI. The examples use the packs [mstack](https://github.com/justmatias/mstack) publishes; any repo or folder with a `packs.toml` works the same way.

### 1. Install the CLI

You need [`uv`](https://docs.astral.sh/uv/) and git.

```bash
uv tool install rulepack
rulepack --version
```

Or run any command without installing, with `uvx rulepack <command>`.

### 2. Set up in one step

From the repo root:

```bash
rulepack init --source mstack=https://github.com/justmatias/mstack@v8.1.0 --pre-commit --ci github
```

`--source NAME=URL@REF` names a pack source, a git repo at a tag, branch or commit (the last `@` separates the URL and the ref). Repeat it for more sources. `init` shows the plan and asks before it writes (`--yes` skips the question, `--dry-run` writes nothing). It never overwrites a file, so a second run changes nothing. It writes:

| File | What it holds |
| --- | --- |
| `.rulepack.toml` | The config: an empty `packs` list and a `[sources.mstack]` table. |
| `rulepack.lock` | The commit each git source's ref resolved to. `init` fetches it into the cache. |
| `.pre-commit-config.yaml` | The rulepack hooks, pinned to the installed version. An existing file gets a new entry in `repos:`. |
| `.github/workflows/rulepack.yml` | A workflow that runs the rulepack action on pushes to `main` and on pull requests. |

Two more flags:

- `--checks-dir lint` also writes `lint/sgconfig.yml` with empty `rules/` and `rule-tests/`, for checks the repo keeps itself. They run next to the packs' checks, and a local check replaces a pack check with the same ID. Without the flag, `init` at a terminal asks whether to keep the repo's own checks in `lint/`.
- `--poe` adds `lint-rules` and `lint-rules-changed` [poe](https://poethepoet.natn.io/) tasks, to `pyproject.toml` or, without one, `poe_tasks.toml`.

### 3. Select packs

List what the sources offer:

```console
$ rulepack packs list
mstack: https://github.com/justmatias/mstack@v8.1.0 (7ed440b6faad0dc8f1fc88640b6bb466b650e768)
  mstack/python-core: no-bare-except, no-isort-off, no-manual-init-boilerplate, ...
  mstack/testing: conftest-has-only-fixtures, no-broad-pytest-raises, ...
```

Select packs as `<source>/<pack>` in `.rulepack.toml`:

```toml
packs = ["mstack/python-core", "mstack/testing"]

[sources.mstack]
git = "https://github.com/justmatias/mstack"
ref = "v8.1.0"
```

Then run every check on the whole repo:

```bash
rulepack lint
```

Commit `.rulepack.toml` and `rulepack.lock` together. `rulepack lint` never writes the lock: it fails and tells you to run `rulepack packs lock` when a git source is missing from the lock or its `git` or `ref` no longer matches the config.

- **Add a source.** Add a `[sources.<name>]` table, then run `rulepack packs lock`. It pins the new source and keeps the existing pins. A source is either a git repo (`git` and `ref`) or a local folder (`path`, relative to the config file). Path sources are not locked.
- **Update a pin.** Change `ref` and run `rulepack packs lock`, or keep the ref and run `rulepack packs update` (every source) or `rulepack packs update mstack` (one) to re-resolve a branch or a moved tag.
- **Adjust a check.** Turn one off or change its severity without leaving the pack:

  ```toml
  [pack_overrides]
  disable = ["no-module-docstring"]
  severity = { no-sleep-in-tests = "warning" }
  ```

### 4. Run on every commit with pre-commit

`rulepack init --pre-commit` adds this entry. pre-commit installs rulepack at that tag in its own environment, so the repo needs nothing else:

```yaml
repos:
  - repo: https://github.com/justmatias/rulepack
    rev: v0.14.0   # a release tag; init writes the installed version
    hooks:
      - id: rulepack-lint    # ast-grep scan of the staged files; warnings fail too
      - id: rulepack-rules   # the rule tests, when a YAML file changes
```

Then install pre-commit, if you don't have it, and the hooks:

```bash
uv tool install pre-commit
pre-commit install
```

rulepack ships no commit-msg hook. Its hooks declare no stage, so they run at every stage you install: if you also install commit-msg hooks, add `default_stages: [pre-commit]` at the top of the config so they run only at the pre-commit stage. The first run fetches the locked commits into the cache, so it needs network access once.

### 5. Run in CI

`rulepack init --ci github` writes this workflow (shortened here). The action runs the full scan, and on pull requests also the scan of the changed files, where warnings fail too. It needs the full history to find the base branch:

```yaml
on:
  push:
    branches: ["main"]
  pull_request:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - uses: justmatias/rulepack@v0.14.0
```

Inputs: `mode` (`full`, `changed` or `both`; the default is `both` on pull requests and `full` otherwise), `base-ref` (default `origin/<the pull request's base branch>`), `rulepack-version` (default the action's own ref), `python-version` and `working-directory`.

On other CI providers, call the CLI directly. The changed-files scan needs the base branch and the history back to the merge base, so check out the full history and fetch the target branch:

```bash
uvx --from git+https://github.com/justmatias/rulepack@v0.14.0 rulepack lint
# On a merge request, also scan the changed files with warnings failing:
uvx --from git+https://github.com/justmatias/rulepack@v0.14.0 rulepack lint --changed --base-ref origin/main
```

### 6. Check agent edits as they happen

`rulepack hook post-edit` scans the files an agent just edited, and `rulepack hook stop` scans the changed files when the agent stops. Both read a Claude Code, Codex or Cursor hook payload on stdin and report findings so the agent fixes them in the same session. In Claude Code, add them to `.claude/settings.json`:

```json
{
  "hooks": {
    "PostToolUse": [
      { "matcher": "Edit|MultiEdit|Write", "hooks": [{ "type": "command", "command": "rulepack hook post-edit" }] }
    ],
    "Stop": [
      { "hooks": [{ "type": "command", "command": "rulepack hook stop" }] }
    ]
  }
}
```

- They run only in repos with a `.rulepack.toml`, and `[hooks] enabled = false` turns them off for a repo.
- Stop reports the same findings only once per session.
- A run that fails or takes longer than `[hooks] timeout` (10 seconds by default) logs a warning and lets the agent continue.
- Hooks read the pack sources from the cache. When a locked commit is not cached yet, the hook fetches it within that time budget, so run `rulepack packs lock` once after cloning to fill the cache up front.

### 7. Migrate from mstack v6

rulepack replaces mstack v6's `mstack lint` and its bundled packs. mstack now publishes those packs as a source.

1. Rename `.mstack.toml` to `.rulepack.toml` (or copy it, if you keep mstack v6 hooks, as in step 5). rulepack ignores `.mstack.toml`.
2. Prefix each pack with its source, and declare the source. A v6 selection such as `packs = ["python-core", "testing"]` becomes:

   ```toml
   packs = ["mstack/python-core", "mstack/testing"]

   [sources.mstack]
   git = "https://github.com/justmatias/mstack"
   ref = "v8.1.0"
   ```

   mstack's `git` pack held prose only and has no checks, so drop it.
3. Keep `sgconfig`, `[scan]`, `[hooks]`, `[changed]` and `[pack_overrides]`. Set `sgconfig = "lint-rules/sgconfig.yml"` if the repo kept mstack's default path, because rulepack's default is `lint/sgconfig.yml`. Delete the keys rulepack does not read (`rules_dir`, `importlinter`, `pre_commit_config`, `[domain_check]` and `[commits]`): unknown keys are an error.
4. Run `rulepack packs lock`, then `rulepack lint`, and commit the config and `rulepack.lock`.
5. In `.pre-commit-config.yaml`, replace `mstack-lint` and `mstack-rules` with `rulepack-lint` and `rulepack-rules` from the entry in [step 4](#4-run-on-every-commit-with-pre-commit). For `mstack-imports`, use import-linter's own pre-commit hook, which needs the project importable in the hook's environment. `mstack-exceptions`, `mstack-domain-check` and `mstack-commit-msg` have no rulepack counterpart: drop them, or keep them from mstack v6 together with a `.mstack.toml` that holds their settings (such as `[commits]` and `[domain_check]`).

## Reference

[docs/reference.md](docs/reference.md) lists every command, flag and configuration key.

## Contributing

Bug reports, ideas and pull requests are welcome. [CONTRIBUTING.md](.github/CONTRIBUTING.md) covers the setup, the checks to run and how a pull request title decides the release. Report security problems privately, as [SECURITY.md](.github/SECURITY.md) describes, and follow the [Code of Conduct](.github/CODE_OF_CONDUCT.md).

## License

MIT. See [LICENSE](LICENSE).
