Handle code complexity exceptions in legacy repositories with two rules and one record: hold changed code to the limit, freeze the old code as a baseline that may only shrink, and write every exception down with an owner, reason, tests, expiry and review trigger. Skip any of the three and the limit either stalls delivery or quietly stops meaning anything.
This matters more now that coding agents write a growing share of the diff. An agent will happily add a fifth nested branch to a function that was already over the limit, because nothing told it not to. A complexity limit is the obvious guardrail. On a legacy repo it also fails the build on day one.
This post is the narrow policy for that moment. It does not redefine cyclomatic or cognitive complexity. For scoring and generic thresholds, read the practical guide to code complexity. Here we cover what to do when a limit meets code that was written before the limit existed.
Why a complexity limit fails on legacy code (and why blanket exemptions are worse)
A complexity limit fails on legacy code because the old code already breaks it. Turn the rule on and you get hundreds of violations nobody can fix this sprint. The team's reaction is predictable: disable the rule, or exempt whole directories.
Blanket exemptions are worse. A folder-level ignore says "this code is allowed to get worse forever." The ignored folders are the ones people edit most, because they are the oldest and most central. The exemption covers new changes as well as old ones.
The opposite failure is just as real. If the gate fails every build until the backlog is clean, engineers route around it, merge with overrides, or stop touching the legacy modules. Delivery stalls exactly where the business needs change.
The way out is to stop asking one rule to do two jobs. Old code and new code need different treatment.
Changed code versus the legacy baseline: two rules, not one
Use two rules. Changed code must meet the limit, and untouched legacy code is frozen at its current level and only allowed to improve. That is the whole idea behind a ratchet.
Rule one applies to the diff. Any function a pull request adds or modifies is judged against your threshold. This is how SonarQube's Clean as You Code model works: the quality gate focuses on new or changed code, and you define the new code period by previous version, number of days, a reference branch or a specific analysis. Pull requests treat changed lines as new code (SonarQube documentation, 2026). Avoid overall-code conditions in the gate if you want to keep legacy issues from blocking delivery.
Rule two applies to the baseline. Record every function currently over the limit and fail the build only if one gets worse or a new one appears. ESLint bulk suppressions are available in ESLint 9.24 and later; the documentation describes the feature and its suppression flags. Run eslint --fix --suppress-rule complexity, commit the generated eslint-suppressions.json, and use eslint --prune-suppressions as violations get fixed. Existing violations are suppressed, new ones fail.
One catch. A per-file suppression baseline can miss a function that is already over the limit and gets more complex, since the count of violations has not changed. If that regression matters to you, ratchet per function against the target branch instead.
The payoff is simple. Touch a legacy function and you pay down its debt in that change. Old code improves only where people actually work, which is where it pays back.
The exception record: owner, reason, tests, expiry and review trigger
An exception is a record, not a comment. It has five fields, all required, or it is not an exception. A bare // eslint-disable or a line in a suppressions file tells you nothing about who is responsible or when it ends. ESLint's suppression file has no expiry dates, so you have to add that discipline yourself.
Require these five fields:
Owner. A named person or team, not "the platform group." Someone who gets asked when the exception is questioned.
Reason. One sentence on why the function cannot meet the limit right now. "Payment retry state machine, splitting it risks double charges" is a reason. "Legacy" is not.
Tests. What protects the behaviour while the code stays complex. If there are no characterization tests, the exception is not approved until someone writes them, because complex untested code is where bugs live.
Expiry. A date, usually 90 to 180 days out. When it passes, the exception either gets renewed with a fresh reason or the build fails.
Review trigger. An event that forces a re-look before the date: the file is edited again, a bug is traced to it, or the owner leaves. Triggers catch the cases where the calendar is too slow.
Keep the records somewhere a reviewer can see them in the pull request, next to the suppression itself. A tracker ticket nobody opens will not survive the first reorg.
Illustrative exception policy you can adapt (not a customer story)
Below is an illustrative policy, written as a template. It is not drawn from any customer. Change the numbers to fit your codebase.
Scope. The complexity limit (for example, cognitive complexity of 15 per function) applies to every function added or modified in a pull request. Untouched functions are held at their recorded baseline score.
Baseline. On adoption, snapshot all functions over the limit. The baseline can only shrink. Any function that grows past its recorded score fails the build.
Granting an exception. The author adds a record with owner, reason, tests, expiry and review trigger. A tech lead approves it in the same pull request. Maximum expiry: 120 days.
Tests required. No record is approved without tests that pin current behaviour.
Renewal. One renewal is routine. A second renewal on the same function needs a written plan and a named engineer with time allocated.
Triggers. Editing a function with a live exception, a production incident tracing to it, or the owner changing teams re-opens the record.
Agents. Coding agents follow the same rules. They are told the limit and the exception process up front, and an agent may not add a suppression on its own. A human owner has to sign the record.
Reporting. Once a month, publish the count of live exceptions, how many expired, and how many were retired. A number that only goes up is a finding.
Stopping exceptions becoming permanent across repos
Exceptions become permanent when nobody counts them, so count them across every repo and make the total visible. One repo with 40 stale suppressions is a local problem. Fifteen repos each with a few, using different formats and no shared expiry rule, is how a whole organisation ends up with a limit that exists only on paper.
Three habits prevent it.
One format everywhere. The same five fields, the same expiry rule, the same approval step. If each repo invents its own, you cannot roll them up.
Expiry that bites. An expired exception fails the build. It does not warn. Warnings get ignored within a week.
A ceiling that falls. Set an org-wide cap on live exceptions and lower it each quarter. Teams that retire exceptions earn room. Teams that renew everything hit the cap.
The cross-repo part is also where coding agents cause drift. A rule written into one repo's CLAUDE.md does not reach the next repo, and each vendor has its own file format. Coderbuds is engineering intelligence for software teams, with delivery and DORA metrics from GitHub or Bitbucket and an agent that feeds your team's standards into coding agents. Point that agent at one shared standard, "changed code meets the limit, exceptions need a record," and agents stay consistent across repos instead of drifting between per-file copies. A rules file is still the right start for a single repo. See how Coderbuds compares with a CLAUDE.md file for where that stops working.
Checking the policy works: delivery signals, review delays and shared standards for coding agents
The policy works if the exception count falls while delivery does not slow down. Measure both. A policy that cleans code by freezing releases has failed too.
Watch four signals:
Live exceptions over time. Should trend down. Flat or rising means renewals are rubber-stamped.
Lead time for changes. If it jumps after you switch the gate on, the rule is too strict or the approval step is a bottleneck. Delivery and DORA metrics from your GitHub or Bitbucket history show this directly.
Review delays. Exception approvals that sit for days are a queue you created. Look at time to first review on pull requests that carry an exception record.
Repeated feedback. If reviewers keep writing "please split this function" on agent-written pull requests, the standard is not reaching the agent. Move the rule into the shared standards your agents read before the PR exists.
Coderbuds surfaces lead time and deploy frequency from your repositories, so you can see whether tightening the limit moved delivery. Patchstack, a 12-engineer application security team, cut lead time 75% and raised deploy frequency 20x in six months. A quality policy and fast delivery don't conflict when the process is light.
Review the policy itself after one quarter. If nobody has retired an exception, the expiry is too long or the owners have no time.
Start with a baseline and a delivery number this week
Code complexity exceptions are safe when they are small, owned and temporary. Freeze the legacy baseline, hold changed code to the limit, and require the five-field record for every exception. Then check that lead time and review delays did not get worse. To see that delivery side, connect one repository to Coderbuds on the 30-day free trial (no card needed) and record your lead time before you switch the gate on.