agentleFS
ForeverScForeverSc/ai-handrail/skills/legacy-refactor-plan/SKILL.md

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.

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
  1. Very specific:

    After guardrails exist (via legacy-safety-rails)

    line 16
  2. Distinctive:

    When the user wants to start actual code modifications

    line 17
  3. Distinctive:

    When planning a migration, extraction, or modernization

    line 18
  4. Distinctive:

    Before planning any refactor, verify:

    line 24
  5. Distinctive:

    If any prerequisite is missing, stop and complete it first using the appropriate skill.

    line 33
  6. Distinctive:

    Prefer safe strategies in this order:

    line 46
  7. Distinctive:

    Anti-Corruption Layer: When new code must interact with legacy code, insert an isolation layer that translates between models.

    line 54
  8. Distinctive:

    Seam-based approach: Use seams identified during discovery as attachment points for wrappers and adapters.

    line 56
  9. Very specific:

    Use the template from templates/refactor-task.md.

    line 89
  10. Distinctive:

    Read the task specification

    line 104
  11. Distinctive:

    Verify pre-conditions are met

    line 105
  12. Distinctive:

    Run all verification checks

    line 107
  13. Distinctive:

    If verification fails — rollback, don't proceed

    line 108
  14. Distinctive:

    Update documentation to reflect changes

    line 109
  15. Distinctive:

    Tests before changes: If a task changes code, the tests covering that code must exist and pass before the task starts.

    line 126
  16. Distinctive:

    Wrap, don't rewrite: Prefer adapter/strangler patterns over rewrites.

    line 129
  17. Distinctive:

    Prove equivalence: Before removing old code, demonstrate that new code produces identical behavior via tests or comparison.

    line 130
  18. Distinctive:

    No drive-by improvements: Only change what the task specifies.

    line 131
  19. Distinctive:

    Document as you go: Update docs after each task.

    line 132
  20. Distinctive:

    Stop on uncertainty: If a task reveals unexpected complexity, stop and create a new discovery round — don't push through.

    line 133

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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:

  1. What is being refactored (specific module, pattern, dependency)
  2. Why (the business or technical motivation)
  3. Boundary — what must NOT change (APIs, contracts, behaviors)
  4. Success criteria — how do we know it's done correctly?

Step 3: Choose Refactoring Strategy

Prefer safe strategies in this order:

  1. Wrap — add an adapter/wrapper around old code at identified seams (see discovery seam inventory), redirect callers gradually
  2. Strangle (Strangler Fig) — build new implementation alongside old, insert Anti-Corruption Layer between them, switch traffic incrementally
  3. Extract — pull a module into a cleaner boundary without changing its logic
  4. Replace — swap implementation behind a stable interface (only with tests + feature flag)
  5. 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:

  1. Each task is independently deployable
  2. Earlier tasks don't depend on later tasks
  3. Highest-risk tasks have the most guardrails
  4. Tasks that add tests come before tasks that change code

Step 6: Execute One Task at a Time

For each task:

  1. Read the task specification
  2. Verify pre-conditions are met
  3. Execute the steps
  4. Run all verification checks
  5. If verification fails — rollback, don't proceed
  6. Update documentation to reflect changes
  7. Move to next task only after human sign-off (if required)

Required Outputs

OutputDescription
Prerequisites checklistVerified list of discovery, docs, and guardrails in place
Scope agreementDocumented boundary, invariants, and success criteria
Strategy rationaleWhy this refactoring approach was chosen
Task listOrdered sequence of tasks per the template
Dependency graphWhich tasks depend on which
Human review mapWhich tasks need human sign-off and why

Hard Rules

  1. One task, one boundary. Each task modifies one well-defined area. No "refactor modules A, B, and C" in one task.
  2. Tests before changes. If a task changes code, the tests covering that code must exist and pass before the task starts.
  3. Existing versions only. Refactored code must work with the project's actual dependency versions. Upgrades are separate tasks.
  4. Use source inspection for risky old dependencies. You must confirm the pinned API via source inspection before changing call sites.
  5. Wrap, don't rewrite. Prefer adapter/strangler patterns over rewrites. Rewrites are last resort.
  6. Prove equivalence. Before removing old code, demonstrate that new code produces identical behavior via tests or comparison.
  7. No drive-by improvements. Only change what the task specifies. Don't "improve" adjacent code.
  8. Document as you go. Update docs after each task. Future AI sessions depend on accurate docs.
  9. Stop on uncertainty. If a task reveals unexpected complexity, stop and create a new discovery round — don't push through.

Anti-Patterns

Don'tDo instead
Plan a big-bang migrationBreak into 5-20 small independent tasks
Assume latest API compatibilityVerify every API against lockfile versions
Remove old code before new code is provenRun both in parallel, compare, then cut over
Combine test-writing with refactoring in one taskSeparate "add tests" and "refactor" into distinct tasks
Skip human review for P0-CRITICAL changesAlways flag P0 changes for human sign-off
Continue when a verification check failsStop, rollback, investigate, re-plan

Human Review Triggers

These ALWAYS require human sign-off before proceeding:

  • Any change to P0-CRITICAL or LEGACY-SEALED modules
  • 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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.