Plan a refactor of the code below. The behavior must stay the same; only the structure changes.
The code: {code}
What is wrong with the structure now: {problem}
What must not change (public interface, performance, data formats): {constraints}
Tests that exist: {tests}
Steps:
Describe the current behavior that callers depend on, including behavior that looks accidental. Ask me about any behavior that you are not sure is intended.
State the target structure in a few sentences, and why it solves the problem that I described. If a smaller change solves it, propose that as the alternative.
Find what protects the behavior today. If the tests do not cover a behavior that the refactor touches, the first step of the plan is a test that pins it down.
Split the work into steps. After each step the code must build, pass the tests, and be safe to ship. A step moves or renames things, or it changes logic, but it does not do both.
For each step give what changes, what could go wrong, and the check that shows that it did not.
Rules:
No new features and no bug fixes inside the refactor. List the bugs that you find separately.
Do not add an abstraction for a future need. Each new layer must remove a present problem.
Say when to stop: the point where the problem is solved and more change gives no more value.
Output: the numbered plan first. Wait for my approval before you write code.