The first post in this series was about the pipeline a change goes through on its way to production. The second was about how we check each change without a person reading it. Both of those are about one pull request at a time. This post is about the codebase as a whole, and what happens to it after a few months of hundreds of changes a day.
Left alone, it gets worse. That isn't because agents write bad code. Most of what they write is fine. The problem is that they learn from whatever is already there, so a mistake that slips through once tends to get copied, and a rule that only exists in someone's head gets broken over and over by agents who were never told about it. At human speed, a team notices and cleans up. At agent speed, the copies pile up faster than anyone can find them.
When I say a self-correcting codebase, I mean something fairly specific. When something goes wrong, the fix shouldn't stop at that one change. Something should be left behind that makes the same mistake less likely next time, and ideally impossible. And the system should be finding some of its own problems, rather than waiting for a customer or an engineer to trip over them.
We've been working on this for most of this year. Some of it worked well, and one fairly large piece of it we built, ran for two months and then switched off. I'll cover both.
Every mistake should leave something behind
In July, one of our services started blocking legitimate requests as if they contained personal data. When we dug in, the cause turned out to have nothing to do with the feature that looked broken. The test suite had been loading a developer's local settings file, which pointed it at a real database, and tests had been writing records into it for a while. By the time we found it there were 164 records in that database that no person had ever created, and some of them were changing how the service behaved.
Fixing the immediate problem was the easy part: delete the stray records and move on. If we'd stopped there, though, it would have happened again within weeks, because nothing about the setup had changed. So the fix that actually merged did four things. It made the test runner ignore local settings files entirely. It added a helper that gives each test that needs a database its own throwaway tenant, created at the start of the test and removed at the end. It added a migration that repairs any data left behind by the old tests, written so it's safe to run more than once. And it came with a short write-up of what happened and why.
The same day, the rule "tests that write to a database get their own isolated one" went into the shared engineering standards that every one of our repositories, and every agent working in them, reads from. One incident in one service became a check in that service and a rule everywhere else.
That's the basic loop, and it's the one I described in the first post: when you find a mistake, you fix the change, then you ask where the rule should live so nobody has to catch it by hand again.

What I didn't say in the first post is how much of the work is in that last step, and how easy it is to get it wrong.
A rule written down somewhere isn't the same as a rule that's enforced
In August, Bugspray's pull request check stopped working for one of the teams using it. Every webhook coming in was being rejected, and nothing in the code that handles webhooks had changed. What had changed was two database migration files that had already been applied, and then edited afterwards. The tool that runs our migrations keeps track of which ones have been applied by file name, so it saw two files it had already run, skipped them, and the edits never reached any database. The code expected the new tables, the database still had the old ones, and every request failed.
The fix was a new migration that rebuilt the tables properly. The part I want to point at is the rule. "Never edit a migration that's already been applied" is about as well known as rules in our industry get, and we already had it written down, in our standards for a different database in a different repository. It didn't help, because whoever was working on Bugspray had no reason to read that document and nothing ran that would have stopped it.
Compare that with a problem we'd had with metrics a few weeks earlier. Our services had started tagging metrics with values that could be almost anything, like IDs, which is a quiet way to run up a monitoring bill. We cleaned it up, and then added a guard in the code that sends metrics. In development and testing it rejects any tag that isn't on an approved list, so the mistake fails loudly the first time someone makes it. In production it drops the tag and counts how often that happens, so nothing breaks for customers but we can see it. There's no document anyone needs to remember. The wrong thing just doesn't work.
That's the rule ladder from the first post doing its job. A check in the code holds every time. A note in a standards document holds when the right agent happens to read the right document, which at our volume means it fails regularly. When we write a rule down now, we treat that as a stopgap until we can turn it into something that runs.
Teaching the agents that check our work
Bugspray's exploration agents are a good example of a system correcting itself, partly because the mistakes were the agents' own.
These are agents that use the product the way a person would, looking for bugs, and filing a finding when they think they've found one. Early on, a lot of what they filed wasn't a bug. Over about two weeks in August, we went through the bad findings one by one and asked what rule would have stopped each of them.
Some were findings where the steps to reproduce weren't something any real user would do, so findings now have to describe a realistic scenario. Some were "bugs" triggered by typing a URL straight into the address bar, skipping the screens a user would normally go through, so jumping straight to a URL no longer counts as a way to trigger a bug. One batch claimed forms were broken because the agent couldn't interact with checkboxes the page had deliberately hidden behind custom controls, so hidden controls can no longer be used as evidence. Some were duplicates of bugs already filed, so the agent now has to show that it searched before it files. And every finding now has to include the agent's reasoning and how confident it is.
Each of those changes went into the instructions the agents follow, and each came with a test that checks the instructions still say it. That second part matters more than it sounds. Instructions for agents get edited all the time, often by other agents, and a rule that's quietly removed during an unrelated cleanup is just as gone as one that was never written. The test makes removing it a deliberate decision.
The latest change in the same spirit came this month. When an agent opens a pull request to fix a bug Bugspray found, the pull request now has to link back to the finding it fixes. That closes the loop the other way: when the fix merges, the finding knows about it, and if the bug ever comes back, the replays I described in part 2 reopen it automatically rather than leaving someone to notice.
The experiment we turned off
For about two months this summer, we tried to make learning automatic.
The idea was simple and, I still think, reasonable. Every time an agent finished a piece of work, before it was allowed to push, a check ran that asked whether anything in this change was worth remembering. If there was, the agent wrote it up as a short lesson and opened a second pull request adding it to a shared library that every agent on every repository could read. If there wasn't, it recorded that it had checked. Either way, the push couldn't go through until the question had been answered for that exact commit.
It worked in the sense that it did what it was designed to do. Between late July and late September, it produced 111 of those lesson branches across five repositories. The shared library grew quickly.
It also grew in ways we didn't like. Lessons overlapped and sometimes contradicted each other. A single document about one design pattern got to more than 900 lines. Agents were reading more and more text before starting work, and it wasn't obvious they were doing better work for it. Much of it described the code as it was on the day of the lesson, which went stale as soon as the code changed. And because the check sat in front of every push, every pull request paid for it, whether or not it had anything to teach.
In September we switched it off and deleted most of what it had produced. The number of standing conventions went from 20 to 17, the longer knowledge documents went from 20 to 4, and one test that had been checking for 16 specific phrases in a standards document was cut down to check for 2. The guidance that survived was the part reviewers actually used, and it now lives where reviews happen.
What I took from it is that a codebase doesn't get better by remembering more. Context is limited for agents in the same way attention is limited for people, and every rule you add competes with every other one. The lessons that turned into checks kept paying off. The ones that stayed as text mostly turned into something agents had to read past. So we stopped asking "what should we write down?" and started asking "what should we stop being able to do?"
How we decide what becomes a rule now
What replaced it is slower and more deliberate, and it works better.
Every so often, someone runs a review of the last few weeks of pull requests across a repository: the review comments, the findings from our automated reviewer, and the cases where an agent got stuck and asked for help. The review looks for themes, and a theme only counts if it shows up in at least two separate pull requests. One mistake is an accident. The same mistake twice is a gap in the system. For each theme it drafts a proposed change, which might be a new check, an edit to the instructions agents read, or the removal of a rule that isn't pulling its weight. A person reads the proposals and decides which ones go in. Nothing gets added to the standards on its own.
I'd underline that last part. Agents are very good at noticing patterns across hundreds of pull requests, far better than any of us. They're less good at judging whether a pattern is worth a permanent rule, and a rule that's wrong does more damage than one that's missing, because every agent follows it. So agents propose and people decide.
Pruning is part of the job too. Every rule we add is something every agent has to process, so when we find one that never fires, or that a newer check already covers, we take it out. The standards file in a healthy repository gets shorter over time as more of it turns into code.
Finding problems nobody reported
Everything so far starts with someone noticing a mistake. The other half of self-correction is the system looking for problems on its own, between pull requests.

