Skip to content
Templates/Refactor
RefactorMedium risk

Refactor Loop

Refactor a module in small, test-verified steps without changing its external behavior.

What this Loop Engineering template does

Refactor the target module in small steps, keeping external behavior identical and the test suite green after each step.

Behavior must not change. Run the tests after each step. Prefer many small, reversible changes over one large rewrite — for example, splitting a large file into focused modules until each is under a size budget.

When to use it

Splitting a large file into focused modules
Improving structure without behavior change
Incremental, test-backed refactors

When not to use it

Refactors without a test safety net
Mixing a refactor with new features

Validation checks

validation
Full test suite passes after each step
Public API and external behavior are unchanged
No file exceeds the agreed size budget
Type checks pass

Boundaries & stop rule

!Do not change external behavior or public APIs
!Do not mix in new features
!Do not delete tests
!Do not refactor unrelated modules
Stop rule — Stop when the refactor goal is met and tests are green, or after 5 failed steps. With Claude /goal, bound it by adding “or stop after N turns” to the condition. If a step breaks behavior, revert it, summarize what broke, and recommend a smaller step or a human decision.

Copy the loop prompt

claude-goal.txt
/goal Refactor the target module in small steps, keeping external behavior identical and the test suite green after each step.
 
Task type: Custom
Target tool: Claude Code
 
Work toward this goal until all validation checks pass or the stop rule is reached.
 
Design hypothesis:
This refactor loop can produce a safer result if the scope stays narrow, validation is explicit, and a checker can reject shortcut work.
 
Smallest useful run:
Run one bounded pass on the newest relevant signal before expanding scope or adding a schedule.
 
Loop cycle:
1. Discovery — Read the latest signal for this template before acting: CI output, issue detail, review comment, dataset report, or content brief.
2. Handoff — Hand the work to one agent in an isolated branch, worktree, or clearly scoped session. Keep final approval with a human.
3. Verification — Use an independent review pass to confirm the result, inspect the diff or artifact, and reject shortcut work.
4. Persistence — Save a short run note with the signal reviewed, actions taken, validation result, and next recommended step.
5. Scheduling — Run manually until the loop is reliable; only then consider a scheduled or event-triggered run.
 
Context:
Behavior must not change. Run the tests after each step. Prefer many small, reversible changes over one large rewrite — for example, splitting a large file into focused modules until each is under a size budget.
 
Validation:
Full test suite passes after each step
Public API and external behavior are unchanged
No file exceeds the agreed size budget
Type checks pass
 
Validation evidence:
Record the original signal, checks run, final result, changed files or artifacts, and any checker rejection.
 
Independent checker:
Use an independent review pass to confirm the result, inspect the diff or artifact, and reject shortcut work.
 
Boundaries:
Do not change external behavior or public APIs
Do not mix in new features
Do not delete tests
Do not refactor unrelated modules
 
Stop rule:
Stop when the refactor goal is met and tests are green, or after 5 failed steps. With Claude /goal, bound it by adding “or stop after N turns” to the condition.
Maximum iterations: 6
 
Budget:
Example only: stop before exceeding the agreed per-run token budget.
 
Human approval:
Required before merge, deploy, delete, purchase, or external communication.
 
Fallback:
If a step breaks behavior, revert it, summarize what broke, and recommend a smaller step or a human decision.
 
Loop Validation Log:
- Hypothesis: This refactor loop can produce a safer result if the scope stays narrow, validation is explicit, and a checker can reject shortcut work.
- Smallest useful run: Run one bounded pass on the newest relevant signal before expanding scope or adding a schedule.
- Expected evidence: Record the original signal, checks run, final result, changed files or artifacts, and any checker rejection.
- Actual evidence: [fill in after the run]
- Passed? [yes / no / partial]
- Feedback: After the run, note what the loop learned, what failed, and what should change before the next pass.
- Next step: [stop / adjust the loop / run the next pass]
 
Do not delete tests, bypass checks, or modify unrelated files just to satisfy the validation condition. If blocked, stop and summarize the blocker, attempted fixes, and recommended next action.

Failure modes to watch

Changing behavior while “just refactoring”
One giant rewrite that is hard to review
Dropping tests to make a step pass
Scope creep into unrelated code
Worked scenario review · maintained by TianMingAI · 2026-07-21

Review receipt

This is a bounded review exercise for the template, not a claim about a production deployment.

Scenario

Three generators duplicate formatting logic, and the proposed cleanup must preserve byte-for-byte copyable output.

Baseline evidence

The loop saves representative outputs, public function behavior, tests, performance baseline, and the exact duplication targeted before moving any code.

Validation result

Characterization tests pass before and after, output snapshots remain identical, the duplicated branch is removed, and no unrelated naming or product behavior changes enter the diff.

Shortcut rejected

Changing output wording because it looks cleaner is rejected as a product change outside the refactor's declared invariant.

Human gate

A maintainer reviews whether the new shared boundary is easier to maintain and approves any deliberate output difference as a separate change.

Final decisionPass

Loop Engineering FAQ

A refactor must not change behavior — the tests that pass before must still pass after. A bug fix deliberately changes behavior and proves it with a new regression test.