# Refactor Code with Tests at Every Step

Structural code changes can alter behavior when tests and scope are unclear. This skill guides small refactoring steps with baseline tests, characterization coverage, and repeated verification.

## Install

```bash
npx skillstore add agentkitco/safe-refactor
```

## Metadata

- Status: approved
- Slug: agentkitco-safe-refactor
- Version: 1.0
- Author version: 1.0
- Skillstore revision: r1
- Version status: invalid
- Tree hash: 2ac924be1ad8453ab10bcdd2226cc3c437f70dd7e7cca9a3fbc83fb0d88f3e3b
- Author: agentkitco
- GitHub username: agentkitco
- License: MIT
- Repository: https://github.com/agentkitco/agentkit-dev-skills/tree/3de707c7875147b65ddff504d86b0d0dbf713fa7/skills/safe-refactor
- Ref: 743429f75b17204d47661a86b577a575ad67517f
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands
- Quality score: 70
- Public page: https://skillstore.pages.dev/skills/agentkitco-safe-refactor
- Manifest: https://skillstore.pages.dev/api/skills/agentkitco-safe-refactor/manifest

## Capabilities

- Defines refactoring goals and behavior boundaries before editing code.
- Locates relevant tests and records baseline results, including existing failures.
- Directs characterization tests for code without coverage before structural changes.
- Searches callers, dependents, documentation, and configuration for affected names.
- Separates renames, extractions, moves, and inlining into independently verified steps.
- Reports completed changes, verification results, deferred issues, and uncertain behavior.

## Use Cases

- Clean Up a Legacy Module: Add characterization tests before extracting repeated logic while preserving existing outputs and error behavior.
- Rename an Internal Interface: Find dependent tests, scripts, documentation, and configuration before renaming internal symbols in small verified steps.
- Prepare Reviewable Structural Changes: Separate structural improvements from bug fixes and summarize test evidence for each step.

## Prompt Templates

### Beginner: Rename a Local Symbol

```
Use safe-refactor to rename [symbol] in [file]. Preserve behavior, find references, and run relevant tests.
```

### Intermediate: Extract Repeated Logic

```
Extract duplicated logic from [module]. Record baseline tests, preserve errors and ordering, and verify each extraction separately.
```

### Advanced: Cover Untested Behavior

```
Refactor [legacy module]. First add characterization tests for current behavior, including edge cases. Keep discovered bugs separate from structural changes.
```

### Expert: Refactor Across Modules

```
Restructure [modules] within [scope]. Inventory callers and public APIs. Preserve existing edits, verify each step, and ask before any API break.
```

## Limitations

- The skill provides workflow instructions, not a standalone refactoring engine.
- Behavior preservation depends on test coverage and executable verification.
- Unavailable tests, unexplained baseline failures, public API breaks, and discovered bugs require user discussion.
- Rollback guidance needs additional safeguards to preserve pre-existing user edits.

## Best Practices

- Define behavior boundaries and record baseline test results before making structural changes.
- Preserve pre-existing user edits and undo only changes from the current refactoring step.
- Make one kind of change per step and verify it before proceeding.

## Anti Patterns

- Mixing structural cleanup with bug fixes, features, or dependency updates.
- Continuing through failing tests without restoring the last verified state.
- Renaming public symbols without checking dependents and obtaining approval for breaking changes.

## Security Audit

- Audited at: 2026-10-17T19:54:05.118\+00:00
- Summary: All three static findings are false positives: inline Markdown command references and a completion report do not establish shell execution or system reconnaissance. A separate rollback instruction could discard pre-existing user edits if git restore is applied without isolating the refactoring changes. No evidence found of prompt injection, credential access, or data exfiltration.

## Stats

- Views: 0
- Downloads: 0
- Favorites: 0
- Popularity score: 0
