From cape
Behavior-preserving code transformation workflow — the "evolve chain." Use whenever the user wants to restructure code without changing what it does. Triggers on: "refactor", "extract", "inline", "rename across", "move this to", "reduce duplication", "simplify", "split this function", "clean up", "restructure", "decouple", "untangle." Also triggers mid-workflow: when execute-plan hits structural problems that block feature work, or when review/code-reviewer flags structural issues to act on. Covers single-operation refactorings (Extract Method, Rename) through multi-step restructurings (decompose module, invert dependency). Do NOT use for: adding new behavior (use execute-plan), fixing broken behavior (use fix-bug), architectural redesign where the target structure is unclear (use brainstorm).
npx claudepluginhub sqve/cape --plugin capeThis skill uses the workspace's default tool permissions.
<skill_overview> Restructure code while preserving behavior. Every transformation follows the same
Guides Next.js Cache Components and Partial Prerendering (PPR) with cacheComponents enabled. Implements 'use cache', cacheLife(), cacheTag(), revalidateTag(), static/dynamic optimization, and cache debugging.
Guides building MCP servers enabling LLMs to interact with external services via tools. Covers best practices, TypeScript/Node (MCP SDK), Python (FastMCP).
Generates original PNG/PDF visual art via design philosophy manifestos for posters, graphics, and static designs on user request.
<skill_overview> Restructure code while preserving behavior. Every transformation follows the same contract: verify tests pass before touching anything, name the refactoring, execute it in small verified steps, confirm tests still pass after. The safety net is existing tests and LSP — not new tests.
This is the "evolve chain" — distinct from building (new behavior) and fixing (broken behavior). Code works correctly but its structure makes the next change harder than it should be. </skill_overview>
<rigidity_level> MEDIUM FREEDOM — The safety protocol (test gate → transform → verify) is non-negotiable. Everything else — which refactoring to apply, step size, whether to use LSP or manual edits — adapts to context. </rigidity_level>
<when_to_use>
Don't use for:
</when_to_use>
<critical_rules>
cape check after every step — not just at the end. Each step must independently
preserve behavior</critical_rules>
<the_process>
Before touching code, identify what you're doing. Use Fowler's refactoring vocabulary when it fits — it communicates intent precisely and constrains the transformation to a known-safe pattern.
Common refactorings and when they apply:
| Refactoring | Signal |
|---|---|
| Extract Function/Method | Long function, commented sections, reusable logic |
| Inline Function | Wrapper that adds no clarity, single-use indirection |
| Move Function/Class | Function lives in the wrong module, cross-module coupling |
| Rename | Name no longer reflects purpose, misleading identifier |
| Extract Variable | Complex expression used multiple times or hard to read |
| Inline Variable | Variable adds no clarity over the expression it holds |
| Split Loop | Single loop doing multiple unrelated things |
| Replace Conditional with Polymorphism | Switch/if chain on type that grows with each variant |
| Decompose Module | Module has multiple responsibilities, hard to test in isolation |
| Invert Dependency | Concrete dependency that should be injected or abstracted |
If the transformation doesn't map to a named refactoring, describe it in one sentence: "Collapse the three handler functions into a single dispatch table."
For multi-step restructurings, list the sequence of named refactorings upfront:
Plan:
1. Extract Function — pull validation logic out of createUser
2. Move Function — move extracted validator to validation.ts
3. Rename — validator → validateUserInput to match module conventions
Present the plan to the user before executing.
Run cape check to baseline the safety net. On non-zero exit, read checkResults from JSON output
and surface entries where passed: false as warnings. If failures are unrelated to the refactor
target, proceed — note them for the user. If failures indicate the test suite can't cover the area
being refactored, stop and fix first. The per-step and final-step cape check gates below remain
strict: they verify the transform preserved behavior, so a regression there must halt.
Full suite: [PASS/FAIL] — [N] tests, [M] failures
If tests pass: you have a safety net. Proceed.
If tests fail: stop. Pre-existing failures mean you cannot distinguish regressions from prior breakage. Tell the user: "N tests are already failing. Fix these first, or confirm you want to proceed knowing the safety net has holes."
Assess coverage of the target code:
Check whether the code you're about to transform is actually exercised by tests. Use LSP (find references from test files), grep for imports/calls from test directories, or read the relevant test files.
cape:find-test-gaps to identify what needs coverage. If the user wants to proceed anyway,
acknowledge the risk and take smaller steps with manual verification.Apply the refactoring in small, verified steps. Each step should be independently safe — if tests break, you know exactly which change caused it.
For each step:
cape checkUse LSP when available:
If LSP is unavailable, use Grep to find all references before transforming and verify them after. This is slower but achieves the same safety.
Scope guard:
Run cape check one final time.
## Refactoring complete
**Transformation:** [Named refactoring(s) applied]
**Files changed:** [List with brief description of each change]
**Tests:** [N] passing, [M] unchanged from baseline
**Behavior:** Preserved — no test output changed
Commit this?
Wait for user approval, then load cape:commit with the Skill tool.
If this refactoring was mid-workflow preparation (called from execute-plan), return to the triggering skill after committing. The structural problem is resolved — the feature work can proceed.
</the_process>
<agent_references>
cape:codebase-investigator dispatch:Dispatch when you need to understand the dependency graph before a Move or Decompose refactoring — who imports this module, what depends on this interface, how deep does the coupling go.
cape:test-runner dispatch:Dispatch for every test suite run during the transformation. Keeps test output out of main context.
cape:code-reviewer dispatch:Optional — dispatch with model sonnet after a multi-step restructuring to verify the result is
cleaner than what you started with. Pass the diff and the refactoring plan (not epic requirements —
there is no epic).
</agent_references>
<skill_references>
cape:find-test-gaps with the Skill tool when:cape:commit with the Skill tool when:</skill_references>
Direct request to extract a functionUser: "Extract the validation logic from createUser into its own function"
Wrong: Read createUser, eyeball which lines are "validation," cut-paste them into a new function, hope nothing breaks.
Right:
Working on a feature that adds rate limiting to the API handler. The handler is a 200-line function that mixes routing, auth, business logic, and response formatting. Adding rate limiting here would make it worse.
Wrong: Jam rate limiting into the monolith. It works but the function is now 240 lines and even harder to modify next time.
Right:
Extracting a function and notice the original code has an off-by-one error in a boundary check.
Wrong: Fix the bug while refactoring. Now the commit mixes structural changes with behavioral changes, and if tests break you don't know which change caused it.
Right: Complete the refactoring with the bug intact — behavior preservation means preserving bugs too. After committing the refactoring, tell the user: "Found a potential off-by-one at utils.ts:47. Want to address it with cape:debug-issue?"
No test coverage for target codeUser: "Decompose this 500-line module into three focused modules"
Run tests — suite passes, but none of the tests import or exercise the target module.
Wrong: Proceed with the decomposition. No tests means no safety net — you could silently break every caller.
Right: "This module has no test coverage. Decomposing without tests risks silent breakage. I recommend adding characterization tests first — want me to load cape:find-test-gaps to identify what needs coverage?"
<key_principles>
</key_principles>