# I refactored 1,400 lines with an AI agent: here's how I checked my tests weren't lying to me

> Source: <https://dev.to/ckomiya/i-refactored-1400-lines-with-an-ai-agent-heres-how-i-checked-my-tests-werent-lying-to-me-4ae4>
> Published: 2026-12-02 21:14:37+00:00

**Recommendation:** Run mutation testing on your test suite after AI-assisted refactors: break the code deliberately in copies and confirm tests fail, then close any gaps before trusting green results.
More in the playbook "Evals and testing for LLM apps": <https://wpnews.pro/playbooks/evals-testing>

[vegapunk-agents](https://github.com/ckomiya/vegapunk-agents) is an open-source project where several AI agents work together on a task. They don't talk to each other. Instead, they coordinate through a shared whiteboard: each one writes down what it found and reads what the others wrote.

That whiteboard is run by a 1,400-line program, all in a single file. I'm now building a second version of the project with LangGraph, a popular library for building agent systems, and it needs the same rules. Rather than copy the file, I decided to pull those rules out into a piece both versions can share. That's refactoring: reorganizing code without changing what it does.

An AI, Claude Code, did the work. My job was to make sure it didn't break anything.

Before starting, I wrote down how I'd check every step:

I did it in small steps over six days, and the behavior didn't change in any of them. The original file ended up at about 340 lines; the rest moved into the shared piece.

Along the way, the tests corrected me too: I'd assumed I knew how one part of the program worked, and I was wrong. It stayed the way it was.

Every test passed. That looked like good news, but passing tests can mean two things: the code is fine, or the tests aren't looking at the right things and would pass even if the code were broken. From the outside, both look the same: all green.

It's like a house alarm. Silence doesn't mean nobody got in; maybe it's unplugged. The only way to know is to open a window on purpose and see if it goes off.

That's what **mutation testing** is for: you break the code on purpose, in a copy, and check whether the tests catch it. Each broken copy is called a **mutant**.

Here's a simplified example. This function saves a note and reports that everything went fine:

``` python
def save_note(text):
    # ... saves the note to the whiteboard ...
    return {"ok": True}
```

And this is a mutant: it does the same thing, but reports a failure.

``` python
def save_note(text):
    # ... saves the note to the whiteboard ...
    return {"ok": False}   # deliberate bug
```

If any test fails against the mutant, the alarm went off: the bug would have been caught. If every test passes, there's a gap, because a bug like that could slip in someday without anyone noticing.

I used it in two ways. At every step of the refactoring, to confirm the new tests actually caught changes; once, a test didn't, and I had to strengthen it before moving on. And at the end, to check the whole safety net at once, with almost a hundred mutants.

Almost every time, the alarm went off. Three times it didn't. Those three were real gaps in my tests: bugs that could have gone through without any warning. I wrote the missing tests and ran the mutants again; this time every one was caught.

Without this step, I would have finished the refactoring convinced everything was covered. The tests told me nothing had broken; mutation testing told me whether I could believe them.

|  | Before | After | 
|---|---|---|
| Lines in the main file | 1,414 | 338 | 
| Lines in total | about 1,400 | about 1,700 | 
| Files | 1 | 8 (the main one + 7 in the shared piece) | 
| Tests | 249 | 362 | 
| Lines no test ever ran | 54 | 6 | 
| Gaps found with mutants | — | 3, closed | 

The total amount of code grew a little: splitting into several files takes some extra code to connect them. The goal was never less code, but code I could reuse. And throughout the whole process, the four recorded tasks came out identical after every step.

With the shared piece ready and well tested, the next step is building the LangGraph version on top of it. That's where the real payoff will show: instead of rewriting the whiteboard rules, that version will use them as they are. I'll cover it in the next post.

If you're curious about the technical details, they're in the [full report](https://github.com/ckomiya/vegapunk-agents/blob/main/docs/refactoring-a3.md) in the repo.
