From lattice
Safely restructures existing code without changing observable behavior using characterization-first workflow with context, design, architecture, quality, and testing guardrails. For 'refactor this' or similar.
npx claudepluginhub techygarg/lattice --plugin latticeThis skill uses the workspace's default tool permissions.
Load these skills based on refactor scope (see Steps 3, 5, 6 for conditional use):
Safely refactors code test-first: verifies/writes tests, one structural change at a time, preserves behavior. Use for 'refactor this', 'clean this up', reorganization.
Guides refactoring code structure and modules to improve maintainability, readability, and performance while preserving external behavior and ensuring all tests pass.
Enforces rigid pre-refactor checklist: read existing code, verify covering tests pass, state goal clearly, validate non-ego motivation, plan smallest first step. Use before restructures, renames, or cleanups.
Share bugs, ideas, or general feedback.
Load these skills based on refactor scope (see Steps 3, 5, 6 for conditional use):
framework:knowledge-priming -- Load project context (tech stack, architecture, conventions) so refactor fits real project not generic patterns (always)framework:context-anchoring -- Load existing feature context doc when available, capture approved refactor plan, preservation rules, structural decisions for future sessions (always)framework:collaborative-judgment -- Surface meaningful trade-offs in structure, seams, migration sequence instead of silently choosing path (always)framework:clean-code -- Improve readability, responsibility boundaries, local code craft while preventing scope creep and wrong abstractions (always)framework:test-quality -- Lock current behavior with characterization tests, keep safety net reliable throughout refactor (always)framework:design-first -- Use progressive design selectively for significant structural changes so target structure agreed before editing code (conditional)framework:architecture -- Validate layer placement, dependency direction, correct structural boundaries (conditional)framework:domain-driven-design -- Validate domain behavior, aggregate boundaries, movement of business rules into correct domain objects (conditional)framework:secure-coding -- Preserve validation, authorization, trust-boundary protections, safe data handling when refactor touches security-sensitive code (conditional)Start from current pain, not preferred abstraction.
.lattice/learnings/review-insights.md exists, read it. Recurring review findings often identify exactly which structural mistakes should be correctedframework:context-anchoring Document Discovery to check for existing context doc for affected feature/module
End step, summarize intent one sentence:
"Refactor X to improve Y while preserving Z."
If can't state improvement target and preservation target that clearly yet, continue clarifying before planning changes.
Optional persistence check:
framework:context-anchoring, then use as source of truth for approved planRefactoring changes structure, not behavior. Make preservation contract explicit before proposing structural edits.
List behaviors that must remain unchanged:
Also list explicit out-of-scope changes:
This step defines refactor's safety boundary. If desired outcome requires changing preserved behavior, stop and discuss whether task actually bug fix, feature, or broader redesign.
Zero Refactor Rule: no structural code changes until user approves target structure and transition plan.
For small refactors, plan may be brief. For larger ones, use framework:design-first selectively:
Present:
End step with explicit gate:
"Does this refactor plan look correct? Should I proceed to Step 4: characterization tests?"
Don't write refactor code until user explicitly approves.
If persistence enabled, use framework:context-anchoring Enrich behavior to capture approved preservation boundaries, target structure, movement plan, out-of-scope items. Don't proceed to Step 4 until plan written.
Before changing structure, lock current behavior with tests.
framework:test-quality inlineStopping rule:
This step workflow's differentiator: refactor not considered safe until current behavior executable and guarded.
End step with explicit gate:
"Characterization tests in place and passing. Ready to discuss refactor strategy and pacing?"
Don't proceed to strategy selection until safety net verified green.
After user approves high-level plan and safety net in place, choose implementation approach.
Preferred strategies:
framework:architecturePreferred pacing:
"How would you like review refactor?"
- Slice-by-slice (recommended) -- Refactor one safe slice at time, pause after each slice. Best for risky legacy code.
- Layer-by-layer -- Complete refactor for one structural layer or concern, then pause for review. Best for broader architectural cleanup.
- Full autonomy -- Execute approved refactor end-to-end, present complete result at end. Best for tightly scoped, low-risk refactors. (Still pause if slice reveals approved plan unsafe or invalid — see Step 6 Deviation Rule.)
Default to slice-by-slice if user doesn't express preference.
Implement only within approved preservation boundaries and target structure.
For each slice:
framework:collaborative-judgment, surface them before presenting slice's code. Don't interrupt mid-slice unless approved plan becomes unsafe or invalid.Always apply:
framework:clean-code -- better boundaries, simpler control flow, smaller focused units, clearer namingframework:test-quality -- maintain strong characterization tests and nearby supporting testsConditionally apply:
framework:architectureframework:domain-driven-designframework:secure-codingDeviation rule:
Refactor succeeds only if both true:
Verify preservation:
Verify structural improvement:
When reporting completion, be explicit about both:
Use framework:context-anchoring Enrich behavior to preserve important parts of refactor:
If no context doc exists and refactor involved non-trivial structural reasoning, suggest creating one so decisions not lost across sessions.
After refactor complete, recommend /review when change:
/review provides independent pass on refactor, can capture broader structural learnings for future work.