What this skill teaches your AI coding agent
Behavior-Preserving Refactoring strictly separates structural reorganization from functional modifications. Under this protocol, an agent tasked with cleaning code or modernizing architecture must first prove that the existing test suite executes with a 100% pass rate. During the refactoring pass, zero new features, bug fixes, or API contract alterations are permitted. Edits are executed as micro-transformations, with immediate test verification after every single step; if any test fails, the agent reverts immediately rather than attempting to patch forward.
Behavior-Preserving Refactoring is an open-source engineering skill licensed under CC0-1.0 that enforces zero behavioral mutations and atomic micro-step verification during structural cleanup.
You need strict, reproducible guardrails enforcing software engineering discipline across agent interactions.
You are building quick disposable prototypes where architectural consistency and test verification are unnecessary.
Activation boundaries & prompt triggers
- 💬 "User prompts: "Strict refactoring discipline enforcing zero behavioral mutations, pre-flig...""
- 💬 "User requests code cleanup, architecture modernization, or dead code removal without altering features."
- 💬 "When autonomous agent needs strict protocol guardrails"
- ⛔ Zero observable behavior changes permitted outside explicit refactoring scope.
- ⛔ All existing unit and integration tests must pass before and immediately after changes.
- ⛔ Never mix feature implementation or bug fixes with refactoring passes.
Inject into Claude.ai, Claude Code, or Cursor
Choose your agent runtime to install the playbook directly into local configuration.
.cursor/rules/behavior-preserving-refactoring.mdc---description: Strict refactoring discipline enforcing zero behavioral mutations, pre-flight test gates, and atomic micro-transformations.globs: *alwaysApply: false---# Behavior-Preserving Refactoring RulesWhen tasked with refactoring, cleaning code, or modernizing architecture:1. ZERO-BEHAVIORAL-CHANGE INVARIANT: - You must NOT add new features, alter API response schemas, or fix unrelated bugs. - All observable inputs and outputs must remain strictly identical.2. MICRO-TRANSFORMATION LOOP: - Run test baseline before starting. - Execute exactly one atomic refactor step at a time (e.g., Extract Method). - Run test suite immediately. If tests fail, revert immediately; do not patch forward.Interactive file tree manifest & playbook viewer
Click any file to audit instructions, verification scripts, and architectural references before installation.
SKILL.md---
name: behavior-preserving-refactoring
description: "Strict refactoring discipline enforcing zero behavioral mutations, pre-flight test gates, and atomic micro-transformations."
version: 1.0.0
license: CC0-1.0
compatibility: [claude-3-5-sonnet, cursor, windsurf, cline]
---
# Behavior-Preserving Refactoring Discipline
Behavior-Preserving Refactoring strictly separates structural reorganization from functional modifications. Under this protocol, an agent tasked with cleaning code or modernizing architecture must first prove that the existing test suite executes with a 100% pass rate. During the refactoring pass, zero new features, bug fixes, or API contract alterations are permitted. Edits are executed as micro-transformations, with immediate test verification after every single step; if any test fails, the agent reverts immediately rather than attempting to patch forward.
## Core Guardrails
- ⛔ Zero observable behavior changes permitted outside explicit refactoring scope.
- ⛔ All existing unit and integration tests must pass before and immediately after changes.
- ⛔ Never mix feature implementation or bug fixes with refactoring passes.
## Autonomous Activation Triggers
When user prompts:
- "User prompts: "Strict refactoring discipline enforcing zero behavioral mutations, pre-flig...""
- "User requests code cleanup, architecture modernization, or dead code removal without altering features."
- "When autonomous agent needs strict protocol guardrails"
Verified behavioral proof of work
Identical prompt executed with and without this skill under autonomous coding loops.
Execute Behavior-Preserving Refactoring Discipline under autonomous coding loops
2- // Baseline code3+ // Deterministic scoped patch complying with all protocol invariantsExecution safety and file access boundaries
Agent skills with executable scripts can run shell or Python utilities. Always audit scripts before execution.
- Scope: Bound to active workspace branch.
- Network Calls: Zero outbound network calls.
- AST Syntax Gate: Pre-flight verification blocks breaking mutations.
Operational triage & resolution runbook
Skill not triggering during user conversational prompt▼
Agent system prompt does not associate task keywords with the skill's trigger boundary.
Reference the skill name explicitly in your prompt (e.g., "Follow the rules in behavior-preserving-refactoring to perform this refactoring").
Conflicting rules with root project instructions▼
Global instructions (.cursorrules or CLAUDE.md) contradict the skill's invariants.
Mark this skill as high-priority override in your agent settings or scope it to specific subdirectories.
Context window saturation warning▼
Multiple skills loaded simultaneously without progressive disclosure.
Keep always-on skills under 1,000 tokens total (~420 tokens consumed by this playbook).
Skill vs. MCP Server vs. Plain System Prompt
| Primitive | Best Used When | Token Cost | Execution Risk |
|---|---|---|---|
| Agent Skill (`SKILL.md`) | Multi-step domain workflows & procedural guardrails | Low (~400 tokens) | Minimal (AST verified) |
| MCP Server | External system integrations (APIs, live databases) | Zero (until called) | Scoped by tool permissions |
| Plain System Prompt | Simple style guidelines & tone preferences | High (Always in context) | High drift risk |
Composable peers & official repository
Combine with Adversarial Code Review & Reception for complementary agent steering across testing and refactoring.
Inspect SkillCombine with Subagent-Driven Development for complementary agent steering across testing and refactoring.
Inspect SkillCombine with Systematic Debugging Protocol for complementary agent steering across testing and refactoring.
Inspect Skill