legacy-refactor-plan
Skill1 starsChanged 9 months agoMITView on GitHub
Use when planning actual code changes to a legacy project after guardrails are in place. Triggers when user says "refactor this module", "modernize this code", "plan the migration", "break this into tasks", or after legacy-safety-rails has established protection.
In this repository5 other files it gives its agents.
- ai-handrailSkill
- legacy-discoverySkill
- legacy-doc-bootstrapSkill
- legacy-kickstartSkill
- legacy-safety-railsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
20 rules. About 0 are common, 18 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈18
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 16
After guardrails exist (via legacy-safety-rails)
- Distinctive: line 17
When the user wants to start actual code modifications
- Distinctive: line 18
When planning a migration, extraction, or modernization
- Distinctive: line 24
Before planning any refactor, verify:
- Distinctive: line 33
If any prerequisite is missing, stop and complete it first using the appropriate skill.
- Distinctive: line 46
Prefer safe strategies in this order:
- Distinctive: line 54
Anti-Corruption Layer: When new code must interact with legacy code, insert an isolation layer that translates between models.
- Distinctive: line 56
Seam-based approach: Use seams identified during discovery as attachment points for wrappers and adapters.
- Very specific: line 89
Use the template from templates/refactor-task.md.
- Distinctive: line 104
Read the task specification
- Distinctive: line 105
Verify pre-conditions are met
- Distinctive: line 107
Run all verification checks
- Distinctive: line 108
If verification fails — rollback, don't proceed
- Distinctive: line 109
Update documentation to reflect changes
- Distinctive: line 126
Tests before changes: If a task changes code, the tests covering that code must exist and pass before the task starts.
- Distinctive: line 129
Wrap, don't rewrite: Prefer adapter/strangler patterns over rewrites.
- Distinctive: line 130
Prove equivalence: Before removing old code, demonstrate that new code produces identical behavior via tests or comparison.
- Distinctive: line 131
No drive-by improvements: Only change what the task specifies.
- Distinctive: line 132
Document as you go: Update docs after each task.
- Distinctive: line 133
Stop on uncertainty: If a task reveals unexpected complexity, stop and create a new discovery round — don't push through.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Legacy Refactor Plan
Break modernization into small, safe, verifiable tasks that AI can execute within controlled boundaries.
Goal
Produce a sequence of refactoring tasks where each task is independently verifiable, reversible, and auditable — never a big-bang rewrite.
When to Use
- After guardrails exist (via
legacy-safety-rails) - When the user wants to start actual code modifications
- When planning a migration, extraction, or modernization
Mandatory Workflow
Step 1: Confirm Prerequisites
Before planning any refactor, verify:
- Discovery completed for the target area?
- Documentation exists for affected modules?
- Test guardrails cover P0 paths through this area?
- Rollback procedure documented?
- Dependency versions identified and locked?
- High-impact outdated dependencies verified from the lockfile, project usage, and source inspection?
If any prerequisite is missing, stop and complete it first using the appropriate skill.
Step 2: Define Refactor Scope
With the user, agree on:
- What is being refactored (specific module, pattern, dependency)
- Why (the business or technical motivation)
- Boundary — what must NOT change (APIs, contracts, behaviors)
- Success criteria — how do we know it's done correctly?
Step 3: Choose Refactoring Strategy
Prefer safe strategies in this order:
- Wrap — add an adapter/wrapper around old code at identified seams (see discovery seam inventory), redirect callers gradually
- Strangle (Strangler Fig) — build new implementation alongside old, insert Anti-Corruption Layer between them, switch traffic incrementally
- Extract — pull a module into a cleaner boundary without changing its logic
- Replace — swap implementation behind a stable interface (only with tests + feature flag)
- Rewrite — last resort, small scope only, requires maximum guardrails
Anti-Corruption Layer: When new code must interact with legacy code, insert an isolation layer that translates between models. The ACL prevents legacy concepts from leaking into new code. When the legacy system is eventually replaced, only the ACL needs to change. See docs/methodology.md → Anti-Corruption Layer.
Seam-based approach: Use seams identified during discovery as attachment points for wrappers and adapters. If no seams were identified for the target area, run a focused discovery round first.
Step 4: Decompose into Tasks
Break the refactor into tasks. Each task MUST include:
## Task: [short description]
**Objective**: What this task accomplishes
**Files**: List of files to be modified
**Invariants**: What must NOT change (contracts, behavior, interfaces)
**Pre-conditions**:
- [ ] Tests to add/verify before this task
- [ ] Baseline captured for affected behavior
**Steps**:
1. [specific action]
2. [specific action]
...
**Verification**:
- [ ] Existing tests pass
- [ ] New tests pass
- [ ] Behavior matches baseline
- [ ] [domain-specific checks]
**Rollback**:
- [how to undo this task]
**Human Review Required**: [yes/no] — [reason if yes]Use the template from templates/refactor-task.md.
Step 5: Order Tasks
Sequence tasks so that:
- Each task is independently deployable
- Earlier tasks don't depend on later tasks
- Highest-risk tasks have the most guardrails
- Tasks that add tests come before tasks that change code
Step 6: Execute One Task at a Time
For each task:
- Read the task specification
- Verify pre-conditions are met
- Execute the steps
- Run all verification checks
- If verification fails — rollback, don't proceed
- Update documentation to reflect changes
- Move to next task only after human sign-off (if required)
Required Outputs
| Output | Description |
| Prerequisites checklist | Verified list of discovery, docs, and guardrails in place |
| Scope agreement | Documented boundary, invariants, and success criteria |
| Strategy rationale | Why this refactoring approach was chosen |
| Task list | Ordered sequence of tasks per the template |
| Dependency graph | Which tasks depend on which |
| Human review map | Which tasks need human sign-off and why |
Hard Rules
- One task, one boundary. Each task modifies one well-defined area. No "refactor modules A, B, and C" in one task.
- Tests before changes. If a task changes code, the tests covering that code must exist and pass before the task starts.
- Existing versions only. Refactored code must work with the project's actual dependency versions. Upgrades are separate tasks.
- Use source inspection for risky old dependencies. You must confirm the pinned API via source inspection before changing call sites.
- Wrap, don't rewrite. Prefer adapter/strangler patterns over rewrites. Rewrites are last resort.
- Prove equivalence. Before removing old code, demonstrate that new code produces identical behavior via tests or comparison.
- No drive-by improvements. Only change what the task specifies. Don't "improve" adjacent code.
- Document as you go. Update docs after each task. Future AI sessions depend on accurate docs.
- Stop on uncertainty. If a task reveals unexpected complexity, stop and create a new discovery round — don't push through.
Anti-Patterns
| Don't | Do instead |
| Plan a big-bang migration | Break into 5-20 small independent tasks |
| Assume latest API compatibility | Verify every API against lockfile versions |
| Remove old code before new code is proven | Run both in parallel, compare, then cut over |
| Combine test-writing with refactoring in one task | Separate "add tests" and "refactor" into distinct tasks |
| Skip human review for P0-CRITICAL changes | Always flag P0 changes for human sign-off |
| Continue when a verification check fails | Stop, rollback, investigate, re-plan |
Human Review Triggers
These ALWAYS require human sign-off before proceeding:
- Any change to
P0-CRITICALorLEGACY-SEALEDmodules - Any API contract change (paths, parameters, response shapes)
- Any database schema migration
- Any change to analytics/tracking events
- Any deletion of code or files
- Any change to authentication or authorization logic
- Any change to payment or billing flows
- Any behavior change visible to end users
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

