Baselines and the ratchet
A new rule in an old repo usually finds offenders on day one. Fixing all of them before the rule can go on means the rule never goes on. A baseline freezes today’s offenders at their current value so the rule can be strict from now on:
- a new offender fails;
- a recorded offender that grew past its frozen value fails;
- a recorded offender that stayed the same or shrank passes.
As offenders are fixed their entries are deleted or lowered, never raised. The debt only shrinks.
The baseline file
Section titled “The baseline file”A baseline is a committed JSON file, one per rule, at .noctcore/lint-meta/baselines/<rule-id>.json
(DEFAULT_BASELINE_DIR). It is a flat map from an opaque key to a number:
{ "src/big.ts": 421}The key means whatever the rule says it means. A rule that tracks more than one kind of offender
namespaces its keys (size:src/a.ts, exports:src/a.ts) so one map holds both.
A ratcheting rule
Section titled “A ratcheting rule”The package root exports the helpers a rule needs at run time:
| Export | What it does |
|---|---|
loadBaseline(ctx, ruleId, dir?) |
The committed map for the rule, or {} when there is none (or it is not a map of numbers). |
isGrandfathered(baseline, key, current) |
true when the key is recorded and current is at or under its frozen value. |
serializeBaseline(map) |
The map as JSON with sorted keys and a trailing newline, for a stable diff. |
DEFAULT_BASELINE_DIR |
.noctcore/lint-meta/baselines |
A rule implements baseline(ctx) to snapshot the current offenders, and checks against the
committed snapshot in run:
import type { IMetaCtx, IMetaRule, IViolation } from '@noctcore/harness';import { isGrandfathered, loadBaseline } from '@noctcore/harness';
const ID = 'max-file-lines';const LIMIT = 300;
function sizes(ctx: IMetaCtx): Map<string, number> { const found = new Map<string, number>(); for (const file of ctx.glob('src/**/*.ts')) { const lines = (ctx.read(file) ?? '').split('\n').length; if (lines > LIMIT) found.set(file, lines); } return found;}
export const maxFileLines: IMetaRule = { id: ID, category: 'source-text', ciCritical: true, description: `Source files stay at or under ${LIMIT} lines; older offenders are frozen in a baseline.`, baseline(ctx: IMetaCtx): Record<string, number> { return Object.fromEntries(sizes(ctx)); }, run(ctx: IMetaCtx): IViolation[] { const frozen = loadBaseline(ctx, ID); const violations: IViolation[] = []; for (const [file, lines] of sizes(ctx)) { if (isGrandfathered(frozen, file, lines)) continue; const was = frozen[file]; violations.push({ file, rule: ID, message: was === undefined ? `${lines} lines, over the limit of ${LIMIT}.` : `${lines} lines, grown past its frozen ${was}. Shrink it back.`, }); } return violations; },};loadBaseline and isGrandfathered are value imports, so the repo installs @noctcore/harness.
Writing the baseline
Section titled “Writing the baseline”The runner does not write baselines: freezing debt is a decision that should show up as a reviewed
diff, not a side effect of a CI run. A short script calls the rule’s baseline and writes the file:
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';import path from 'node:path';
import type { IMetaCtx } from '@noctcore/harness';import { DEFAULT_BASELINE_DIR, portableFileGlob, serializeBaseline } from '@noctcore/harness';
import { maxFileLines } from '../lint-meta/rules/max-file-lines.mts';
const root = process.cwd();const ctx: IMetaCtx = { root, read: (rel) => (existsSync(path.join(root, rel)) ? readFileSync(path.join(root, rel), 'utf8') : null), exists: (rel) => existsSync(path.join(root, rel)), glob: (pattern) => portableFileGlob(pattern, root), exec: () => ({ code: 1, stdout: '', stderr: 'exec is not used by this script' }),};
const file = path.join(root, DEFAULT_BASELINE_DIR, `${maxFileLines.id}.json`);mkdirSync(path.dirname(file), { recursive: true });writeFileSync(file, serializeBaseline(maxFileLines.baseline?.(ctx) ?? {}));console.log(`froze ${file}`);node scripts/freeze-baseline.mtsBefore the baseline, the rule reports the file:
[ERROR] max-file-lines (src/big.ts): 421 lines, over the limit of 300.After freezing it at 421 the rule reports nothing for it. One more line, and it fails again:
[ERROR] max-file-lines (src/big.ts): 422 lines, grown past its frozen 421. Shrink it back.Keeping it honest
Section titled “Keeping it honest”- Commit the baseline with the rule, in the same pull request, so the reviewer sees both.
- Lower or delete entries as offenders are fixed. A rule may also fail when a recorded offender is gone, so stale entries cannot pile up; that choice is the rule’s.
- Re-freezing to raise a number is the one move the ratchet exists to prevent. If it is ever right, it is right in a reviewed diff with a reason.
- The rules in
@noctcore/harness/rulesare strict and have no baseline: they hold properties that should be true in every repo from the start.