Coding agents on legacy code: start with characterisation tests
Stefan-Iulian Tesoi · · 7 min read

Yes, once the behaviour they must preserve is pinned down by tests. An agent reading untested code cannot tell a bug from a behaviour someone depends on, and it tidies both. Have it write characterisation tests that record what the code does today, get a person to review those, and only then let it change anything.
That order matters more for coding agents on legacy code than for a person working on the same module. A developer who has maintained a system for years carries a map of its oddities. The agent starts every session without one, reads the code literally, and treats anything that looks wrong as something to fix.
Why is legacy code harder for an agent than for a person?
Because the knowledge that makes legacy code safe to change is mostly not in the code.
Michael Feathers gave the working definition in Working Effectively with Legacy Code, published in 2004: legacy code is code without tests. The definition is useful because it names what is missing. Without tests, the only record of what the system is supposed to do is the memory of the people who run it, the tickets that changed it, and the customers who would notice a difference.
A coding agent has access to none of those. It sees a function that returns null for an empty list where every other function returns [], and the inconsistency looks like a defect. It may be one. It may also be the signal a nightly export job uses to skip writing a file. Reading the function cannot settle which, and an agent is built to finish the task it was given, not to stop and ask about something next to it.
A person in the same position hesitates, mostly because they have been burned before. The agent has no such history, and it is fast, so oddities get normalised at the speed it reads.
What is a characterisation test?
A test that records what the code does now, not what it should do.
Feathers introduced the term in the same book. You call the code with an input, observe the output, and write an assertion that expects exactly that output, even when it looks wrong. The test does not claim the behaviour is correct. It claims the behaviour is current, so that any change to it shows up as a failing test instead of as a complaint three weeks later.
The same idea goes by golden master or approval testing, with tooling such as ApprovalTests, Jest snapshots and syrupy for pytest. For agent work the plain form is usually better: one explicit assertion per behaviour, named after what it records, is easier to review than a 400-line snapshot file.
Writing characterization tests is also one of the jobs an agent does well. It is mechanical and bounded, and the output checks itself: every new test must pass against the unchanged code, or it has recorded something that is not true.
Pinning behaviour before changing it
Split the work into two items, and do not let the second start until a person has read the first.
- Characterise. The agent writes tests for the code it will change and for that code's direct callers. No production code changes. Every new test passes on the current main branch.
- Review the record. A person reads each test and marks the behaviour it pins: intended, a bug the team keeps for now, or unknown. An unknown gets a question to whoever owns the system, not a guess.
- Change. The agent makes the change. For a pure refactor, every characterisation test still passes, unedited. For a behaviour change, exactly the tests that recorded the old behaviour change, and no others.
The third step is what keeps review short. The diff to the test files becomes the specification of the behaviour change. If an agent asked to rename a module also edited an assertion about rounding, the reviewer sees it in one line instead of finding it in production.
This turns the usual warning about AI-generated tests around: a test passing both before and after a change proves nothing about the change. For a refactor, passing before and after is exactly the claim. For a behaviour change the warning holds again.
Splitting is also where the rules for splitting stories for coding agents apply most strictly. A refactoring and a behaviour change in one item produce one diff, and nobody reading it can tell which lines were meant to change what the system does.
Which tasks suit coding agents on legacy code?
The ones whose result can be checked against something already recorded.
| Task | Fit for an agent | Why |
|---|---|---|
| Writing characterisation tests | Good | Every test must pass on unchanged code |
| Explaining a module and mapping its callers | Good | Read-only, and a person checks the explanation |
| Extracting or renaming under existing tests | Good | The tests define "nothing changed" |
| Upgrading a dependency with a published changelog | Good, with tests | The breaking changes are written down |
| Deleting code that looks unused | Poor | Reflection, config files and cron jobs call code a search never finds |
| Changing behaviour nobody can describe | Poor | There is nothing to check the result against |
| Rewriting a module wholesale | Poor | Every undocumented behaviour goes at once |
The pattern behind the table holds for modernising a legacy codebase with AI generally. The agent is strongest where the expected outcome is already written down, and weakest where it must infer intent from code that was never written to express any. AI refactoring legacy code works when the tests come first, because refactoring by definition preserves behaviour, and the tests are how anyone knows which behaviour there is.
The undocumented behaviour an agent tidies away
Consider a billing system with two date helpers, one in invoicing and one in reporting. They look like duplicates. An agent asked to reduce duplication merges them, keeps the cleaner implementation, and every test passes, because the module has 12% test coverage and none of it touches due dates.
The invoicing helper had one extra rule: a due date that lands on a weekend moves to the Monday. A customer's contract depends on it. The first anyone hears of the change is a late-payment reminder sent on a Saturday, for an invoice that by contract is not yet due.
By the standard it was given, the agent did nothing wrong. The rule it removed lived in a single if with no test and no comment, which, read from the code alone, looks the same as an accident.
A characterisation pass would have produced a test called something like due date on a Saturday moves to Monday, a reviewer would have marked it intended, and the merge would have failed on it. The difference in cost is an hour of agent time against a conversation with a customer.
Laimonade keeps a registry of regression invariants per area, the do-not-break rules for one part of a system, and a coding agent reads the ones covering the paths it is about to edit through get_regression_context, listed in the MCP tools reference. Behaviour a person confirms as intended during characterisation belongs there, so the next agent starts with the rule. The agent's work still stops at In Review, where someone reads the test diff against the item, as in the coding agent workflow a team can actually run.
Frequently asked questions
Should an agent refactor and change behaviour in the same item?
No. Keep them as separate items with a review between them. A refactor is verified by every characterisation test passing unedited; a behaviour change is verified by exactly the intended tests changing. Mixed into one diff, neither check works, and the reviewer has to work out from the code which differences were meant.
How much test coverage is enough before an agent starts?
Enough that every line the agent will change, and every caller of it, runs under at least one test that asserts on its output. A percentage for the whole codebase says little. A coverage report filtered to the files named in the item says almost everything, and it takes minutes to produce.
Can an agent explain a legacy module before changing it?
Yes, and it makes a good first item. Ask for the callers, the side effects, and every behaviour that looks unintentional, each with a file and line. Someone who knows the system can correct that in minutes. Treat the explanation as a draft of the characterisation tests, not as a replacement for them.