---
name: pre-commit-check
description: 'Pre-commit readiness check. Runs and auto-fixes all lefthook pre-commit checks: BiomeJS lint/format (UI), Ruff lint/format (API), TypeScript type-check (UI + studio), and unit tests (pytest/Jest). USE FOR: preparing code for commit; fixing lint errors; fixing type errors; fixing test failures before pushing; green-lighting a branch; pre-push check. Run this before committing or opening a PR.'
argument-hint: 'Optional: path or package to scope (e.g. "ui", "api", "studio")'
---

# Pre-Commit Check

Mirrors the lefthook hooks exactly, then auto-fixes anything fixable, then reports remaining issues that require manual attention.

## What This Skill Covers

| Hook | Tool | Scope | Auto-fixable? |
|------|------|-------|--------------|
| pre-commit | BiomeJS `check --write` | `ui/` TS/TSX/JS/CSS | ✅ Yes |
| pre-commit | Ruff `check --fix` + `format` | `api/` Python | ✅ Yes |
| — | TypeScript `tsc --noEmit` | `ui/`, `studio/` | ❌ Manual fix required |
| pre-push | pytest | `api/` | ❌ Manual fix required |
| pre-push | Jest | `ui/` | ❌ Manual fix required |

---

## Procedure

### Step 1 — Identify scope

Determine which packages have changed. If an argument was supplied, scope to that package only. Otherwise run all checks.

```bash
# See what's changed (staged + unstaged)
git status --short
```

### Step 2 — UI: BiomeJS lint + format (auto-fix)

Run BiomeJS in write mode so it auto-fixes everything it can:

```bash
cd ui && npx biome check --write .
```

Then verify no remaining errors:

```bash
cd ui && npx biome check .
```

If BiomeJS still reports errors after `--write`, they require manual intervention. Read the error output carefully — unsafe fixes (e.g. logic-changing rewrites) are intentionally not auto-applied. Fix them manually, then re-run the check.

### Step 3 — API: Ruff lint + format (auto-fix)

```bash
# Auto-fix all safe lint violations
cd api && uv run ruff check --fix .

# Auto-format
cd api && uv run ruff format .
```

Then verify clean:

```bash
cd api && uv run ruff check . && uv run ruff format --check .
```

If `ruff check` still reports violations after `--fix`, they are unsafe to auto-fix. Read the error messages, fix them manually, then re-run.

### Step 4 — TypeScript type-check (UI)

```bash
cd ui && npx tsc --noEmit
```

TypeScript errors must be fixed manually. Common patterns:
- Missing type annotations or incorrect types — add explicit types
- Import errors — verify the module exists and is exported
- `any` in strict mode — replace with proper types

Re-run until `tsc --noEmit` exits with code 0.

### Step 5 — TypeScript type-check (Sanity Studio)

```bash
cd studio && pnpm type-check
```

The pre-existing error in `sanity.cli.ts` (missing `vite` types) is known and can be ignored if it is the **only** error. Any new errors must be fixed.

### Step 6 — Backend unit tests

```bash
cd api && uv run --extra dev pytest
```

If tests fail:
1. Read the failure output to identify which test(s) failed
2. Determine whether the failure is in the test itself or in the implementation
3. Fix the root cause — do **not** delete or skip tests to make them pass
4. Re-run pytest until all tests pass

### Step 7 — Frontend unit tests

```bash
cd ui && pnpm test -- --passWithNoTests
```

> **Note:** Jest is not yet fully configured in `ui/package.json`. If the test script outputs a placeholder message, this step is a no-op for now.

### Step 8 — Final verification

Run the full suite one more time to confirm a clean state:

```bash
# UI — lint
cd ui && npx biome check .

# API — lint
cd api && uv run ruff check . && uv run ruff format --check .

# UI — types
cd ui && npx tsc --noEmit

# API — tests
cd api && uv run --extra dev pytest

# UI — tests
cd ui && pnpm test -- --passWithNoTests
```

All commands must exit with code 0 before the branch is commit-ready.

---

## Reporting

After completing the procedure, report a status table:

| Check | Status | Notes |
|-------|--------|-------|
| BiomeJS lint/format | ✅ / ❌ | List any remaining manual-fix items |
| Ruff lint/format | ✅ / ❌ | List any remaining manual-fix items |
| TypeScript — UI | ✅ / ❌ | Count of errors |
| TypeScript — Studio | ✅ / ⚠️ | Pre-existing vite error is OK |
| pytest | ✅ / ❌ | Passed / total count |
| Jest | ✅ / ⚠️ | Note if not yet configured |

If anything is ❌, describe exactly what needs to be fixed and where.

---

## Quick Reference — Exact Lefthook Commands

These are the real commands run by lefthook (from `lefthook.yml`):

```bash
# pre-commit: UI (run from ui/)
npx biome check {staged_files}

# pre-commit: API (run from api/)
uv run ruff check {staged_files} && uv run ruff format --check {staged_files}

# pre-push: UI tests
cd ui && pnpm test -- --passWithNoTests

# pre-push: API tests
cd api && uv run --extra dev pytest
```

This skill intentionally runs on the **full package** (not just staged files) to avoid a situation where unstaged changes cause failures after commit.
