In chapter 11, I showed you our CI. Two days later, SonarCloud's dashboard showed me what it let through.
Everything worked as planned. The gates were required, the PRs waited. But several Sonar gates were red on main, almost always for the same family of reasons: code too complicated, small mistakes an editor could have flagged. The CI held the gate, but it held it too late. The agent was done, the PR was open, and ten minutes later we learned everything had to be redone.
This chapter tells the move: from "the CI catches it" to "the error never leaves the editor".
What the dashboard showed me
I asked an agent to read, repository by repository, why the gates were red. The answer fit in one sentence. Nearly all of them failed on new code, and nearly always on cognitive complexity, Sonar's S3776 rule: a function with too many nested things to be read without effort.
I did not lower a threshold, and I did not mark anything "won't fix". The functions were split, repository by repository, in short PRs. In krizaka-ui, that same pass revealed 37 bugs, sixteen of them sorts without a comparison function. A sort without one sorts as text: [10, 9, 1].sort() gives [1, 10, 9]. Nobody had seen it because it almost always worked.
I could have stopped there. But looking at the list, I told myself none of it needed to wait for Sonar. A lint rule finds the same thing in a second, while the agent is writing.
Complexity, before the PR
The first step was to bring that rule into the editor. @krizaka/config 0.11 adds cognitive complexity to krizakaStack(), at the same threshold as Sonar, 15. A counter joins our ratchet: functions already over the threshold are frozen at their current number, and the CI refuses to add a single one.
The principle is the one from chapter 11. But where the ratchet used to count our own rules, it now counts a rule that also exists at Sonar. What Sonar would say ten minutes later, the lint says right away.
And the rollout broke, of course. Updating the repositories, we saw that a command, krizaka-ratchet --update, silently erased the counters it did not measure, such as the page-template one, specific to each repository. Nothing failed. A counter vanished, and the guard it held went with it. That is krizaka-ts#163: the command now keeps what it does not know and says so in its report. A test holds it.
That is what I kept from this first step. A tool that erases a guard without a word is worse than a missing guard.
All of Sonar at lint
One rule was not enough. 0.12 turns on the full eslint-plugin-sonarjs set, the engine of SonarCloud's JavaScript and TypeScript rules. A Sonar error becomes a lint error.
There was debt, of course. In each repository we counted what the rule already found and froze it. The numbers are recorded in the repository and can only go down, like the others. I did not want to fix everything at once, for the same reason as with ratchets: the rule protects new code immediately, and the existing debt goes away little by little.
For Java, the same idea is written differently. In krizaka-build#107, the shared parent runs PMD and SpotBugs with its security plugin in verify, with a baseline per module. And krizaka-build#106 adds security rules, written out in an ADR, to our ArchUnit architecture tests.
That last step had its own failure. A repository without SpotBugs's exclusions file broke on its first build. krizaka-build#108 makes the file optional and adds a canary job, which builds an empty project against the parent on every change. We learned not to publish a parent without having a consumer try it.
Before
- Sonar answers on the PR, ten minutes later
- Gates red on new code, mostly cognitive complexity (S3776)
- A pass in krizaka-ui finds 37 bugs, 16 of them sorts without a comparison
- The agent has already moved on
Now
- @krizaka/config 0.11: cognitive complexity at lint, threshold 15
- 0.12: the full eslint-plugin-sonarjs set, a Sonar error is a lint error
- The existing debt is counted and frozen: it only goes down
- Sonar stays, as the last net
Secrets and workflows, as early as possible
There are things a CI should never have to catch. A secret pushed to a repository, for example: once it is in the history, it is burnt.
.github#334 puts gitleaks and zizmor as early as possible. First a hook on every commit, with one configuration for all repositories. Then a CI job that reads the whole history. After that come GitHub's push protection, then the Sonar rule on secrets, as the last net. zizmor does the same for our workflows, which are code like the rest and can be attacked like it.
The first pass over the main repositories found no real secret, only test values and one dummy value used by the CI, which I allowed with a reason written beside it.
Alongside, I did a less glorious cleanup. In the repositories cloned on our machines, 88 tokens were sitting in the remote address, where a git remote -v shows them to anyone looking over your shoulder. As far as I know they leaked nowhere. But they had no business being there. I removed them, and the agents now use a token they ask for at the moment they need it, never written into an address.
One source for the rules
Moving checks toward the editor carries a risk: each repository keeps its copy, and the copies drift apart. We had already lived that with CI files.
The right move is the same as in chapter 11. 0.13 of @krizaka/config ships the shared ast-grep rule pack, along with the krizaka-sg-ratchet and krizaka-seo-family commands. No repository should keep its own copy of the ratchet or its own rules. Versions 0.11, 0.12 and 0.13 came out the same day, October 27.
The race nobody saw
One day, Yomizaka's main branch turned red. Thirty lint warnings against a cap of 27. Yet every PR had been green when it was merged.
The explanation is silly. Two PRs were each green on an old base. The first merged and set the cap. The second, started earlier, merged afterwards without being checked again on the new main, and added its warnings. No PR was at fault, and the sum was.
The guard is a GitHub setting: require the branch to be up to date before merging. I turned it on for Yomizaka, then for 12 repositories, with a small script that updates PRs waiting on auto-merge. I first believed SonarCloud only answered on PRs. That is true of its automatic analysis; run from the CI, its analysis also answers on the merge queue's branches. I checked it on one repository: the queue works, Sonar included. So we are moving to the merge queue, which keeps the same promise without updating PRs by hand.
- Two PRs (green on the same old main)Each is checked against the main it started from.
- The first merges (the cap is set at 27)Main is green, with its lint warnings counted.
- The second merges (not checked again)Started earlier, it merges without being checked on the new main, and adds its warnings.
- Main turns red (30 warnings, cap 27)No PR was at fault. The sum was.
- The guard (up to date before merging)Turned on for Yomizaka, then 12 repositories; now the merge queue, which keeps the same promise, Sonar included.
A fix without a guard is not finished
I added a rule to our CI, in .github#336: a PR that fixes a bug must add a test that would have caught it, or a rule. Without that, the check fails.
The rule is not a punishment. It forces the question I had made a habit in chapter 11: why did the guards not see it? Without it, I forget, and so does the agent.
In the same change, PRs that only touch documentation now go through a light Sonar scan, instead of rerunning the whole CI. A text PR does not need a Docker build, but it has the right to be checked.
The two paths, drawn
Someone asked me to show the flow and the folders, so here they are: where a change goes, and where the guards live. Two real examples, drawn from the workflow files. The arrows give the order of the story, the list under each drawing gives the detail.
Krizaka UI first. A change goes from the editor to npm.
- Editor (the error shows first)Types and lint rules answer while I write, before anything is committed.
- pre-commit (gitleaks, zizmor).pre-commit-config.yaml runs the org's two hooks on the staged files: a secret never gets into the history, a workflow is read before it is pushed. Krizaka UI has no pre-push hook; Akinaza has one, below.
- Type-check, test, build (turbo run check)One turbo run check per package: lint (with the ast-grep rules and their ratchet), types, tests, build, the size limit, publint. The Storybook screenshots are compared pixel by pixel with the baselines, and a failure uploads the diffs.
- Documentation site (ui.krizaka.com)Build, lint, types, tests and the family's SEO contract on the export. Then the crawl of a sample of pages, in three slices, and the layout and motion checks, in a browser, dark and light, desktop and phone. A last job, Documentation site (ui.krizaka.com), gives the verdict.
- Duplicates, realtime (two more jobs)Duplicate code, held by dup-ratchet.json, and the realtime adapters tested against a real PostgreSQL and a real Valkey.
- Security scan (gitleaks, zizmor)The same two tools, this time on the whole pull request. CodeQL runs next to it, enforced by an organisation rule.
- SonarCloud (automatic analysis)For Krizaka UI, Sonar analyses the repository on its own side, not in our workflow, and its answer is required. This automatic analysis does not answer on the queue's branches; the CI analysis does, which is why the repositories are moving to it, as krizaka-ts already has.
- Merge gate (merge queue, checks green)The PR enters the merge queue. The checks, Sonar included, run on the queue's temporary branch, then it merges.
- main (squash, linear history)GitHub merges when everything is green. Nobody pushes to main.
- Version Packages (a PR from Changesets)Changesets keeps a "chore: version packages" PR up to date with the versions and the changelogs. Merging it is what releases.
- Release (npm, provenance)release.yml publishes the packages. Each one tries npm's OIDC trusted publishing first, with provenance; a token is only a logged fallback for a package that has no trusted publisher yet. Then a job fails if a version on main is not on npm.
One thing is not on this path: the "fix needs a guard" job of .github#336. It belongs to the shared Node pipeline, and Krizaka UI keeps its own ci.yml. I prefer to say it than to draw a box that does not exist.
Where the guards live in that repository:
krizaka-ui/
├── .pre-commit-config.yaml gitleaks and zizmor, before each commit
├── .github/workflows/
│ ├── ci.yml check, documentation site, duplicates, realtime
│ ├── security.yml gitleaks and zizmor on the PR
│ ├── release.yml Version Packages, npm, provenance
│ └── update-baselines.yml renews the screenshot baselines
├── rules/ and sgconfig.yml the ast-grep rules
├── sg-ratchet.json their debt, which only goes down
└── dup-ratchet.json the duplicated lines, same ideaNow the Akinaza shop app, in apps/mobile. It is an Expo app, and the path is longer on the test side.
- Editor (the error shows first)Same idea: types and lint answer while I write.
- pre-commit (gitleaks, zizmor)The same two hooks, from .pre-commit-config.yaml.
- pre-push (pnpm run check).githooks/pre-push makes the install match the lockfile, then runs lint, types and tests. We never push to see what CI says.
- Lint, types, tests (Mobile CI, side by side)mobile-ci.yml calls the shared Expo pipeline: ESLint with a cap on warnings and the ratchet of apps/mobile/lint-ratchet.json, tsc, the Jest tests and the bundle export. The four run side by side, and only when the app, the contracts it reads or the workflow changed.
- mobile-e2e (Maestro on an emulator)The shared workflow builds a release APK, starts an Android emulator and plays the Maestro flows of apps/mobile/.maestro against a throwaway backend, signed in as the demo shop's owner. It always reports mobile-e2e / gate.
- Security scan (gitleaks, zizmor)The same scan as the other repositories.
- CI gate (Sonar, fix needs a guard)The repository's main pipeline also runs on the PR, with its CI analysis for Sonar. It carries SonarCloud after the tests, the duplicates and the "fix needs a guard" job, and gives one verdict, ci / CI gate.
- Merge gate (merge queue, checks green)The PR enters the merge queue. The checks, Sonar included, run on the queue's temporary branch, then it merges.
- main (squash)GitHub merges when everything is green.
- Showcase videos (on main, never required)When Mobile CI is green on main, download-videos.yml records the web and phone showcase journeys, and the org's workflow sends them to the CDN and opens a PR for the download page. If it fails, nothing is blocked.
And where the guards live:
akinaza/
├── .pre-commit-config.yaml gitleaks and zizmor, before each commit
├── .githooks/pre-push lint, types and tests, before each push
├── .github/workflows/
│ ├── mobile-ci.yml Expo pipeline and mobile-e2e
│ ├── ci.yml main pipeline: Sonar, duplicates, fix needs a guard
│ ├── security.yml gitleaks and zizmor on the PR
│ └── download-videos.yml showcase videos, on main
├── apps/mobile/
│ ├── lint-ratchet.json the app's lint debt
│ └── .maestro/ the flows (smoke, tabs, invoices...)
├── rules/ and sg-ratchet.json ast-grep rules and their ratchet
├── dup-ratchet.json duplicated lines
└── perf-budgets.json the web's JavaScript budget per pageWhat is not settled
As I write, three Sonar gates are still red on main: krizaka-ui, krizaka-blog and the .github repository itself. I am working on them, and I did not bypass them.
The Sonar rules that follow data from end to end, the "taint" ones, have no local twin. They show in the editor through connected mode, and Sonar remains their only gate. And some type-aware rules only fire with the full TypeScript configuration: that is next.
What I take from it
A CI catches errors. But the later it catches them, the more they cost, because the agent is done and the PR is already open. The idea is to lift each check one notch, toward the commit, toward the editor, toward a rule written once for every repository.
And when a bug gets through anyway, the question does not change: what should have seen it, and where? The answer is a guard, never "pay more attention".
