# Three-layer wiring

Layer 1 (while coding): the editorconfig pack and the frontend lint pack.
Layer 2 (commit + CI): this folder — deterministic, zero-token checks.
Layer 3 (PR review): SonarQube gate plus the stack_code_review skill,
scoped by its Layer discipline section to judgment findings only.

## Hooks by moment

Hooks are thin dispatchers — one small script per git moment, all logic in
`tools/zh.py` (the same granular/composite split as the commands):

| Hook | Runs | Purpose |
|---|---|---|
| `pre-commit` | `zh check --staged` | Fast staged checks: secrets, guide tags, format/lint on staged files, health when pins change |
| `commit-msg` | `zh msg` | SG-GIT-02 Conventional Commits (`ZH_MSG=warn` to only warn, `off` to skip) |
| `pre-push` | `zh pr --skip test` | Full check + build before anything leaves the machine (`ZH_PREPUSH_TESTS=1` adds tests) |
| `post-merge` | advisory | Warns when dependency files changed so restore/`npm ci` is not forgotten |

`ZH_HOOKS=off` skips the blocking hooks for one command. Hooks are a local
mirror, never the authority: `--no-verify` exists, so CI remains the hard
gate. Install with `git config core.hooksPath .githooks` (zh adopt does
the copying).

## Adopt

1. Copy `.githooks/pre-commit`, `.gitleaks.toml`, `.hadolint.yaml`,
   `.github/actionlint.yaml`, `.github/workflows/quality.yml` into the repo.
2. Copy `validate_guide_tags.py` and `check_package_health.py` into
   `tools/`, plus the generalized guide as `tools/GENERALIZED_STYLE_GUIDE.md`
   (the validator's default search path) or pass `--generalized`.
3. Enable the hook: `git config core.hooksPath .githooks`.
4. Install local tools: gitleaks, actionlint, hadolint (versions pinned in
   the workflow: gitleaks 8.30.1, actionlint 1.7.12, hadolint 2.15.1 —
   checked 2026-12-02). Missing tools are skipped locally with a hint;
   CI always enforces.
5. Set repo secrets: `NUGET_PRIVATE_FEED_API_KEY`, and `SONAR_TOKEN` when
   the Sonar step is enabled. Actions are pinned by commit SHA
   (checkout v7.0.1, setup-dotnet v6.1.0, setup-node v7.1.0).
6. Performance budgets (SG-FE-PERF-01): `budget.json` holds starter
   Lighthouse budgets (LCP 2.5s, CLS 0.1, TBT 200ms, script 300KB);
   wire with Lighthouse CI per route and treat breaches as findings.

## What runs where

| Check | Commit hook | CI |
|---|---|---|
| gitleaks (staged / full history) | staged | full |
| Style-guide tag validator | when guide staged | always |
| Health gate | when pins change | always |
| dotnet format verify | staged .cs | whole solution |
| ESLint / Stylelint | staged files | whole app |
| actionlint + S7636 grep | when workflows staged | always |
| hadolint (SG-BUILD-06) | when Dockerfile staged | always |
| Build + tests + locked restore | — | always |
| npm audit high/critical | — | always |

Verified 2026-12-02: actionlint clean on quality.yml; hadolint flags
root/unpinned Dockerfiles and passes the non-root sample; gitleaks
catches a staged secret; the hook blocks an expired bypass and a staged
secret, and passes a clean commit. (The sandbox git wrapper disables
repo hooks, so the hook script was exercised directly — same code path.)
