A coverage ratchet is a threshold that only goes up. CI stores the best number it has seen, fails any change that lands below it, and raises the stored value when somebody does better. It sounds fair. What does it do to a real history?
To find out, I replayed several versions of the rule over 200 real commits of click, the Python command line library, and counted every block. Here's what that showed. The subject is coverage enforcement in CI, so it takes for granted that you already know a coverage figure can be gamed.
What did I replay, and with which gate?
I took the 200 most recent first-parent commits on click's main branch, ending at 08b803c (16 November 2026) and starting at 505e485 (26 May 2025), plus one earlier commit to seed the baseline. First-parent history is what landed on main, with each merged pull request as a single step. Nothing was cherry-picked.
Every commit got a clean checkout and a full run of the default test suite under coverage.py 7.17.5 with branch measurement on, source = click, two decimals of precision and click's own exclusion patterns. The config stayed fixed for all 201 runs. Python was 3.13.7 on an Apple M1, with pytest 9.0.2, the version in click's lockfile at the newest commit.
That pytest pin matters. I started on pytest 9.2.0, and the oldest commit failed during test collection, because a deprecation warning became an error under click's filterwarnings = error. coverage.py still wrote a report: 20.62 percent instead of 79.67. A gate that reads the report without checking the test exit status would call that a 59 point drop. Run the gate only after the tests pass.
Of the 200 commits, 102 touched src/click, 12 touched only tests and 86 touched neither. One commit had four failing tests, so I left it out of the verdicts, along with the parent comparison for the commit after it. I ran the whole replay twice, and all 201 results matched on every line and branch count. Across the window coverage climbed from 79.67 to 85.66 percent, while uncovered units (missed lines plus missed branches) fell from 1,183 to 936.
The gate itself is short. Two metrics, one rule for judging and one for tightening:
def judge(metric, current, baseline, tolerance=0):
if metric == "percent":
return round(current - (baseline - tolerance), 2) >= 0
return current <= baseline + tolerance
def tighten(metric, current, baseline):
if metric == "percent":
return max(current, baseline)
return min(current, baseline)
The baseline can come from two places. A stored baseline is a file holding the best value so far. A parent baseline is whatever the previous commit on main measured, which is what a pull request check against the base branch does.
How many commits did each gate block?
| Gate | Blocked | Judged |
|---|---|---|
| Stored percent, no slack | 57 | 199 |
| Stored percent, 0.1 point slack | 3 | 199 |
| Stored percent, 0.5 point slack | 0 | 199 |
| Percent against parent, no slack | 15 | 198 |
| Stored uncovered count, no slack | 98 | 199 |
| Stored uncovered count, slack of 5 | 14 | 199 |
| Uncovered count against parent | 18 | 198 |
| Changed lines, at least 90 percent run | 17 | 198 |
The last row looks gentle partly because only 92 of those commits changed any executable line. The other rows apply to everything, including documentation edits.
Counting uncovered code sounds fairer than a ratio, so why is it the harshest row? A stored count never forgives. Any feature that adds a single unrun branch raises it, even if the percentage improves, and it was red on 98 of 199 commits.
Where did the ratchet earn its keep?
The largest drop in the window is dbf4d14, a merge from the stable branch on 19 May 2026. Coverage fell from 81.69 to 81.44 percent. Every test that ran passed.
Fewer of them ran. Skips went from 25 to 85. A new skipif on a pager test gave its reason as flakiness in free-threaded Python 3.14 on macOS, but the condition ended in sys._is_gil_enabled(). That's true on the ordinary build. On my 3.13.7 interpreter, 60 test cases were skipped with a message about a Python I wasn't running. Two days later the condition on main read not sys._is_gil_enabled(), and the skips were back to 25.
Exit status didn't show it. A 90 percent changed-line check did fail the commit, but over a single Windows line unrelated to the skip. The total saw the real damage: 16 more uncovered units across the two terminal UI modules, _termui_impl.py and termui.py. This is the case I'd keep a ratchet for: tests that stop running without anyone editing the code they cover. On Linux the first term of that condition is false, so only a macOS run would lose them.
Plainer catches exist too. 7c99ebe, which made click's text wrapper aware of ANSI codes, added 30 uncovered units and left 21 of 72 changed lines unrun. All three kinds of gate failed it.
Why did the stored baseline stay red for 26 commits?
On 8 October 2025, commit 8d4051e took coverage from 80.42 to 80.36 percent. Four of its 12 changed executable lines never ran. That's a fair catch too.
In the replay, as in the real history, the commit is on main anyway. The stored baseline stays at 80.42, and the next 25 commits are judged against a number the code no longer reaches. None of those 25 lowered coverage against its parent. Of all 26 red commits, 19 touched no source or test file. The streak ended on 27 February 2026.
Over the full window the pattern holds: 57 red commits, 41 of them no worse than their parent, 30 with no change to source or tests at all.
Keep the limit of this in mind. A required check would have stopped 8d4051e before it merged, so this isn't a record of anything click's maintainers saw. It shows what your pipeline does after one admin merge or one override that leaves the stored number alone.
A zero-slack stored ratchet mostly punishes whoever commits after the change that lowered coverage.
My view, and you're welcome to argue: a stored baseline that nothing is allowed to lower is a bad default. If your team can override the check, the override should write the new, lower number in the same commit, with a reason next to it.
Which honest changes did it block?
Comparing with the parent removes the bystanders. It blocked 15 commits, and 11 of them raised the uncovered count. The other four didn't:
679a7a0swapped a loop forout.extend(lex). Both changed lines ran. The uncovered count stayed at 1,104, and the percentage went from 82.82 to 82.81.08b803c, a Python 3.11 cleanup, removed two methods: 85.67 to 85.66, uncovered unchanged.ae46cfd, a stream handling fix that added 24 tests to the default run, deleted a covered method and lost 0.01.3cbaa76, a merge from the stable branch, ended with 21 fewer statements. Coverage fell from 84.65 to 84.58 although the uncovered count dropped by one.
Why does removing code hurt? The percentage is covered units divided by all units. Delete something that was fully tested and both shrink by the same amount, which pulls any ratio below 100 percent downward. An error-count ratchet, like the TypeScript strict one, doesn't have this problem, because deleting dead code doesn't add errors to the count.
The drops were small. Twelve of the 15 were 0.10 point or less, and the largest was 0.25.
What did both ratchets let through?
Seven commits passed the percent check and the count check against their parent while less than 90 percent of their changed executable lines ran. The biggest is c69643b, which added the public function click.get_pager_file() on 30 April 2026.
It edited no test file, and the number of passing tests was 1,487 before and after. Of 73 changed executable lines, 35 never ran. Yet coverage rose from 80.71 to 80.88 percent and the uncovered count fell from 1,156 to 1,150, because the rewrite replaced older pager code that carried more unrun lines and branches than the new version does. I can't tell from these counts how much of the 35 was already untested in its old form. The maintainers added 228 lines of pager tests on 16 May, in a follow-up commit.
Then the blunt version. At the newest commit I turned all 1,511 assert statements in click's tests into bare expressions, so each one still evaluates and checks nothing. The run exited 0. Coverage: 85.66 percent, 936 uncovered, identical in every count, and both ratchets passed it. Checks written with pytest.raises were left alone, but a ratchet wouldn't have noticed if the plain assertions had never existed. That's the same blind spot I described for lines run versus results verified, now with a ratchet on top.
Is fail_under in coverage.py a ratchet?
Not by itself. The coverage.py docs say fail_under makes the report exit with status 2 when the total is lower than the value. You maintain that value. They also say precision defaults to 0, and that the comparison uses whichever is greater, precision or the number of decimals written in the threshold. The change history dates that second rule to version 7.17.1.
I probed it at the newest commit, whose total is 85.657 percent:
| Options | 7.17.0 exit | 7.17.5 exit |
|---|---|---|
--precision=0 --fail-under=86 |
0 | 0 |
--precision=0 --fail-under=85.9 |
0 | 2 |
--precision=2 --fail-under=86 |
2 | 2 |
--precision=2 --fail-under=85.66 |
0 | 0 |
Watch the first row: a threshold of 86 passes a project that's below 86. The second shows an upgrade turning a green build red with no code change, and the red answer is the correct one.
I also simulated the lazy ratchet, where somebody copies the whole number the report prints into fail_under. It blocked 3 of 199 commits, ended with a threshold of 86 while real coverage was 85.66, and at one point passed a commit 0.49 point under its own threshold. Set precision = 2.
In JavaScript projects the knobs differ. Jest and Vitest both document negative thresholds as a maximum number of uncovered items, which is the count metric built in. Vitest also has coverage.thresholds.autoUpdate, which rewrites the thresholds in your config file when current coverage beats them, and accepts a function, for example one that rounds down with Math.floor. I read those pages (Jest 30.5, Vitest 5.1.2) and ran neither tool.
Why did one commit score two different numbers?
Is the number even a property of your code? I ran the newest commit under three interpreters, twice each.
| Python | Coverage | Uncovered |
|---|---|---|
| 3.12.8 | 85.86 | 923 |
| 3.13.7 | 85.66 | 936 |
| 3.14.2 | 85.66 | 936 |
Click's types.py branches on sys.version_info >= (3, 13), since type variable defaults arrived at runtime in that release, so each interpreter leaves the other side unrun. Record a baseline on 3.12, move CI to 3.13, and the zero-slack gate fails with no code change. The 0.20 point gap is larger than 13 of the 15 real drops the parent check caught.
Operating systems do the same. _winconsole.py has 162 statements and none of them ran on my Mac. Commit c5cced7, a Windows fix to open_url, had 9 changed executable lines and none ran here. A changed-line gate that runs only on macOS would block it, however well it had been tested on Windows.
A stored number will outlive the interpreter that produced it. Write the Python version, the coverage version and the OS into the baseline file, and treat a change in any of them as a reset that somebody signs off.
What would I run alongside it?
A changed-line check first. diff-cover does this job: its README defines diff coverage as the share of new or modified lines that tests cover, comparing against origin/main unless told otherwise. I didn't run it. The 17 blocks in the table come from my own short function that intersects git diff hunks with the coverage.py JSON report, counting lines only.
It's jumpy on small changes. Five of the 17 hinged on a single unrun line, including the follow-up commit that added the pager tests. Requiring at least two missed lines before failing leaves 12. It's also blind to old code: b67832c and 5850ef9 ran every changed line, yet the uncovered count went up.
Here's the setup I'd start with, given these numbers:
- Stop if the test step fails. Don't let the gate read a partial report.
- Fail a pull request when under 90 percent of changed lines ran and at least two didn't, with a named override for platform-only code.
- Keep a stored percentage with 0.1 point of slack as a backstop. It blocked 3 commits. The changed-line check with its two-line floor catches two of them; the third is
dbf4d14and its 60 skipped tests. The slack doesn't compound, since the stored value only moves up. - Print the uncovered count on the pull request and leave it advisory.
- Treat edits to the coverage config as code. The docs say
# pragma: no coverexcludes a line or its whole clause andomitleaves files out of measurement. Give both a second reviewer.
Together, checks 2 and 3 would have stopped 13 commits. What does 0.1 give up? Eight of the 11 drops that added uncovered code were too small to trip the backstop. The changed-line check catches five of those eight, and three get through both. That's the trade, stated plainly.
The per-file complexity ratchet raised the same question from the other side: store a number, then check what cheap or innocent edits do to it. If you're deciding where this gate sits among the others, the CI gate audit lists what runs before it.
What this doesn't show
One repository, pure Python, macOS only. A replay can't show how people react to a red check, and a team that sees red will change its commits. I didn't measure the effect of exclusion pragmas, omit lists or other config edits, so I'm not quoting figures for them. Mutation testing, Jest, Vitest and diff-cover stayed out of scope. The thresholds above are what looked sensible on about eighteen months of one project, so replay your own main branch before you copy them. The gate is two short functions, and the replay is a loop over git log.