Metamorph
Refactoring assistant that checks LLM refactors by re-running the original tests.
- Status
- Implemented and documented; not yet run in a clean environment; no benchmarks
- AI for code
- Refactoring
- Testing
- React
Status: the implementation and its test suite are included, but they have not yet been run in a clean environment, and benchmarks are not yet published. This page describes the design and scope; the repository README is the source of truth for the exact behaviour.
Problem
Language models can rewrite legacy code quickly, and they can just as quickly change its behaviour. A refactor that reads well but breaks a case nobody remembered is worse than no refactor. The useful question is not "can a model refactor this?" but "how do we know the result still does the same thing?"
Metamorph started as my B.Tech final-year project on refactoring legacy code with AI.
Approach
The principle is simple: a refactor is accepted only if the original tests still pass.
legacy code + original tests
|
v
run baseline tests (must pass)
|
v
model proposes a refactor
|
v
run the ORIGINAL tests against the result
|
pass ---> accept, report metrics and diff
fail ---> retry with the failure output, up to a limit, then reject
What is implemented:
- A browser-based editor with a results panel showing the verdict, the number of attempts, metric changes and the diff.
- A Python engine (FastAPI) that runs the verify loop in an isolated subprocess with a timeout. A Docker sandbox is available as an opt-in.
- Pluggable model backends: a local model served by Ollama, any OpenAI-compatible endpoint, and a scripted fake for tests.
- Static metrics before and after (lines, cyclomatic complexity, maintainability) and a unified diff.
- Reports as JSON and Markdown.
- An evaluation harness for running a folder of legacy snippets with their tests. It ships with a few hand-written samples and no results.
Design considerations
- Local first. A local model keeps code private and makes the tool usable on code that cannot be sent to a hosted service.
- Tests as the oracle. Passing tests is necessary, not sufficient: the tests define the behaviour that is protected, and gaps in the tests are gaps in the guarantee. The report says so.
- Provider abstraction. The engine does not depend on one model vendor, which also makes the loop testable without a model.
- Small, reviewable changes. Smaller refactors are easier to verify and easier for a person to review.
Limitations
- The tests are the only safety net, so weak tests give weak guarantees.
- The default sandbox is a subprocess with resource limits, not a hardened container.
- Quality depends on the model that is used.
- No benchmark results are published, and the evaluation harness has not been run at scale.
Roadmap
- Run the evaluation harness on a larger set of legacy snippets and publish the pass and failure rates with the exact setup.
- Add property-based checks to widen what the tests protect.
- Support more languages than the current engine.