A new kind of Gen 1 has arrived. It was not written over ten years by people who have since left. It was generated over a few weeks with AI coding tools, by one person or a small team, to find out whether anyone wanted the product. Somebody did. Now it has users, data and revenue, and it has to last.
The cause is new but the situation is not. It is legacy code: software that has to be changed by people who do not know why it is the way it is. With older systems the author has left. Here, many decisions were never made by a person at all, so nobody can be asked. The usual Gen 2 question just arrives early: keep it, fix it, or start over?
Why this Gen 1 is different
Three things set a generated v1 apart from an ordinary old system.
- It is young. Fewer of its odd branches are hard-won bug fixes, and more are accidents.
- It is inconsistent. Parts produced in separate sessions may do the same job three different ways.
- It is production anyway. Michael Feathers put it this way: "When a system goes into production, in a way, it becomes its own specification." Users and integrations now depend on what the code does, including the parts nobody intended.
The third point is the one teams skip. The code may be weeks old, but its behavior is already a contract.
Step 1: audit what exists
Before deciding anything, find out what is there. Reading comes first, and it is the hard part. Joel Spolsky's line from 2000 applies without change: "It's harder to read code than to write it."
The audit answers three questions.
- What does it do? List every route, screen, scheduled job and webhook. Compare the list with what the product is believed to do. The gaps in both directions matter: features nobody knew were there, and features everyone assumed were there.
- What does it depend on? Every package, external API, hosted service and platform feature. Note which are pinned to a version, which are still maintained, and which are used for one function that could be removed.
- What secrets and data does it touch? Every key, token and connection string, and where each one lives. Every table and bucket, what personal data is in it, and which code paths can read or write it.
The output is a short written inventory. It is the first documentation the system has ever had, and every later decision leans on it.
Step 2: build the test harness first
A generated v1 usually has few tests, or tests that were generated alongside the code and assert whatever the code happened to do without anyone checking it. Either way, there is no safety net yet.
The tool for this is the characterization test. Feathers defines the purpose plainly: "The purpose of characterization testing is to document your system's actual behavior, not check for the behavior you wish your system had."
His method is mechanical:
- Write a test with a dummy expected value.
- Run it. The failure shows what the code really produces.
- Put the real output in as the expected value, and name the test for the behavior observed.
- Repeat with new inputs.
Do not fix anything at this stage, even obvious bugs. Record them. The point is to know when a later change alters behavior, whether or not that behavior was right. Feathers notes that the hardest part is usually breaking dependencies so the code can run in a harness at all.
Start where the money and the data are: sign-up, login, payment, anything that writes to the database.
Step 3: keep, refactor or regenerate
With an inventory and a harness, each part of the system can be sorted into one of three piles. These are the same choices as the four real options, applied one module at a time.
- Keep. It works, the tests pin it down, and nobody needs to change it soon. Ugly is not a reason to touch it.
- Refactor. The behavior is right but the code is hard to change: duplicated logic, three patterns for one job, no clear boundaries. Improve it in place, behind the tests.
- Regenerate from a written spec. The behavior itself is wrong or unknown, and the module is small and has clear edges. Write down what it must do, including the cases the characterization tests uncovered. Then rebuild it, by hand or with the same tools, and hold the new version to the spec and the tests.
The spec is what makes the third pile different from how the v1 was built. The replacement comes from a document a person has read and agreed to, so the next maintainer has something to ask.
Step 4: add what is missing
A v1 built to prove demand tends to cover the path where everything goes right. The audit usually turns up the same four gaps.
Authorization checks. OWASP describes the goal as access control that "enforces policy such that users cannot act outside of their intended permissions." Its guidance is to deny by default except for public resources, to implement access control once and reuse it, and to enforce record ownership so a user cannot read or change another user's records by supplying a different identifier. Check every route in the inventory against that, including the write and delete endpoints.
Input validation. OWASP's guidance is to validate on the server even when the browser checks the same fields, and to define what is accepted instead of trying to list what is malicious. It is also clear that validation does not replace the specific defenses: parameterized queries for SQL injection and output encoding for cross-site scripting.
Error handling. Find every place a failure is swallowed, retried forever or shown to the user as a raw stack trace. Decide what should happen in each.
Observability. Logs, metrics and alerts, so that production problems are found by the team and not by customers. OWASP's access control guidance includes logging access control failures and alerting when appropriate.
Each of these is new behavior, so each gets its own tests.
Why "just regenerate all of it" is still a rewrite
The tempting argument goes like this: the first version took three weeks to generate, the tools are better now, so generate a clean one and throw the old one away.
That is a rewrite. Cheap typing does not change what makes rewrites fail.
- The knowledge loss is the same. Spolsky's warning was that throwing code away throws away what it learned. A young codebase has learned less, but every production fix made since launch is in there, and none of it is in the prompt.
- The behavior is still a contract. Customers, integrations and stored data depend on what the system does today. A regenerated system has to match it or migrate it.
- The data does not regenerate. A new schema means a migration, with all the usual work of a cutover.
- Scope grows. A clean start invites every deferred feature, which is the second-system effect at high speed.
- The cost moves to verification. Proving that the new system does what the old one did takes the same tests and the same parallel run as any other replacement.
Full regeneration is defensible under the same conditions as any clean rewrite: the system is small, its behavior is written down, the old version can be frozen, and going back is possible. If those do not hold, regenerate one module at a time behind the tests, which is the strangler fig pattern at the scale of a codebase.
When not to use it
The audit-first path assumes the code works and someone depends on it. It is the wrong amount of care for a throwaway.
- A prototype nobody relies on. Delete it and write what you need. There is nothing to preserve.
- Code that never reached users. Without real behavior to protect, a test harness around it protects nothing.
- A system with a known security hole. Take it offline or close the hole first; keep-refactor-regenerate comes after.
The short version
Treat an AI-built v1 as what it is: a production system with no author. Read it, write down what it does, pin its behavior with tests, and then replace only the parts that have earned it. The Should we rewrite it? tool asks the same questions of this system as of any other.
Sources
- Michael Feathers - Characterization Testing (August 8, 2016)
- OWASP Top 10:2021 - A01 Broken Access Control
- OWASP Cheat Sheet Series - Input Validation Cheat Sheet
- Joel Spolsky - Things You Should Never Do, Part I (April 6, 2000)
Last updated November 29, 2026.

