Correctless

/crefactor β€” Structured Refactoring

Restructure code with behavioral equivalence enforcement β€” tests must pass before and after, and any test change requires explicit approval.

When to Use

How It Fits in the Workflow

/crefactor is a standalone workflow β€” it does not use the TDD state machine. It can run alongside an active feature workflow (on a separate branch) or independently. The test suite IS the spec: if tests pass before and after, the refactor is correct.

What It Does

  1. Captures refactor intent β€” asks what you are refactoring, why, and what should NOT change. Writes an intent document for the audit trail.
  2. Assesses test coverage β€” runs coverage on the files being refactored. If coverage is adequate, existing tests become the behavioral contract. If coverage is low or zero, proceeds to characterization tests.
  3. Writes characterization tests (when needed) β€” spawns a separate agent that captures the current behavior of the code, including quirks and bugs. These tests assert reality, not intent.
  4. Snapshots the behavioral contract β€” records total test count, all passing test names, and test file checksums. This baseline is the standard everything is measured against.
  5. Plans and executes in phases β€” breaks the refactor into phases where tests pass after each one. Each phase uses two separate agents: a refactor agent (restructures code, cannot touch tests) and a verification agent (confirms behavioral equivalence, read-only).
  6. Gates all test changes β€” if the verification agent detects a test file was modified, the refactor halts. You must approve the change with a stated reason. This is the critical invariant: silent test changes erase the behavioral contract.
  7. QA review β€” an independent agent checks for behavioral drift the test suite might not cover (API response shapes, error messages, log formats).

Example

User: /crefactor β€” extract domain layer from HTTP handlers

Agent: What are you refactoring?

User: The business logic in src/handlers/ is mixed with HTTP concerns.
      I want to extract it into src/domain/.

Agent: Coverage is 62% on handler files. Adequate β€” using existing
       tests as the behavioral contract.

       Baseline captured β€” 47 tests all passing.

       Refactor Plan:
       Phase 1: Create domain interfaces in src/domain/
       Phase 2: Move validation logic from handlers to domain
       Phase 3: Move database calls from handlers to domain services
       Phase 4: Reduce handlers to HTTP parsing + domain call + response

       Each phase: restructure β†’ run tests β†’ verify equivalence.

User: Go ahead.

Agent: Phase 1 complete β€” 47/47 tests passing.
       Phase 2 complete β€” 47/47 tests passing.
       Phase 3 HALT β€” test `TestUserHandler_Create` was modified.

       The test was importing `handlers.CreateUser()` directly.
       After the move, the function is at `domain.CreateUser()`.
       This test was testing implementation details (import path),
       not behavior. Is this change intentional?

User: Yes, update the import path.

Agent: Approved. Resuming...
       Phase 3 complete β€” 47/47 tests passing.
       Phase 4 complete β€” 47/47 tests passing.

       Refactor complete β€” 47 tests passing, behavioral equivalence
       verified.

What It Reads / Writes

Reads Writes
Source files being refactored .correctless/artifacts/refactor-intent-{slug}.md
Test files (behavioral contract) .correctless/artifacts/refactor-baseline-{slug}.json
ARCHITECTURE.md .correctless/artifacts/refactor-summary-{slug}.md
.correctless/antipatterns.md .correctless/artifacts/qa-findings-refactor-{slug}.json
.correctless/config/workflow-config.json Characterization test files (if needed)
Β  ARCHITECTURE.md (if patterns changed, with approval)

Intensity Levels

Same at all intensity levels for the behavioral equivalence pipeline with agent separation and test-change gating. At high/critical intensity, mutation testing on refactored code, cross-spec impact analysis (checks whether structural changes affect other features’ invariants), and drift debt updates are added.

Common Issues