Refactoring Execute
Transforms audit findings into a prioritized action plan and executes refactoring changes incrementally, with mandatory user confirmation before any code modification.
Prerequisites
Before starting, verify these conditions:
Audit report exists. There must be a structured findings document available — either from the refactoring-audit skill or any report that contains categorized, severity-rated findings with file locations. If no report is available, do not proceed. Instead, suggest the user run refactoring-audit first to generate the diagnostic report, then return to this skill with the results.
Version control is clean. Warn the user if you detect uncommitted changes. Recommend they commit or stash before proceeding. If the user chooses to proceed anyway, note the risk but don't block execution. However, if the plan includes high-risk actions (modifying shared code, changing auth logic, restructuring data access), issue a second explicit warning before executing those specific actions without a clean git state.
Stack is identified. If the audit report includes a detected stack, use it. If not, detect the stack the same way refactoring-audit does (scan config files and dependency manifests).
Execution Flow
This skill has two distinct steps that must not be merged:
Step 1 — Generate the Plan
Read references/prioritization-rules.md for the detailed prioritization logic.
Organize all findings from the audit report into three priority tiers:
Priority 1 — IMMEDIATE (execute first):
- All 🔴 findings tagged with 🔒 SECURITY
- Findings that could cause data loss or unauthorized access
- Bugs caused by missing error handling on critical paths
Priority 2 — IMPORTANT (execute after Priority 1 is complete):
- Remaining 🔴 findings not related to security
- 🟡 findings that affect maintainability
- Significant duplication (3+ files)
- Pattern inconsistencies that create confusion
Priority 3 — CLEANUP (execute last):
- All 🟢 findings
- Dead code removal
- Naming improvements
- Readability optimizations
For each action in the plan, specify:
- What: Concrete description of the change
- Where: Exact files to be modified, created, or deleted
- Risk: High / Medium / Low — could this break existing functionality?
- Dependencies: Does this action need to happen before or after another action?
- Grouped with: Actions that should be done together as a unit (e.g., extracting a utility and updating all its consumers)
Example Plan Actions
For calibration, here is what well-formed plan actions look like:
## Priority 1 — IMMEDIATE
### Group A: Fix authentication gaps in project actions
| # | What | Where | Risk | Dependencies |
|---|---|---|---|---|
| A.1 | Add session verification at the start of `deleteProject` | src/actions/projects.ts | High | None |
| A.2 | Add session verification at the start of `updateProject` | src/actions/projects.ts | High | None |
| A.3 | Add user-ownership check before project deletion | src/actions/projects.ts | High | depends-on: A.1 |
MANDATORY PAUSE
After presenting the plan, stop and wait for the user to confirm. Do not proceed to Step 2 until the user explicitly approves the plan or a subset of it.
The user may:
- Approve the full plan → proceed with all priorities in order
- Approve specific priorities only → execute only the approved tiers
- Request modifications → adjust the plan and present again
- Add constraints → respect any files or modules the user says not to touch
- Reject the plan → stop execution, keep the plan as documentation
Step 2 — Execute
Read references/execution-checklist.md for pre/post verification steps.
Once approved, execute the plan following these rules:
Incremental execution:
- Apply one refactoring at a time (or one grouped unit at a time)
- After each change, verify the code still compiles/parses without errors
- If a change causes errors, fix them before moving to the next refactoring
- If a fix isn't obvious, revert the change and flag it for the user
Ordering within each priority:
- Changes that create new shared utilities, types, or helpers (other changes may depend on these)
- Changes that extract or centralize logic
- Changes that update consumers to use the new centralized code
- Changes that remove dead code or unused artifacts
- Changes that rename or restructure
Before deleting anything:
- Verify the file, function, export, or variable is truly unused by searching the entire project
- If there's any ambiguity (dynamic imports, framework conventions, reflection), ask the user before deleting
Before modifying shared code:
- Identify all consumers of the code being changed
- Ensure the modification doesn't break any consumer
- If the change affects a public API or interface, list all affected consumers in the summary
Decision points:
- If a refactoring requires a design decision (e.g., choosing between two valid approaches, naming a new abstraction, selecting a pattern), stop and ask the user
- If a refactoring is riskier than initially assessed, inform the user before proceeding
- If you discover new issues during execution that weren't in the audit, note them but don't act on them unless the user asks
Progress Reporting
After completing each priority tier, provide a summary:
## Priority [N] Complete
### Changes Applied
| # | Action | Files Modified | Files Created | Files Deleted |
|---|--------|---------------|---------------|---------------|
| 1 | [Description] | [paths] | [paths] | [paths] |
### Manual Verification Needed
- [Anything the agent cannot verify automatically — e.g., visual changes, runtime behavior, integration tests]
### New Issues Discovered
- [Any problems found during execution that weren't in the original audit]
### Ready for Priority [N+1]?
Wait for user confirmation before proceeding to the next priority tier.
Critical Rules
- Never skip the plan confirmation. This is non-negotiable. The plan must be presented and approved before any file is modified.
- This is refactoring, not rewriting. Preserve existing functionality. Don't change behavior, only improve structure, clarity, and safety.
- One thing at a time. Resist the temptation to combine multiple unrelated changes in a single step. Each change should be reviewable in isolation.
- Verify before deleting. Dead code identification in the audit may have false positives. Always re-verify before removal.
- Respect the user's constraints. If the user says "don't touch the auth module" or "skip Priority 3," respect that absolutely.
- Adapt to the stack's conventions. When creating new files (utilities, types, helpers), follow the project's existing naming conventions, directory structure, and code style. Don't impose external conventions.
Edge Cases
- Audit report from a different tool: The plan still works as long as findings have descriptions and file locations. Adapt the prioritization to whatever severity system the report uses.
- Project without version control: Warn the user strongly. Recommend they initialize git and commit before proceeding. If they insist on proceeding, acknowledge the risk.
- Conflicting findings: If two findings suggest opposite changes (e.g., one says centralize, another says split), present both options and let the user decide.
- Very large refactoring scope: If the plan has 50+ actions, suggest breaking it into multiple sessions, each covering one priority tier or one module.
1---2name: refactoring-execute3description: Generates a prioritized refactoring plan from audit findings and executes code changes incrementally with user confirmation. Use this skill when the user has a refactoring audit report (from refactoring-audit or any structured findings document) and wants to act on it. Also triggers when the user says "execute the refactoring plan", "fix the audit findings", "refactor based on the report", "apply the refactoring recommendations", "start fixing the issues from the audit", or any request to systematically apply code improvements based on a diagnostic report. This skill modifies files — it always presents a plan and waits for explicit user approval before making any changes.4license: MIT5---67# Refactoring Execute89Transforms audit findings into a prioritized action plan and executes refactoring changes incrementally, with mandatory user confirmation before any code modification.1011## Prerequisites1213Before starting, verify these conditions:14151. **Audit report exists.** There must be a structured findings document available — either from the `refactoring-audit` skill or any report that contains categorized, severity-rated findings with file locations. If no report is available, do not proceed. Instead, suggest the user run `refactoring-audit` first to generate the diagnostic report, then return to this skill with the results.16172. **Version control is clean.** Warn the user if you detect uncommitted changes. Recommend they commit or stash before proceeding. If the user chooses to proceed anyway, note the risk but don't block execution. However, if the plan includes high-risk actions (modifying shared code, changing auth logic, restructuring data access), issue a second explicit warning before executing those specific actions without a clean git state.18193. **Stack is identified.** If the audit report includes a detected stack, use it. If not, detect the stack the same way `refactoring-audit` does (scan config files and dependency manifests).2021## Execution Flow2223This skill has two distinct steps that must not be merged:2425### Step 1 — Generate the Plan2627Read `references/prioritization-rules.md` for the detailed prioritization logic.2829Organize all findings from the audit report into three priority tiers:3031**Priority 1 — IMMEDIATE** (execute first):32- All 🔴 findings tagged with 🔒 SECURITY33- Findings that could cause data loss or unauthorized access34- Bugs caused by missing error handling on critical paths3536**Priority 2 — IMPORTANT** (execute after Priority 1 is complete):37- Remaining 🔴 findings not related to security38- 🟡 findings that affect maintainability39- Significant duplication (3+ files)40- Pattern inconsistencies that create confusion4142**Priority 3 — CLEANUP** (execute last):43- All 🟢 findings44- Dead code removal45- Naming improvements46- Readability optimizations4748For each action in the plan, specify:49- **What**: Concrete description of the change50- **Where**: Exact files to be modified, created, or deleted51- **Risk**: High / Medium / Low — could this break existing functionality?52- **Dependencies**: Does this action need to happen before or after another action?53- **Grouped with**: Actions that should be done together as a unit (e.g., extracting a utility and updating all its consumers)5455### Example Plan Actions5657For calibration, here is what well-formed plan actions look like:5859```60## Priority 1 — IMMEDIATE6162### Group A: Fix authentication gaps in project actions63| # | What | Where | Risk | Dependencies |64|---|---|---|---|---|65| A.1 | Add session verification at the start of `deleteProject` | src/actions/projects.ts | High | None |66| A.2 | Add session verification at the start of `updateProject` | src/actions/projects.ts | High | None |67| A.3 | Add user-ownership check before project deletion | src/actions/projects.ts | High | depends-on: A.1 |68```6970### MANDATORY PAUSE7172After presenting the plan, **stop and wait for the user to confirm**. Do not proceed to Step 2 until the user explicitly approves the plan or a subset of it.7374The user may:75- Approve the full plan → proceed with all priorities in order76- Approve specific priorities only → execute only the approved tiers77- Request modifications → adjust the plan and present again78- Add constraints → respect any files or modules the user says not to touch79- Reject the plan → stop execution, keep the plan as documentation8081### Step 2 — Execute8283Read `references/execution-checklist.md` for pre/post verification steps.8485Once approved, execute the plan following these rules:8687**Incremental execution:**88- Apply one refactoring at a time (or one grouped unit at a time)89- After each change, verify the code still compiles/parses without errors90- If a change causes errors, fix them before moving to the next refactoring91- If a fix isn't obvious, revert the change and flag it for the user9293**Ordering within each priority:**941. Changes that create new shared utilities, types, or helpers (other changes may depend on these)952. Changes that extract or centralize logic963. Changes that update consumers to use the new centralized code974. Changes that remove dead code or unused artifacts985. Changes that rename or restructure99100**Before deleting anything:**101- Verify the file, function, export, or variable is truly unused by searching the entire project102- If there's any ambiguity (dynamic imports, framework conventions, reflection), ask the user before deleting103104**Before modifying shared code:**105- Identify all consumers of the code being changed106- Ensure the modification doesn't break any consumer107- If the change affects a public API or interface, list all affected consumers in the summary108109**Decision points:**110- If a refactoring requires a design decision (e.g., choosing between two valid approaches, naming a new abstraction, selecting a pattern), stop and ask the user111- If a refactoring is riskier than initially assessed, inform the user before proceeding112- If you discover new issues during execution that weren't in the audit, note them but don't act on them unless the user asks113114## Progress Reporting115116After completing each priority tier, provide a summary:117118```119## Priority [N] Complete120121### Changes Applied122| # | Action | Files Modified | Files Created | Files Deleted |123|---|--------|---------------|---------------|---------------|124| 1 | [Description] | [paths] | [paths] | [paths] |125126### Manual Verification Needed127- [Anything the agent cannot verify automatically — e.g., visual changes, runtime behavior, integration tests]128129### New Issues Discovered130- [Any problems found during execution that weren't in the original audit]131132### Ready for Priority [N+1]?133```134135Wait for user confirmation before proceeding to the next priority tier.136137## Critical Rules138139- **Never skip the plan confirmation.** This is non-negotiable. The plan must be presented and approved before any file is modified.140- **This is refactoring, not rewriting.** Preserve existing functionality. Don't change behavior, only improve structure, clarity, and safety.141- **One thing at a time.** Resist the temptation to combine multiple unrelated changes in a single step. Each change should be reviewable in isolation.142- **Verify before deleting.** Dead code identification in the audit may have false positives. Always re-verify before removal.143- **Respect the user's constraints.** If the user says "don't touch the auth module" or "skip Priority 3," respect that absolutely.144- **Adapt to the stack's conventions.** When creating new files (utilities, types, helpers), follow the project's existing naming conventions, directory structure, and code style. Don't impose external conventions.145146## Edge Cases147148- **Audit report from a different tool**: The plan still works as long as findings have descriptions and file locations. Adapt the prioritization to whatever severity system the report uses.149- **Project without version control**: Warn the user strongly. Recommend they initialize git and commit before proceeding. If they insist on proceeding, acknowledge the risk.150- **Conflicting findings**: If two findings suggest opposite changes (e.g., one says centralize, another says split), present both options and let the user decide.151- **Very large refactoring scope**: If the plan has 50+ actions, suggest breaking it into multiple sessions, each covering one priority tier or one module.