Ratchet: Quality Can Only Go Up

AI-assisted development can lead to gradual quality regressions. The "ratchet pattern" enforces monotonically non-decreasing quality metrics, blocking merges that lower any measured value below a recorded floor.

Ratchet: Quality Can Only Go Up

Each change an AI assistant merges can look fine on its own while the codebase's overall quality quietly slides. Test coverage drops a few percentage points per sprint. Lint warnings pile up in recently touched files. Type errors appear in modules that were clean a month ago. No single merge introduces a large regression. The damage is the sum of many small ones.

Human developers produce the same slide. AI agents shorten the timeline: what a human team might let accumulate over a quarter, a team running parallel AI agents can accumulate in two weeks.

The Ratchet (a one-way quality floor) answers this directly. The team records a floor for each quality measure, and any merge that would drop a measure below its floor is blocked. The floor can only rise. The name comes from the mechanical device, a gear with a pawl that turns one way and locks against the other. Quality advances freely and cannot slip back.

How the Mechanism Works

The Ratchet's state lives in a checked-in file that records the current floor for each metric category: branch coverage percentage, total lint violations, type check errors, import boundary violations, and whatever other metrics the team has chosen to track. This file is updated whenever a merge improves on the current floor. It is never updated downward.

Before every merge, the chain of automated checks runs the full measurement suite against the incoming change. Each measurement is compared to the corresponding value in the Ratchet file. If any measurement falls below its floor, the merge is blocked with an explicit report showing which metric regressed and by how much.

The elegance of this approach is in what it leaves out. It needs no human reviewer to notice that a particular file's coverage dropped. It needs no team norm about test coverage that depends on people remembering to check. It needs no per-PR configuration. The floor is defined once and enforced automatically on every merge.

The Pre-Session Layer

The Ratchet described above is a check that runs after the code is written. A complementary approach operates earlier: checking structural constraints before an agent session begins, before any code is written.

Import boundary violations, type export constraints, and dependency direction rules are all properties of the codebase that can be checked against the intended scope of a task before any code is generated. If the task as specified would require an agent to work across a module boundary that the architecture does not permit, surfacing that constraint before the session starts is preferable to surfacing it at merge time.

Pre-session constraint checking requires encoding the structural rules of the codebase in a form the system that assigns work to agents can query. This is additional tooling, but it prevents a category of work from being attempted at all when the architectural constraints would invalidate it, saving the agent session cost and avoiding the friction of a blocked merge after work is complete.

Calibrating the Floor

One practical question the Ratchet raises immediately: what is the right starting floor? Setting the initial floor too high, at a value the codebase does not currently meet, blocks all merges immediately. Setting it too low fails to provide meaningful enforcement.

The practical answer is to measure the current state of the codebase, use those measurements as the initial floor, and then raise it as improvements come. The Ratchet needs no pre-existing quality discipline. It asks only that quality hold from wherever the team is starting.

There is nuance here for codebases with areas of intentionally different quality expectations: legacy code that will not be tested, generated files that should be excluded from coverage, experimental modules with acknowledged technical debt. The Ratchet file can carry per-directory or per-module floors in place of a single global value, allowing different enforcement thresholds for different parts of the codebase.

The Audit Value

A checked-in Ratchet file that is updated on every merge creates a historical record of quality improvement over time. The git history of the file shows when each metric improved, how quickly, and implicitly correlates with the work that produced each improvement.

For teams that need to demonstrate quality trends to stakeholders, engineering leadership, security reviewers, compliance auditors, this history is more useful than point-in-time snapshots because it shows direction and speed of improvement in addition to current state.

Limitations and Tradeoffs

The Ratchet works well for metrics that can be objectively measured and aggregated. It is less useful for quality properties that are harder to quantify: code clarity, architectural coherence, API design quality. These matter, and the Ratchet leaves them untouched. Human review remains important for the qualitative dimensions.

There is also a category of legitimate regression that the Ratchet can mishandle: intentional removal of code that happens to remove covered lines, bringing down the coverage percentage even though the removal was the right architectural decision. The right response is usually a manual floor update with an explanatory commit message. That deserves a team norm, so floor updates cannot become a quiet way past the check for regressions that nobody intended.

The Ratchet is only as useful as the metrics it enforces, and Goodhart's law applies in full: when a measure becomes a target, it stops being a good measure. High coverage can sit on top of weak tests. A low lint error count can sit on top of unreadable code. The Ratchet enforces the floor on the things you choose to measure, which makes the choice of what to measure a consequential decision.