Refactoring & migration with AI

Never let an AI refactor untested code.

The idea

AI is incredible at translating code (e.g., Python 2 to 3, or Javascript to Typescript). But it will inevitably alter subtle business logic or swallow edge cases during the rewrite.

To safely migrate legacy code with AI, you must first wrap the old code in Characterization Tests (tests that just lock in the current behavior, bugs and all). Then, let the AI rewrite the code. If the tests stay green, the AI preserved the exact behavior. If they flip red, the AI hallucinated.

Legacy Module (JS)
T1
T2
T3
New Module (TS)
T1
T2
T3
Untested legacy code. An AI rewrite right now is extremely dangerous.

How it works (The Safety Net)

# Step 1: Characterization Tests
# Do not fix bugs. Just record what the legacy code actually does.
def test_legacy_pricing():
    assert legacy.calc(100) == 120 # Normal
    assert legacy.calc(-50) == 0   # Weird edge case, but record it!
    assert legacy.calc(None) == 0  # Locks in current behavior

# Step 2: AI Rewrite
# Prompt: "Translate this file to TypeScript. Preserve exact logic."

# Step 3: Run the Tests
# If test_legacy_pricing() passes on the new TS code, you know 
# with 100% certainty the AI didn't break the weird edge cases.