Characterization tests
Locks down current behavior before any refactor. The agent writes tests that capture exactly how the code works today — bugs and all — so you can change confidently without breaking hidden contracts.
// refactoring
A codebase with no tests. Inconsistent style. Functions that do five things each. You can spend a month refactoring by hand, or point an agent at it and review its work. The trick is knowing when the agent is right and when it's confidently wrong. Characterization tests pin current behavior first. Staged diffs make every change reviewable. The agent can still get it wrong, so the tests and your review are the safety net.
Prepaid in crypto · no card on file · nothing renews by itself
I. what it does
Locks down current behavior before any refactor. The agent writes tests that capture exactly how the code works today — bugs and all — so you can change confidently without breaking hidden contracts.
One logical change per diff. Extract a function. Rename a variable. Simplify an error handler. Each change is small enough to review quickly, and revertible if something breaks.
Finds functions, variables, and imports that are genuinely unused. But flags edge cases like reflection and metaprogramming where "dead" code is actually alive.
Refactors code to use updated APIs when libraries change. The agent understands deprecations and rewrites code to the new version systematically.
Moves code from one API surface to another — e.g., swapping out a library entirely. Staged so you can land the changes incrementally, not as one risky monolith.
Identifies functions with zero test coverage and flags them for manual review. You decide if they are critical (need tests) or safe to refactor without them.
II. the real risk
Legacy code is full of unspoken contracts. A function looks pointless until you realize it's used via reflection. An exception catch looks paranoid until you see a comment from 2015 saying "this happens in production every other Thursday." A variable looks unused until a runtime debug shows it's set by a third-party library.
An agent reads the code statically. It cannot see these hidden dependencies. It recommends changes confidently that break at 2 AM. That's why you need characterization tests first — they lock down the current behavior, silent contracts and all. Then, when the agent proposes changes, you can see quickly if it broke something.
III. the workflow
Step 1: Characterization tests. Ask the agent to run the current code on sample inputs and capture every output. It generates tests that pin the exact behavior. They should all pass against the current code before you change anything.
Step 2: One focused change at a time. Extract the validation logic into its own function. Rename variables for clarity. Simplify the error handling. One logical change per diff, kept small. Run the tests after each change. If they pass, you keep the diff. If they fail, you revert instantly.
Step 3: Know where to stop. Big architectural changes (splitting modules, swapping APIs entirely) are human decisions. The agent is good at mechanical refactors. Let it do those. You make the calls on strategy.
Step 4: Monitor in production. Log output metrics. If the refactored code behaves differently under real load (slower, more errors, different output), you know immediately. Revert if needed. The staged approach makes rollback fast.
IV. common failure modes
"Dead" code that isn't dead. A function with no internal callers. The agent deletes it. But it's part of the public API, and external code relies on it. The test suite doesn't catch this because there are no internal tests for the public surface.
Reflection and metaprogramming. The agent sees a variable set but never read, and doesn't realize it's set by reflection. Deleting it breaks at runtime.
"Unnecessary" error handling. An exception that hasn't been raised in 5 years of logs. The agent says it's dead code. But it handles the one edge case that happens once a quarter. Deleting it means something fails a few times a year with no safety net.
Silent semantic changes. Reordering operations that look independent. Moving a print statement, removing a temporary variable, inlining a function. Each change looks safe. Cumulatively, they change timing or side effects in ways tests don't catch.
Good characterization tests should catch most of these: the new code produces different output, and you revert. They will not catch everything, which is why review stays on you.
V. questions
VI. see also
// get started
Install the CLI, paste a token, describe the job. Plans start at $20 a month, prepaid in crypto, and nothing renews unless you buy again. There is no free trial.
curl -sSf https://dash.tartarusai.dev/tartarus-setup.sh | bash