← Work with SpecDD skills guides

How to refactor while preserving specified behavior (specdd-refactor)

How-To Work with SpecDD skills Intermediate 1091009HOWTO-1091009

HOWTO-1091009Work with SpecDD skillsIntermediate

This guide shows you how to use specdd-refactor for structure changes that should preserve active spec-driven development behavior.

A refactor is not a behavior change. If behavior must change to complete the request, stop treating the work as a pure refactor and resolve authority for the behavior change first.

Short answer

Use specdd-refactor when code, tests, docs, specs, or project structure should change without changing specified behavior. The agent should identify public contracts, scenarios, invariants, dependencies, forbidden boundaries, and write authority; work in small mechanical steps; run baseline checks when practical; and update specs, docs, or tests only when the refactor changes structure they describe.

When to use this guide

Use this guide when:

Steps

1. Confirm this is a refactor

Use a focused prompt:

Refactor the Itinerary validation internals.

Then check whether the request changes behavior. If it adds validation, changes outputs, renames public symbols, changes errors, or moves responsibilities between specs, it is not a pure refactor.

2. Identify behavior to preserve

The agent should identify:

This becomes the refactor safety checklist.

3. Confirm write authority

Every file that moves or changes needs authority through the active spec chain.

Example:

Can modify:
  ./itinerary.js
  ./itinerary.test.js

If a refactor needs to move responsibility across spec boundaries, stop and review the specs first. specdd-refactor must not silently move ownership.

4. Plan small mechanical steps

Prefer changes that are easy to review:

Avoid broad rewrites unless the specs or user request require them.

5. Run baseline and final checks

When practical, run baseline checks before refactoring. Then rerun the relevant checks after each risky step or at least after the final change.

Verification should prove behavior was preserved:

6. Update specs only when structure changes

A pure internal cleanup may not need a spec edit. Update specs, docs, or tests when the refactor changes structure they describe.

Example: if itinerary.js becomes itinerary-validation.js and itinerary-items.js, update Owns, Can modify, or Structure in the relevant spec.

Do not rewrite behavioral requirements if behavior did not change.

Common mistakes

How to verify the result

The refactor worked when:

← Work with SpecDD skills guides