# Code Health — a Rubric and a Scored Scan
<!--
ref: @protocol/code-health
tags: [code-quality, rubric, standards, scan, recurring, score]
triggers: [code health, score the codebase, are my coding standards being followed, code quality scan, rescan code health, before and after score]
trigger_prompt: When the operator asks whether a codebase follows his coding standards, or a recurring code-health scan comes due, load this for the rubric, the scoring formula and how to report a before and after.
version: 2
updated_at: 2026-11-11T07:24:08.446Z
-->
The operator ships across many projects without reading a code file. This is how he finds out whether the code still follows his standards: **a rubric (below), a scoring procedure, and a recurring impulse that rescans and reports before and after.** It is a protocol and an impulse, not a tool. The rubric is the description of what he wants. The agent that picks it up chooses how to check each line (its own greps, existing linters, a script it writes and keeps beside the report) and may learn from what it finds. Do not build a fixed scanner; if a check turns out useful, record its command on the rubric line below.
Ancestor: [`@codebase/tech-debt-agent`](@codebase/tech-debt-agent) finds *drift* (two patterns coexisting) by grep. This protocol keeps that recipe and adds what it lacks: a fixed rubric, a number per line, and a before and after. For a protocol entry (not code), see #70 protocol-qc. Same idea, different subject.
## Scoring (the same every run, or before and after mean nothing)
1. Pin the commit: scan `origin/main` of each repo and record the short SHA. Never score a dirty working tree; other sessions' hunks are not the codebase.
2. Each line below has a **check** and a **formula**. Two shapes: *ratio* `100 × (1 − violating / population)`, and *density* `100 × max(0, 1 − per-kLOC / limit)`. Round to a whole number.
3. **Score = weighted mean** of the lines that could be measured. A line you could not measure is reported `n/m` and left out of the mean. It is never scored 100. A line whose check returned nothing is suspect until you have run its check on a known violation (positive control) and seen it fire.
4. Every scored line carries evidence: the count, the population, and at least three `file:line` examples (all of them when there are fewer). A number without a `file:line` is not a finding.
5. Legacy the standard itself marks as queued for cleanup is counted and **listed separately**, not silently exempted.
## The rubric
Weights: **1** = a rule the operator or the project wrote down; **0.5** = a calibration default of mine that he has not ruled on (marked ⚖; change it here, not in a report).
### From the project's own §Code standards (`CLAUDE.md`, ruled in PR review #67)
| id | rule | check | formula | w |
|---|---|---|---|---|
| H1 | The database is declarative: no stored functions, no triggers | Live: `select count(*) from pg_trigger where not tgisinternal` and user functions in `public`. Historical: migrations that `CREATE FUNCTION/TRIGGER` | ratio over live objects; historical files listed as info | 1 |
| H2 | ID is the only addressing contract: no parallel `…/key/:key` routes | grep route decorators for `/key/`, `by-slug`, `by-key` | ratio over routes | 1 |
| H3 | Convention over annotation: no `@Column({ name })` the naming strategy already produces | for each explicit `name:` compare to `snake_case(property)` | violating / explicit names | 1 |
| H4 | QueryUtils raw SQL: no `createQueryBuilder`/`Brackets`, no `$1` positional params | grep `backend/src` | files with a violation / files that issue SQL | 1 |
| H5 | No testing theater: behaviour is proven through the real controller on real Postgres | controllers with no `*.integration.spec.ts` naming them | uncovered / controllers | 1 |
| H6 | Migrations are applied by Flyway only; a shipped migration is never edited | `git log --diff-filter=M` on `backend/database/V*.sql`; version gaps. Hand-applied SQL cannot be seen from the repo: say so | edited / migrations | 1 |
### From `@protocol/engineering-defaults` and the working-style standards
| id | rule | check | formula | w |
|---|---|---|---|---|
| H7 | Migrations carry structure, not data | migrations containing `INSERT INTO`, `UPDATE … SET`, `DELETE FROM` | with data / migrations | 1 |
| H8 | Column DEFAULTs are the model's, not the migration's ⚖ | migrations containing `DEFAULT` | with DEFAULT / migrations | 0.5 |
| H9 | TypeScript and Bun, never Python | `*.py` files and `python` shebangs or invocations in `scripts/`, `devops/`, package scripts | violating / code files in scripts and devops | 1 |
| H10 | Types are honest: no `any`, no suppressed errors ⚖ | `: any`, `as any`, `<any>`, `@ts-ignore`, `@ts-expect-error` in `src`, plus the shapes that are `any` underneath: `Record<…, any>`, `any[]`, `Array<any>`, `Promise<any>`, `\| any`, `any>` (comment lines excluded; widened 2026-11-11 after 43 such sites hid behind a score of 75) | density per kLOC, limit 10 | 0.5 |
| H11 | A file reads top to bottom (`@codebase/typescript` §File Organization) ⚖ | source files over 600 lines (also report those over 1,500) | over 600 / source files | 0.5 |
| H12 | Every test runner states its width (`@wildeagency/dev-standards`, heavy-runs) | `maxWorkers` / `workers` set in each jest, vitest, playwright config | unset / configs | 1 |
| H13 | The theme gate is green (`@codebase/design-system` §Token gate) | `node devops/scripts/lint-theme.mjs`, exit code and violation list | violations / frontend source files; **a non-zero exit is reported RED whatever the number** | 1 |
### Observed, not scored (report the count; the operator has not said what the target is)
`console.log` left in `src`; `TODO|FIXME|HACK` markers; the largest ten files by lines.
## Adding or changing a line
The operator owns the rubric. A line goes in this table with its source (his words, a ruling date, or the CLAUDE.md rule) or it is marked ⚖. Raise a proposed line as a finding in the report, not as a silent edit. When a line changes, rescore the last report's commit under the new line so before and after stay comparable.
## The report
One dated file per repo per run, never overwritten: `workspace/code-health/<YYYY-MM-DD>-<repo>.md`, in the repo that was scanned. Order: **(1)** the headline `Score 76 (was 71, +5) at <sha>` and the three lines that moved most; **(2)** the table of every line with score, count/population, formula and ≥3 `file:line`; **(3)** what could not be measured, and what each check cannot see; **(4)** proposed changes that would raise the score, each naming its line and the expected movement. **Score only; fix nothing in a scan run.** Fixes are separate impulses the operator or a lane picks up, and the next scan reports their effect.
## The recurring impulse
`@protocol/schedule-work`: an impulse with `metadata.cadence`, three headings (`What to do each run`, `Worth a message`, `Done when`). Each run: fetch this protocol, scan each in-scope repo at its `origin/main`, write the dated report, compare with the previous report, and append a `log` entry with the score line. **Worth a message:** a drop of 3+ points on any repo, a line that went RED, or a line that could not be measured twice running. A clean or rising run is silent. Complete the claim each run, never the impulse.
---
*Related: `@content/web/lorekit/2026-11-07-agent-memory-opentelemetry-moment` · `@protocol/example-impulse` · `@agentsite/render-fetch-contract` · `@any-family/home/photo-backup` · `@any-family/calendar/tax-prep`*
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*