Restructure code with behavioral equivalence enforcement β tests must pass before and after, and any test change requires explicit approval.
/cspec + /ctdd), fixing bugs (use /cdebug), or changes that intentionally alter behavior/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.
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.
| 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) |
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.
/crefactor warns you. Use a separate branch or finish the current feature first.