DEV Community

Cover image for Keeping code clean at AI speed
HybridTechie
HybridTechie

Posted on AI-assisted

Keeping code clean at AI speed

Engineering at AccuMate, part 2

We build AccuMate, software for accounting practices, with AI agents. They write code faster than any of us can read it. That moves the bottleneck. Typing the code is no longer the slow part. Knowing that the code is right is.

This post is about how we handle that. The short version: no single check catches everything, so we stack many plain checks, cheapest first, and we treat every mistake that gets through as a missing check.

Every check has holes.

A linter cannot see a missing permission. A unit test cannot see a requirement nobody wrote down. A human reviewer gets tired on the fortieth file. An AI reviewer is confident about things it has not checked.

So we do not look for one check that is good enough. We stack checks so that a mistake has to find a hole in every one of them at once. If you know the Swiss cheese model from safety engineering, this is that.

Two stages of checks, with owners

The checks sit in two stages, dev and QA, with the merge between them. Every area of the product has two named owners: a dev owner and a QA owner. When something in an area goes wrong, nobody has to ask whose it is.

The dev stage: cheap checks first.

Rules the AI reads before it writes.

Our repositories have a rules file that the coding agent reads first. It says how the code is layered, how errors are handled, and the rules that must never break, such as one accounting practice never seeing another practice's data. This is not a check in itself. It is the ground the checks stand on.

Machines catch the cheap mistakes in minutes.

Machines stop the cheap mistakes

  • Secrets. A secret scanner (gitleaks) runs before the commit and again in CI.
  • Form. Formatting, linting, type checks and a production build of each front end.
  • Schema. Every database migration has to upgrade, downgrade to the base and rebuild cleanly.
  • Architecture. 551 architecture tests check the structure of the backend. For example, every route has to ask who is calling before it answers, and client data is fenced per route.
  • Scans. Dependency audits and Semgrep check our code and the packages we use.

551 architecture tests

The architecture tests matter most for AI-written code. An agent will happily write a working route that skips a rule it did not notice. A test that encodes the rule does not get tired and does not forget.

AI reviews, and another AI tries to disprove it.

One AI reviews, a second tries to disprove each finding

Reviewer agents read the change against our rules. They have read-only tools: they can read and search the code, and they cannot edit it.

Narrow reviewers raise false alarms. So when agents review a pull request, each finding goes to a second agent whose only job is to disprove it: it opens the cited lines, looks at similar code elsewhere, and runs the reproduction where there is one. It returns "confirmed", "drop" or "unproven". Only confirmed findings reach a person.

This is a step we run, not a CI gate.

Two people sign off.

The author and a reviewer both sign off. Our pull request template has one line we take seriously:

An agent wrote it, I'm the one accountable for it.

Then the backend suite runs. It has more than 25,000 tests, and they pass before the change merges.

More than 25,000 tests pass before the merge

The QA stage: someone decides what correct means.

The QA owner decides what correct means for their area. That is a judgement about the product, not about the code, and it is the one thing we do not hand to the agent that wrote the change.

QA tests live in a separate repository

Our QA tests live in their own repository, apart from the product code. That separation is deliberate. The commit that breaks a test cannot also quietly rewrite it to pass.

More than 10,000 automated QA tests run against a real copy of the app, with nothing faked: a real backend and a real database. Then UAT, and the QA owner signs off. That makes three people.

More than 10,000 automated QA tests

When the holes line up.

Sometimes a mistake gets through every check

Sometimes a mistake gets past every check. When that happens, we close the gap before we fix the bug:

  1. A test that fails without the fix.
  2. A line in the rules the AI reads, so the next agent does not repeat it.
  3. Where we can, a new automatic check.

We put the new check as early as we can, in the dev stage, where it is cheapest to run. A bug found late becomes a check that runs early.

A late bug becomes an early check

The checks keep running after the merge.

Deep scans recheck code that already passed: sweeps across every route, security scans, and an evaluation suite for the Assistant.

Agents also pay down old debt. When we add a rule that existing code breaks, we do not wait for the code to be perfect. The current exceptions go on a frozen list, and a companion test fails on any entry that is no longer needed. So a new exception cannot be added, and a fixed one has to come off the list.

The old debt list can only shrink

In outline, the pattern looks like this (simplified, not our code):

# The files that broke the rule when the rule was added. Never add to this list.
KNOWN_EXCEPTIONS = frozenset({"billing/legacy_export.py", "reports/old_summary.py"})

def test_rule_holds_everywhere_else():
    offenders = {f for f in source_files() if breaks_rule(f)}
    assert offenders <= KNOWN_EXCEPTIONS, f"new code breaks the rule: {offenders - KNOWN_EXCEPTIONS}"

def test_no_stale_exceptions():
    stale = {f for f in KNOWN_EXCEPTIONS if not breaks_rule(f)}
    assert not stale, f"fixed, so take these off the list: {stale}"
Enter fullscreen mode Exit fullscreen mode

The second test is the ratchet. Without it, the list rots into something nobody reads. With it, the list can only shrink.

The product works the same way.

AccuMate has an Assistant that prepares work for accountants. It runs on the same idea, aimed at the client's numbers instead of our code.

Each figure is checked against its source page

When the Assistant reads a figure from a client's document and cites the page, code (not a model) looks for that figure on that page. If it is not printed there, the figure is flagged for the accountant. When the Assistant writes to a client about what a document shows, the message has to carry a quote, and the quote has to be found in that document, or the message is refused.

An accountant signs off before anything goes out

Then an accountant reviews the work and signs off before anything goes to a client or the ATO. The Assistant prepares. The accountant decides, and stays accountable.

Agents write the code. We write the checks.

Agents write the code. We write the checks.

Andrej Karpathy's essay "Software 2.0" argued that more software would be written by optimisation rather than by hand, and that people would spend their effort on the goals and the data. We see a version of that every day. Agents write the code. Our job is to say what correct means, and to build the checks that hold the code to it.

What we would tell another team starting out:

  • Order checks by cost. Run the cheap ones first and often. Save people for the questions only people can answer.
  • Encode rules as tests. A rule in a document is a hope. A rule in a test is a check.
  • Keep the tests away from the code they test. The change that breaks a test should not be able to edit it.
  • Make the AI disprove itself. A second agent that tries to refute each finding removes most of the noise.
  • Treat every escape as a missing check. Fix the bug, and add the check that would have caught it, as early as you can.

Part 1 of this series, on the system that builds AccuMate, is here: The system that builds AccuMate.

Top comments (0)