SKILL: Code Hygiene Refactor
Purpose
Drive active cleanup of duplication, stale files, unnecessary code, oversized modules, and retroactive technical debt while preserving behavior. This skill is a Markdown-only operational tool: it tells the agent what to inspect, what to block, and what evidence to record.
Activation Triggers
Load this skill when:
- a user reports duplication, monoliths, repeated snippets, stale files, unnecessary files, dead code, technical debt, cleanup, or the Portuguese equivalents: higiene de código, limpeza, duplicação, código duplicado, arquivo obsoleto, arquivo morto, código morto, refatoração, dívida técnica, monolito, or sobras de implementação;
- retroactive instantiation is applied to an existing project;
- a file is large, low-cohesion, or repeatedly changed;
- a plan touches code with known
#tech-debt; - an agent is tempted to copy/paste similar code;
- a cleanup/refactoring plan is opened;
- the user asks to remove, delete, retire or discontinue an application item (button, field, column, modal, tab, page, route, menu, action, feature) — the removal includes sanitizing the item's code, per
REFACTORING.mdsection 26.
Inputs
Active plan and scope, changed-path and ownership evidence, similar-code search, dependency/consumer references, focused behavior checks, and rollback or restoration source for removals.
Non-Negotiable Rules
- Preserve observable behavior unless a separate functional plan exists.
- Do not remove dead code without evidence from search, tests, owners, routes, or build behavior.
- Do not combine broad cleanup with a feature delivery unless the cleanup is required to keep the feature small. Code that existed only for an item the user asked to remove is not broad cleanup: it is part of the removal.
- A requested removal does not end at hiding, disabling or disconnecting: inventory and remove the item's exclusive interface, state, styles, texts, routes, services, policies, tests, scripts and documentation; record each exception (shared code, persisted data, public contract, historical record) with its reason.
- Do not introduce new dependencies for cleanup without explicit research and plan approval.
- Prefer extracting existing repeated logic over inventing generic abstractions.
- Keep each cleanup batch reviewable and reversible.
Proportionality Check
Before creating a utility, service, wrapper, component, dependency, file, layer, or parallel mechanism, evaluate in order:
- Is the behavior necessary in the current scope?
- Does the codebase already contain a complete or partial solution?
- Does the language, framework, platform, or infrastructure provide a native capability?
- Does an already installed dependency solve it adequately?
- Only then, is new code justified?
proportionality_gate: required
ladder:
- in_scope
- existing_solution
- native_capability
- installed_dependency
- justified_new_code
advisory_when: bounded_direct_change
For any non-trivial increase in technical complexity, record:
- the real problem and root cause;
- reused assets and searches performed;
- native and installed alternatives considered;
- why the smallest viable option is insufficient;
- minimum behavior tests;
- limitations of the deliberate simplification; and
- the condition that would justify future evolution.
This check is mandatory when adding a dependency, abstraction, service, wrapper, architectural layer, or parallel workflow. It is advisory for small direct changes whose implementation is self-evidently bounded. It must never reduce security, privacy, accessibility, traceability, validation, audit, compliance, data integrity, or risk-required tests.
Do not create a separate anti-overengineering skill unless repeated evidence shows this integrated check cannot cover the workflow.
Hygiene Scan
Use the smallest scan compatible with the risk:
| Scan | Use when | Evidence |
|---|---|---|
| Local | One file or one function is changing. | Similar-code search, direct callers, before/after behavior. |
| Module | A component, route, service, prompt pack, or folder is changing. | Inventory table, imports/callers, tests, duplication candidates. |
| Systemic | Multiple modules, legacy migration, or retroactive adoption. | Dependency/impact map, phased plan, rollback, characterization tests. |
Findings Matrix
Classify each finding before editing:
| Finding | Required action |
|---|---|
| Duplicate code | Extract only when the shared rule is truly identical; otherwise document intentional variation. |
| Large file | Load anti-monolith-guard; split by responsibility before adding behavior. |
| Dead code | Mark CANDIDATE_FOR_REMOVAL; remove only after validation. |
| Stale file | Mark authoritative source and obsolete copy; remove only inside a plan. |
| Catch-all utility | Split by domain capability or keep scoped with explicit owner. |
| Repeated prompt | Move stable prompt rule to versioned AI/resource doc; keep context examples separate. |
| Missing tests | Add characterization or manual validation before structural change. |
| Hidden dependency | Make dependency explicit or document order/contract before moving code. |
Cleanup Sequence
- Inventory the target area.
- Identify duplicates, large files, stale files, dead code, and catch-all modules.
- Rank findings by risk and user impact.
- Choose one small cleanup batch.
- Characterize current behavior.
- Apply the smallest extraction, deletion, move, or rename.
- Validate behavior.
- Record result in plan, changelog, and wiki if reusable.
Requested Removal
Use the Local scan for a button or field and Module for a modal, page or feature. The full rule, inventory and exceptions are in REFACTORING.md section 26. Plan block (prose is equally valid; a blank labelled field fails plan-removal):
## Removal sanitization
- Removed item and originating request:
- Terms searched (component, label, key, route, policy, text, style, test):
- Removed: interface / state / styles and texts / routes and services / policies / tests and scripts / documentation
- Exceptions with reason (shared, data, public contract, history) and deadline:
- Final search without an unjustified remaining hit:
Output Required
Create or fill a report using the code hygiene section of governance/TEMPLATE_REFACTORING.md when the scan is module-level or systemic. For local scans, add this compact block to the active plan:
## Code Hygiene Scan
- Skill loaded: `skills/code-hygiene-refactor/SKILL.md`
- Scan level: `local` / `module` / `systemic`
- Duplicate candidates:
- Large/monolithic candidates:
- Dead/stale candidates:
- Cleanup batch selected:
- Behavior preservation evidence:
- Deferred debt:
Exit Criteria
The cleanup is complete only when duplication, monolith risk, stale files, or dead code were reduced or explicitly deferred with evidence and a follow-up plan. A requested removal is complete only with its Removal sanitization block filled and a clean final search.