From codebrain
Use when restructuring code without changing behavior — extracting functions, splitting files, renaming, reorganizing modules, reducing complexity, or cleaning up debt. Ensures behavior is preserved through snapshot testing before and after. Not for bug fixes or features.
npx claudepluginhub chrsmay/codebrain-plugin --plugin codebrainThis skill uses the workspace's default tool permissions.
Safe refactoring workflow. Change structure, preserve behavior. Test before AND after.
Creates isolated Git worktrees for feature branches with prioritized directory selection, gitignore safety checks, auto project setup for Node/Python/Rust/Go, and baseline verification.
Executes implementation plans in current session by dispatching fresh subagents per independent task, with two-stage reviews: spec compliance then code quality.
Dispatches parallel agents to independently tackle 2+ tasks like separate test failures or subsystems without shared state or dependencies.
Safe refactoring workflow. Change structure, preserve behavior. Test before AND after.
Refactoring = same behavior, different structure. If the behavior changes, it's not a refactoring — it's a feature or bug fix. Use /codebrain:plan for those instead.
/codebrain:refactor <what to refactor>
Examples:
/codebrain:refactor split auth.ts into separate modules/codebrain:refactor extract duplicate validation logic/codebrain:refactor reduce complexity in processOrder functionRead the constitution. Refactoring should move code TOWARD constitution compliance, not away from it.
Analyze the target code.
mcp__codebase_memory__search_graph) to understand how the target connects to the rest of the systemmcp__code_review_graph__get_impact_radius_tool to identify the blast radiusIdentify the specific problem. What makes the current structure bad?
Identify the behavioral contract.
Run existing tests.
npm test / pytest — record pass/fail stateRun build + lint.
Save snapshot to .codebrain/active/refactor-snapshot.md:
# Refactor Snapshot
**Target:** [file(s)]
**Tests:** [N passing, M failing] (before refactoring)
**Build:** PASS/FAIL
**Public API:** [list of exported functions/types that must not change]
Generate refactoring plan. Spawn planner agent with:
Plan must include:
Present plan for approval. If the plan changes any public API signatures, that's a breaking change — flag it and get explicit user approval.
Execute ONE refactoring step at a time.
git add && git commit -m "refactor: [step description]")Common refactoring operations:
Run the full test suite. Compare against Phase 2 snapshot:
Run build + lint. Compare against snapshot.
Run /codebrain:quality on the refactored files to confirm improvement:
Check public API preserved.
[BREAKING_CHANGE] and confirm with userInvoke /codebrain:verify against the refactor snapshot to confirm behavioral equivalence.
Update imports across the codebase. If files were split or renamed, ensure all consumers are updated.
Delete the refactor snapshot. It served its purpose.
Update memory.
.codebrain/memory/patterns.md.codebrain/memory/architecture.md if module boundaries changed.codebrain/memory/continuity.md with what was refactored and why