Some of this runs on every change. An automated reviewer reads every pull request and posts what it finds as a comment. It never approves anything, which keeps it honest; it's there to make the human or agent reading the change faster, not to replace them. The checks that came out of earlier mistakes, like the metrics guard and the migration checks, run here too.
Some of it runs on a schedule. Our full end-to-end suite runs every night against the main branch, separately from the pull request checks, and when something fails it updates a single tracking issue rather than opening a new one every night, so there's one place to see what's broken and for how long. That suite also treats a test that only passes on a retry as a failure, so flakiness shows up as a problem to fix instead of being quietly retried away. Another job checks every day that the pricing information we rely on from AI model providers still matches what they're actually publishing, because that's the kind of thing that changes without anyone telling you. Our monitoring alerts are defined in the repositories as code, so they get reviewed like any other change and can't drift away from what the code actually does.
And some of it is exploration. Bugspray's agents can be pointed at the product to go looking for bugs in the parts of the product that have the least coverage, and what they find goes through the same validation and the same loop as everything else: a confirmed finding becomes a replay that runs against future pull requests, and if the team turns it on, a ticket and a first attempt at a fix.
I'll be honest about where this is thinnest. We don't yet have anything that scans for duplicated logic or code nobody calls anymore, which is exactly the kind of slow drift I described in the first post. That's the part of the system we're working on next, and I expect it to look like everything above: start with an agent that proposes, put a person in charge of deciding, and turn the decisions that keep coming up into checks.
Where to start
If I were starting with a team today, this is roughly the order I'd go in:
- After every incident or bad pull request, ask where the rule should live, not just how to fix the change. Write the answer into the fix itself.
- Push rules up the ladder. A written rule is a placeholder for a check. Keep a list of the ones you haven't turned into checks yet, and work through it.
- Test your agent instructions. For every rule that exists because of a real mistake, add a small test that fails if someone removes it.
- Look for patterns on a schedule, not on every change. Review the last few weeks of review comments and stuck agents, and only act on themes that show up more than once.
- Prune. Every quarter, remove rules that never fire or that a check already covers. The instructions should get shorter as the checks get better.
- Run something every night that nobody asked for: the full end-to-end suite, a contract check against the services you depend on, or an agent exploring the least-tested part of your product. Make sure what it finds lands in one place a person actually reads.
None of this is very glamorous, and most of it is about deciding what not to do. But it's what lets a small team keep shipping at this volume without the codebase getting harder to work in every month, which, in the end, is the whole point of the series.
This is the work we do at ES Consulting. We help teams build the loop that turns each mistake into a check, set up Bugspray to find and replay the bugs nobody reported, and do it inside your codebase alongside your engineers, so what's left behind is a system your team owns. If your codebase is getting harder to work in as your agents speed up, we'd be glad to compare notes: esconsulting.io.
