# Add Pre-commit Hooks to a Reflex App

Yes. An agent reads the checks your CI already runs on a Reflex app, mirrors them as pre-commit hooks that run on each commit, runs them on the tree and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-pre-commit-hooks/reflex/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the checks that already exist before it adds a hook. In a Reflex app like reflex-dev/reflex-chat, `.github/workflows/ci.yml` runs three jobs on pushes and pull requests to `main`: `check-pyright` installs `requirements.txt` and `pyright` with `uv` on Python 3.10 and runs `pyright .`; `check-ruff` runs `astral-sh/ruff-action`, which is `ruff check` with no config, because the repository has no `pyproject.toml` or `ruff.toml`; and `check-export` runs `reflex init` and `reflex export` with `OPENAI_API_KEY` set to `dummy`, since `chat/state.py` raises at import when the variable is unset. `requirements.txt` holds two lines, `reflex>=0.7.11` and `openai>=1.78.1`; `rxconfig.py` names the app `chat` and adds the sitemap plugin; the code is four modules under `chat/`, with `state.py` at 170 lines and the two components at just over a hundred each. There is no `.pre-commit-config.yaml`, no dev requirements file and no contributing section in the README beyond fork and open a pull request.

The hooks mirror CI rather than invent a second standard. The agent writes `.pre-commit-config.yaml` with three repositories: `pre-commit/pre-commit-hooks` for trailing whitespace, end-of-file newlines, YAML syntax and large files; `astral-sh/ruff-pre-commit` with the `ruff` hook and `--fix`, pinned to the release `ruff-action` resolves on the day the run happens, so a commit is checked by the same rules the workflow applies; and a `local` hook that runs `pyright` with `language: system`, because pyright needs the app's own `reflex` and `openai` installed to resolve imports, and an isolated hook environment would have to reinstall both. Before adding `ruff-format` it runs `ruff format --check` over the tree: if the files already match, the format hook goes in beside the check hook; if they do not, it stays out, and the pull request says how many files a format pass would touch so the team can take that as its own change.

Then it runs everything the way a contributor would. It creates a virtual environment, installs `requirements.txt`, adds a `requirements-dev.txt` with `pre-commit`, `ruff` and `pyright`, runs `pre-commit install` and `pre-commit run --all-files`, and reads what the hooks report. Whitespace and newline fixes the hooks make themselves go into the commit; a ruff finding that `--fix` cannot resolve is fixed by hand or, if it is a rule the team may not want, listed in the pull request with the file and line. Pyright must come out the same as CI, so the run compares its output with the `check-pyright` job's command.

Last it adds a short Contributing section to `README.md` with the three commands, `pip install -r requirements-dev.txt`, `pre-commit install` and `pre-commit run --all-files`, and leaves `ci.yml` as it is, noting in the pull request that a `pre-commit/action` step could replace the two lint jobs once the team is happy with the hooks.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add pre-commit hooks to this Reflex app. Read .github/workflows/ci.yml, requirements.txt, rxconfig.py and the chat/ package first, and list the checks CI runs. Write .pre-commit-config.yaml that mirrors them: the standard pre-commit-hooks for whitespace, end of file, YAML and large files; the ruff hook pinned to a current release with --fix; and a local system-language hook that runs pyright so it sees the installed reflex and openai packages. Run ruff format --check first and add the format hook only if the tree already passes; otherwise report how many files it would change. Add a requirements-dev.txt with pre-commit, ruff and pyright. Create a virtual environment, install everything, run pre-commit install and pre-commit run --all-files, commit the fixes the hooks make, and resolve or list any remaining findings with file and line. Add a Contributing section to the README with the setup commands. Do not change ci.yml. Open a pull request that quotes the final pre-commit run.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with `.pre-commit-config.yaml`, `requirements-dev.txt`, the README section and whatever whitespace or newline fixes the hooks applied on the first run. Its description quotes the output of `pre-commit run --all-files` with every hook's name and status, states whether the format hook was included and why, and lists any ruff finding it left for the team with its file and line.

A reviewer checks that the ruff and pyright hooks give the same answer as the `check-ruff` and `check-pyright` jobs, which the quoted output beside the workflow shows, and that the pinned ruff release is the one they want to stay on. Merging changes nothing in CI; each contributor runs `pre-commit install` once and the checks move to commit time.

## Run it on every terminal hand-off

A maintainer who keeps fixing lint in review starts this from the terminal with this body and the repository in `resources`, and goes back to their own work. The run reads the real workflow and the real tree, so the hooks it writes match the checks the team already passes rather than a generic list.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. When the turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and a follow-up to the same session can ask it to add the format hook with the reformatting commit, or to swap the two lint jobs in `ci.yml` for a `pre-commit/action` step.

## Why run it on Rush?

The work in this job is not the YAML, which is short, but the first full run of the hooks: installing the app's packages so pyright resolves `reflex`, matching the ruff release CI uses, and reading what the hooks find in the real files. The agent does that in its own sandbox and keeps the complete `pre-commit run --all-files` output in the run's record, so the pull request shows the state the tree is in rather than promising one.

The same run fits any Python app with a lint workflow: a Reflex app with a `pyproject.toml` and ruff settings, a FastAPI service with mypy instead of pyright, a package with a test hook. Each gets hooks derived from its own CI and a first run against its own code.

## Frequently asked questions

### Why is pyright a local hook instead of the mirror repository?

The mirror runs pyright in its own environment, where `reflex` and `openai` are not installed, so every import resolves to unknown and the check is useless unless both are listed as hook dependencies and reinstalled on each update. A `language: system` hook runs the pyright in your virtual environment, the same way the CI job installs it next to the app.

### Will it reformat my code?

Only if the tree already passes `ruff format --check`, in which case the format hook is added and changes nothing today. If files would change, the hook is left out and the pull request says how many, so reformatting can be its own commit rather than hidden in this one.

### Does it change the GitHub Actions workflow?

No. `ci.yml` keeps its three jobs, so pull requests from contributors who skip the hooks are still checked. The pull request notes that a single `pre-commit/action` step could replace the ruff and pyright jobs later.

### Why pin the ruff hook when CI floats?

`ruff-action` with no version runs the latest release on each CI run, so a new rule can appear in CI before anyone has seen it locally. A pinned hook makes commits deterministic; `pre-commit autoupdate` moves it when the team chooses, and the pull request names the release it pinned.

## More Reflex jobs

- [Add a SQLite Database to a Reflex App](https://getrush.ai/run/add-sqlite-database/reflex/)
- [Write Integration Tests for a Reflex App](https://getrush.ai/run/write-integration-tests/reflex/)
- [Write Pytest Tests for a Reflex App](https://getrush.ai/run/write-pytest-tests/reflex/)

## The same job on other stacks

- [Add Pre-commit Hooks With fmt, validate, tflint and terraform-docs to a Terraform Repo](https://getrush.ai/run/add-pre-commit-hooks/terraform/)
- [Add Pre-commit Hooks to a Textual App](https://getrush.ai/run/add-pre-commit-hooks/textual/)
- [Add pre-commit Hooks to an Ansible Role](https://getrush.ai/run/add-pre-commit-hooks/ansible/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
