Refactor Safely
This loop systematically refactors your codebase to improve structure while maintaining test coverage. The agent identifies opportunities for cleaner code without breaking existing functionality.
Goal
Improve code structure
How to Run
Use this loop in any supported IDE or CLI tool to iteratively refactor code with safety nets. Simply provide your codebase and let the agent handle the refactoring process.
- 01
Setup Environment
Ensure you have Node.js and npm installed. Clone or open your project in one of the supported tools (Cursor, Claude Code, Codex, etc.).
- 02
Initiate the Loop
Run the kickoff prompt to start the refactoring process. The agent will take control from there.
- 03
Monitor Progress
Watch as the agent makes changes, runs tests, and iterates. Intervene only if necessary based on feedback.
Workflow Steps
- 01
Analyze current codebase state and identify refactoring targets
- 02
Make targeted improvements to code structure (variables, functions, classes)
- 03
Run npm test to verify changes don't break existing functionality
- 04
If tests fail, inspect results and adjust the refactoring approach
- 05
Repeat until tests pass or maximum iterations reached
Kickoff Prompt
Start the "Refactor Safely" loop. Goal: Improve code structure Max iterations: 10 Between iterations run: npm test Exit when: Tests pass Please refactor the provided codebase to improve code structure while ensuring all tests continue to pass. Focus on making the code more readable and maintainable through proper naming, breaking down large functions, removing duplication, and organizing modules logically. Begin now. Self-pace this loop. After each iteration, run `npm test` and evaluate the output, and only continue if the exit condition is not met (Tests pass). Stop when the exit condition passes or 10 iterations are reached. Give a short status update each pass.
Guardrails
hardcoded- ·Do not modify existing test files or add new test files unless explicitly requested.
- ·Preserve all existing functionality and behavior during refactoring.
- ·Avoid introducing new dependencies or changing package versions.
- ·Ensure any new code follows standard language idioms and best practices.
- ·Stop immediately if more than 3 consecutive iterations fail the test check.
Flow Diagram
Related loops — Quality
Quality
Dead Code Eliminator
Automatically identifies and removes unused code to improve codebase quality and maintainability. This loop performs iterative scans for dead code, removes it gradually, and ensures no regressions are introduced.
Quality
Lint Until Clean
Automatically iterates through lint error detection and resolution until the codebase passes linting with zero issues. This loop ensures code quality and consistency by having the agent fix problems as they are identified.
Quality
Code Quality Until A Grade
A prompt-only loop designed to iteratively improve code quality and maintainability until a specified quality gate passes. The agent analyzes the codebase, identifies quality issues, and applies targeted fixes to achieve an 'A' grade quality score.