Skip to content

The Cheap Gates Run First, the Decisive Ones Run Last

Lint and format catch cheap mistakes. The gates deciding if code survives sit further down the pipeline, in an order that took years to get right.

· · 9 min read
A dark server rack with status lights, the kind of infrastructure a CI pipeline runs its gates against

Why did getting the gate order right take years?

Twelve gates. That is roughly what a serious pipeline runs before code merges, once you count format, lint, types, static analysis, three flavors of tests, and four separate security checks. Most teams don't design that order on purpose. They add a gate whenever something breaks in production, bolt it onto the end of whatever CI script already exists, and never touch the sequence again. An order that grows that way tends to optimize for the wrong thing: getting to green, not getting to correct.

The rule I actually use, after enough painful reordering to trust it: run the cheapest checks first, but don't confuse cheap with meaningful. A format check is nearly free and nearly worthless as a signal. Mutation testing is expensive and tells you more about a codebase than almost anything else on this list. The gates that decide whether code survives sit at the far end of the pipeline, not the front. That has real consequences for anyone leaning on an AI assistant to write code, because a model can silence a measurement a lot faster than it can fix what the measurement caught. Why does that keep happening on team after team? Because silencing a warning is a five-minute fix, and actually addressing what it found rarely is.

Which gates are cheap enough to run on every commit?

1. Format check (Prettier or Biome). Runs in under a second and catches exactly one class of problem: inconsistent whitespace and quote style. It has zero opinion on whether the code works. I still put it first, because a failing format check is the cheapest possible signal that something is off in a commit, and there's no reason to burn CI minutes on anything else until it passes.

2. Secret scanning (Gitleaks, TruffleHog). Here's my one genuinely disputable position on this list. Most teams file secret scanning under a "security phase" bolted onto the end of the pipeline, after lint, after tests, sometimes after a manual review. That's backwards. A leaked API key or a committed .env file isn't a code-quality problem waiting its turn. It's an active incident the moment it lands on a shared branch. Run it second, immediately after format. A secret caught here is caught before the merge, not after it, when the key already has to be rotated.

Secret scanning doesn't belong in the security phase at the end of the pipeline. It belongs right after format, because a leaked key doesn't wait for lint to finish.

SHARE Post on X Bluesky

3. Lint (ESLint or Biome). Third, and this is where the AI part of the story turns uncomfortable. An assistant can satisfy a linter in one pass, every time, without writing code that is actually correct. Wrap the unused variable in an underscore. Disable the rule inline. Rename a function to dodge a naming convention. None of that touches whether the logic works. ESLint's own documentation is honest about this scope: it checks patterns, not behavior. Why do so many teams still treat a clean lint run as proof of quality? I think it's because green is easy to read, and correctness isn't. The same teams tend to be the ones that suppress rules instead of fixing code.

4. Type check (tsc --noEmit, mypy --strict). Types catch a real category of bug, the wrong shape passed to the wrong function. But they are gameable too, just with a different tool. any in TypeScript and # type: ignore in Python are the type checker's version of a lint-disable comment. A "finished" strict-mypy migration can turn out to be a pile of added ignores with none of the underlying type confusion resolved. Is that migration really finished, or just declared finished? The check still catches plenty on the way there, which is why it runs fourth instead of getting skipped.

5. Dependency and SCA scan (OSV-Scanner, npm audit). Fast, because it is a lookup against a known-vulnerability database rather than an analysis of your code. Google's OSV-Scanner pulls from the same open advisory data most SCA tools use, and running the check this early means a newly disclosed CVE in a transitive dependency blocks the build before anyone wastes an hour debugging something unrelated. The lookup has a blind spot, though: a name that was registered last week has no advisory yet, which is how a hallucinated import goes green while this gate stays quiet.

A terminal window mid build, scrolling output in red and white text
Photo by Lukas on Unsplash

Where does gate cost start buying something real?

6. SAST (Semgrep). Static application security testing looks for exploitable patterns: SQL built from string concatenation, an unvalidated redirect, a hardcoded crypto key. Semgrep's documentation covers custom rule writing, which matters, because the default ruleset misses plenty that is specific to your own stack. The AI failure mode here is different from lint. Ask a model to "fix the Semgrep finding" and it will often silence the finding with an inline suppression comment rather than change the vulnerable pattern underneath. Read the diff. Don't trust the green checkmark alone, and remember that a green run also says nothing about classes no rule models, which I counted by CWE.

7. License and compliance check. The least glamorous gate on the list, and the one most teams skip until legal asks about it. It sits here, not at the end, because a GPL-licensed dependency pulled in transitively is far cheaper to catch and swap before six other pull requests build on top of it.

