Qualityprompt onlyMedium

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.

← all loops
refactoringcode-qualitytestingiterative-improvement

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.

  1. 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.).

  2. 02

    Initiate the Loop

    Run the kickoff prompt to start the refactoring process. The agent will take control from there.

  3. 03

    Monitor Progress

    Watch as the agent makes changes, runs tests, and iterates. Intervene only if necessary based on feedback.

Workflow Steps

  1. 01

    Analyze current codebase state and identify refactoring targets

  2. 02

    Make targeted improvements to code structure (variables, functions, classes)

  3. 03

    Run npm test to verify changes don't break existing functionality

  4. 04

    If tests fail, inspect results and adjust the refactoring approach

  5. 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

rendering…

Related loops — Quality