Why your AI keeps making the codebase messier

I went looking for one number in a vibe coded app and found four of it. The same calculation, a single percentage, written four separate times in four different corners of the codebase, each one a little different from the others. Three of them landed on the same answer by luck. The fourth had a quiet fallback: when its input failed to parse, it returned fifty percent and kept going. No error, no log line, nothing red. It just handed a made-up number to everything downstream and let the app keep running on data it had invented.

The reason that happens, and the reason a vibe coded codebase tends to get harder to build on over time instead of easier, is that your codebase is an input to the agent, not only its output. The model writes fast because it reads what's already there, matches the pattern, and extends it. When what's already there says the same thing four different ways, the agent has to pick one, and it picks the closest one, not the correct one. The fix that actually holds is one source of truth per rule: one canonical place each calculation or decision lives, so the next round of generation reasons from a single definition instead of guessing between four.

why does vibe coding make a codebase messier over time?

Because generation compounds on generation. The first version is clean enough. Then you ask for a feature near that logic, and the agent reimplements a piece of it locally instead of finding the original, because reusing code requires knowing it exists and generating code doesn't. Now there are two versions. A week later there are four, and one of them has a subtle difference nobody chose. This is a real feedback loop, not a willpower problem you can scold yourself out of: messy generated code is a worse input, a worse input produces messier generation, and the mess accelerates. The agent isn't degrading because the model got worse. It's degrading because you're feeding it its own worst output and asking for more.

Name the pattern so you catch it early. Every duplicated rule is a fork in the truth, and a coding agent resolves forks by proximity, reaching for whichever copy is nearest in the file it's already editing. Four implementations of one formula isn't four times the code. It's four chances to be silently wrong, and you find out which one broke only when it defaults to fifty percent in production and nobody gets an error.

how do I keep my AI from turning the codebase into a mess?

Give every rule one home, and make your delegations read before they write. This is spec-first discipline, and it's less about writing documents than about deciding where truth lives before you generate against it. One calculation, one function, one file that owns it. Everything else calls that, nothing reimplements it. When you hand the agent a task, the instruction isn't only what to build; it's where the relevant truth already lives and to read it first. The build move I trust most is exactly this: each task is a delegation with the exact files to read named up front, so the agent extends the one real implementation instead of inventing a fifth.

That single decision is what keeps a vibe coded app buildable past week three. It doesn't slow you down the way it sounds like it should, because the expensive part was never the typing. The expensive part is the silent divergence you find two months later, the afternoon you burn proving which of the four percentages is the real one. Deciding the formula has exactly one home costs a sentence in a spec. Not deciding it costs you that afternoon, plus however long the synthetic data was live before anyone noticed.

what breaks with this approach

The trap is chasing the wrong win. There's a tempting headline going around that clean code makes the AI dramatically better, and if you read that as a correctness gain you'll refactor for weeks and feel cheated. The honest measurements point somewhere quieter: cleaning up mostly moves how many tokens the agent burns and how often it has to re-read a file to find its footing, not whether the code it writes is correct. Refactor for navigation cost, the thing you and the agent both pay every time you touch the code, and you get a real return. Refactor chasing a pass-rate jump nobody actually measured and you get a surface number read as the wrong kind of win.

The other thing that breaks is the instinct to do it all at once. You don't need to dedupe the whole codebase this weekend. Pick the rules that would hurt if they were silently wrong, the money math, the access checks, the thing that decides who sees what, and give those one home first. A cosmetic pass across everything feels productive and protects nothing. One canonical implementation of the calculation that quietly defaulted to fifty percent would have protected the whole app.

questions that keep coming up

How do I find the duplicated logic without reading everything? Ask the agent, but ask it to search, not to remember. Have it grep for the calculation or the rule by name and list every place that logic appears, then read that list yourself. The point of the exercise isn't the cleanup, it's seeing how many homes your truth already has. Most people are surprised by the count.

Isn't reimplementing sometimes fine, even faster? Duplicating a throwaway helper is fine. Duplicating a rule is not, and the test is whether being wrong matters. If two copies of a formula drift, does someone get charged wrong, see the wrong data, or get access they shouldn't? If yes, it needs one home. If it genuinely doesn't matter when the copies disagree, it was never a rule, just a convenience.

Do I need a big spec system to do this? No. Spec-first discipline here is one habit for the vibe coder: before you generate against a rule, decide where that rule lives and tell the agent to read it first. That's a sentence, not a system. The document is optional; the single source of truth is not.

If you're shipping product with AI and you keep finding the same logic implemented three different ways, and you want a sparring partner on getting the codebase back to one source of truth, /work-with-us.

// part of the custom apps topic

// grab the free starter kit that makes your AI stop forgetting and stop guessing: get it →

// building with AI? the field manual has the structured lessons.

// hitting this on a real build? this is what I fix →