Close-up of a computer screen showing a code review comment thread
Photo by Yancy Min on Unsplash

8. Unit tests (vitest, pytest). Fast, and the gate almost everyone assumes is the strongest signal on the list. It isn't, not on its own. A generated test that asserts expect(result).toBeDefined() passes every single time and proves nothing about the code it is supposedly guarding. I would rather see three real assertions than thirty of those. This is also exactly where coverage stops meaning what people think, and it is the strongest argument for gate twelve.

9. Static analysis (SonarQube, SonarCloud). Slower than the earlier gates, because it needs a fuller picture of the codebase rather than a single changed file. SonarSource's own documentation is explicit that the useful number here is debt trending over time, not a single scan's pass or fail. I care less about today's score than whether next month's is worse. Is the trend line actually moving, or just holding steady on a treadmill of suppressed rules? That number tells you whether a team is fixing things or routing around the tool.

Which slow gates, ten through twelve, decide the most?

10. Integration tests. These exercise the real seams: the database call, the API contract, the queue that a unit test mocks away entirely. Slower, because they touch real infrastructure instead of a stub. But a green integration suite means something a mocked unit test simply can't claim on its own.

11. Flaky-test detection (N reruns). Running the suite three or five times back to back, specifically to catch nondeterminism, is expensive by design. You're multiplying total test time just to find the handful of tests that pass on luck rather than logic. Worth every extra minute. A flaky test that gets ignored long enough becomes a test nobody trusts, and an untrusted test is worse than no test at all, because it still shows up green.

12. Mutation testing (Stryker, mut.py). Last, and by far the most expensive gate, because it reruns your entire test suite against dozens or hundreds of deliberately broken versions of your own code. If the suite still passes after a mutation flips a single comparison operator, that suite was never testing the thing it claimed to test. This is the gate that actually answers the question coverage only pretends to answer. It runs dead last because nothing before it is cheap enough to justify going first, and nothing after it needs to run at all.

What do you do with a flaky test once you've caught it?

Detection, gate eleven, is only half the job. Catching a flaky test and then merging it anyway, which is what most teams do under deadline, just moves the problem downstream. You need a policy, not a rerun count.

The policy I'd pick has three moves. Quarantine on detection: a test that fails its rerun check gets tagged and pulled out of the blocking suite automatically, so it stops failing unrelated pull requests, but it keeps running and reporting. Never quarantine silently. The moment a test leaves the gate it goes on a tracked list with an owner and a date, or "temporary" becomes "forever". And cap the quarantine: if it isn't fixed or deleted in two weeks, it's deleted. An untrusted test earning a permanent exemption is worse than the flake it was hiding.

The trap is the blanket retry. Wrapping the whole suite in "retry up to three times" makes CI green and buries every real intermittent bug alongside the flaky ones. Retries hide nondeterminism; quarantine surfaces it. I later put numbers on both halves of that sentence in what retries and reruns actually do, and the rerun count in gate eleven turned out to be the weaker half. For concurrency code, add a total check instead of a retry: a counter that must equal threads times iterations caught a broken spin-lock on every run. Do you actually know which of your green runs passed on the second try? If the answer is no, your pipeline is lying to you politely.

Twelve gates, four of which an assistant can satisfy without fixing anything underneath, is a survivable ratio if the other eight actually hold the line. So which order do your gates run in right now? Getting the order right usually means getting it wrong first, and I would bet at least one of the twelve on your list is sitting in the wrong slot in your pipeline this week. The CI gate audit checks which of the twelve a pipeline file is missing, in your browser.

Frequently Asked Questions

What order should CI gates run in?
Run the cheapest checks first, but don't confuse cheap with meaningful. The author's order starts with format, secret scanning, lint, types and dependency scans, then SAST, licenses, unit tests and Sonar. Integration tests, flaky-test detection and mutation testing run last.
Why run secret scanning right after the format check?
A leaked API key or committed .env file is an active incident the moment it lands on a shared branch. It isn't a code-quality problem waiting its turn. Running it second means a leak is caught before merge, which has saved teams an actual key rotation.
What should you do with a flaky test?
Quarantine it on detection, so it stops failing unrelated pull requests but keeps running. Put it on a tracked list with an owner and a date. If it isn't fixed or deleted within two weeks, delete it. Blanket retries just hide intermittent bugs.
Why is mutation testing the last CI gate?
It's the most expensive gate, because it reruns your whole suite against many deliberately broken versions of your code. If the suite still passes after a mutation flips a comparison operator, it never tested that behavior. It answers what coverage only pretends to.