AI coding agents and dead code: green tests, unreachable code
On the AI layer for a fintech platform I wrote about 125 specifications in two weeks and had AI coding agents implement them. I acted as the curator: I wrote the specs, the agents wrote the code and the tests, and I accepted or rejected the result. The agents were fast, and their reports were confident: implemented, tested, everything wired in.
On 17 October I stopped taking the last part on trust and checked the whole branch. It had 4,611 new names – functions, classes, methods, constants, fields. Twenty-four had no consumer outside the tests. Eight of those were dead.
The shape of the dead code
None of the eight was a typo or a forgotten helper. Every one had the same shape: the agent wrote a method, covered it with a test, and the product path went around it.
- A second way to create a system run, next to the one the product used. Only the tests called it.
- A method that read the messages of a request, duplicating the API that already did.
- A method that accepted a user’s answer to a clarifying question – which the product path never called, because the answer arrived another way.
- A ‘can this transition happen?’ function, while the transitions themselves went through a table.
- A field in the rules schema that no rule file contained.
- Two methods for publishing a new revision of a document.
- An operator check that nothing invoked.
Five were deleted. The check was wired in as an operator command. The two publishing methods went back into the specification for revisions, which kept one and deleted the other.
The uncomfortable lesson is that coverage made this worse. Each dead method had a passing test, so it counted towards the 96–97% line and looked like the healthiest code in the module. Coverage tells you code ran under test; it says nothing about whether anything else runs it.
Two scripts, because each lies differently
I ended up with two small scripts, and I need both, because each one fails in a different direction.
Reachability from entry points. About 90 lines with Python’s ast module. It builds the graph of static imports starting from every way code is actually run (the sketch below is simplified): the API application, the worker process, the operator CLIs – the knowledge-base loader, the rules loader, the eval runners and the rest – plus migrations/env.py and every Alembic revision. Tests and conftest are excluded. A module the graph can’t reach is a defect.
def reachable(entry_modules: list[str], root: Path) -> set[str]:
seen, stack = set(), list(entry_modules)
while stack:
mod = stack.pop()
if mod in seen or not (path := module_path(mod, root)):
continue
seen.add(mod)
tree = ast.parse(path.read_text())
for node in ast.walk(tree):
if isinstance(node, ast.ImportFrom) and node.module:
stack.append(resolve(node.module, node.level, mod))
elif isinstance(node, ast.Import):
stack.extend(alias.name for alias in node.names)
return seen
Its weakness is anything loaded by a string or a path. Alembic loads revisions by path, and the CLIs are run by name, so the first versions flagged all of them. The fix was to declare them as entry points explicitly, not to loosen the check. A short list of known exceptions – the fake models for tests, the test database helpers – matches a table of ‘code waiting to be wired’ in the project rules, so an exception is a documented decision rather than a silenced warning.
Every new name needs a consumer. About 180 lines. It takes the .py files changed between two commits, compares definitions at the base and the tip, and for each new name looks for a use by word in non-test code, not counting its own definition. Framework hooks don’t need a consumer – Alembic’s upgrade and downgrade, dunders, model_config, names under Pydantic validators and Temporal’s workflow, activity and signal decorators. An enum member counts as used if its value appears in code, data or the eval sets.
This one is a lower bound. A name that happens to match another word gets a false consumer, and anything called through getattr or a registry is checked by hand. Reachability is coarse – it works on modules – but it doesn’t miss. The name check is fine-grained but optimistic. Together they cover each other.
The run that collected nothing
The worst report I received was green and had tested nothing. The agent kept the list of test paths in a shell variable. In zsh an unquoted $VAR isn’t split on spaces the way it is in bash, so pytest received one long string as a single path and collected zero tests. The short output read as a clean run, and so did the report.
The fix is one character, ${=VAR}, and a rule: a run that collected zero tests is not a run. I read the summary line and the count of skipped tests on every report before anything else.
Acceptance on a clean tree
The agents worked in one shared working copy, and that turned out to matter. A test run there picked up another agent’s half-written files, so a green result could depend on work that was not in the commit. Acceptance now starts from git archive of the tip into an empty directory, and runs there:
- Check the list of commits and their author against the report.
- Lint.
- Run both scripts over the commit range and compare with what the agent claimed.
- Check file sizes against the project’s limits.
- Run the full suite with xdist and a real Temporal server on its own Postgres, with coverage and a single migration head.
- Check every point of the specification against its source.
- Mutate the key lines myself: the test that covers a line must go red when the line is wrong.
- If schemas or prompts changed, run the live check against the real models.
Step 7 is the one agents can’t fake. A test written to pass passes; a test that fails when you break the code it claims to cover is a test.
What this costs, and what I would change
All of this is a manual step. The scripts aren’t in CI, so acceptance depends on the curator remembering to run them, and on a long day that is exactly the step that gets skipped. Next time:
- An entry-point manifest from day one, including everything loaded by a string or a path, so the reachability check is right from the first commit instead of after its first false alarms.
- Both scripts as a CI step, failing the build, with the exception table as their only escape hatch.
- A working tree per agent instead of a shared copy, so a run can only see its own work.
The general rule is older than AI agents: a report is a claim, and acceptance is a check you run yourself. It just matters more when the work arrives as 691 commits in two weeks, written by authors that never sound unsure. The same instinct – verify what you don’t control – runs through testing LLM integrations, and the tooling idea is the core of team-bootstrap.