---
target: {{target_path}}
branch: null
work_type:
ref:
refactored_headings: []
dependencies: []
---
# Scratch Pad — Refactor — Replace With Refactor Focus
## Flow Overview
- [ ] Phase 0: Clarify motivation + boundaries
- [ ] Phase 1: Inventory + risks
- [ ] Phase 2: Proposed refactor plan (user gate)
- [ ] Phase 3: Mechanical refactor (small steps)
- [ ] Phase 4: Validate + clean up
## Phase 0 — Motivation & Boundaries
### Action: State the refactor motivation and constraints
#### Example
**Motivation**: Template resolution code is hard to reason about due to duplicated slug handling.
**Constraints**:
- No change in external behavior.
- Keep error messages stable.
**Non-goals**:
- Do not change template search order.
## Phase 1 — Inventory & Risk
### Action: Inventory impacted code paths and callers
#### Example
- Touch: `TemplateCatalog::override_candidates`, `sanitize_key`.
- Callers: `Specman::create` and `LifecycleController` creation plans.
- Validation: unit tests for slug normalization + an integration smoke test.
### Action: Identify risky behavior changes
#### Example
- Risk: changing path precedence breaks workspace overrides.
- Guardrail: assert resolution uses `.specman/templates/scratch/ref.md` first.
## Phase 2 — Plan (User Gate)
### Action: Draft a step-by-step refactor plan
#### Example
1. Add tests for current behavior.
2. Extract a helper for slug computation.
3. Replace duplicated logic and keep outputs identical.
### Action: Ask for user confirmation
#### Example
**Question**: OK to proceed with the 3-step refactor plan above?
## Phase 3 — Refactor Execution
### Action: Apply mechanical changes in small steps
#### Example
- Commit A: rename local variables for clarity (no logic change).
- Commit B: replace duplicated slug logic with `sanitize_key` call.
## Phase 4 — Validate & Clean Up
### Action: Re-run tests and verify invariants
#### Example
- Ran: `cargo test -p specman`
- Verified: override order unchanged; output identical for fixture workspaces.
## Notes
## Decisions
## Next Steps