test-preserving-refactor

test-preserving-refactor is a skill for Claude Code from yeaight7/agent-powerups. It costs 27 tokens per session (592 once invoked), scanned A, original, Apache-2.0.

A refactoring workflow that uses tests to confirm the software still behaves the same after each structural code change. Refactoring means changing code organization without changing what it does.

In plain words
What is it for?
Use it when extracting, moving, or inlining code in an area covered by tests, or when restructuring has previously caused breakage.
Why use it?
It catches mistakes immediately and provides a safe way to stop or undo a change when tests fail.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin.

Part of the codebase-maintenance plugin — 8 skills, 4 commands, 3 agents shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/yeaight7/agent-powerups/test-preserving-refactor
Any agent
npx skills add yeaight7/agent-powerups --skill test-preserving-refactor
Clone the repo
git clone --depth 1 https://github.com/yeaight7/agent-powerups

Made for: Claude Code.

Or install codebase-maintenance, the plugin that ships this one along with the rest of its 8 skills, 4 commands, 3 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for test-preserving-refactor

README.md
[![agentmods](https://agentmods.dev/badge/skills/yeaight7/agent-powerups/test-preserving-refactor.svg)](https://agentmods.dev/skills/yeaight7/agent-powerups/test-preserving-refactor)
Your own site
<a href="https://agentmods.dev/skills/yeaight7/agent-powerups/test-preserving-refactor"><img src="https://agentmods.dev/badge/skills/yeaight7/agent-powerups/test-preserving-refactor.svg" alt="Measured on agentmods" height="20"></a>
Per session 27 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 592 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00027 $0.00592
Opus 5 $0.00014 $0.00296
Sonnet 5 $0.00005 $0.00118
Haiku 4.5 $0.00003 $0.00059

Measured 6d ago against content hash b1a2f6d4b2c1, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-06, from the pricing page.

Security

Grade A, and why

test-preserving-refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 6d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/codebase-maintenance/skills/test-preserving-refactor/SKILL.md · 62 lines

How it starts

The opening of the file, as written. The whole thing — 62 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Purpose

Refactoring is only safe if it is backed by tests. Work in a strict loop — green baseline, one structural change, immediate re-run, revert on red — so you are never trapped in an uncompilable state.

When to Use

  • Code needs restructuring (extract/inline/move) without behavior change
  • A previous refactor spiraled into a broken, hard-to-recover state
  • Tests covering the target area exist or can be run

Inputs

  • The target area and the command that runs just its tests

Workflow

  1. Run tests first. Before touching any code, run the tests covering the target area. They MUST be green. If they are red, stop and fix the tests (or the code) first:

    npx jest path/to/target           # or: pytest tests/test_target.py -q
    
  2. Small steps. Make one structural change at a time (e.g., extract a method).

  3. Run tests immediately after the single structural change. The per-change loop:

    # repeat per structural change: edit -> test -> commit (green) | revert (red)
    npx jest path/to/target && git commit -am "refactor: extract X" || git checkout -- .
    
  4. Revert on red. If the tests fail, you made a mistake. Revert the change (git checkout -- . or git restore .) and try a different approach. Do not attempt to "fix" the refactor while tests are failing.

  5. Commit on green. Once the small change is green, consider it a safe checkpoint.

This strict loop prevents you from getting trapped in an uncompilable state.

Output

  • A chain of small, individually green commits
  • The final test run output proving unchanged behavior

Verification

  • Baseline test run was green before the first edit
  • Exactly one structural change per test cycle
  • Every red result handled by revert, not forward-fixing
  • Each green step committed as a checkpoint
  • No test was modified to make the refactor pass

Failure Modes

  • Refactoring on red — starting from failing tests means you cannot tell what you broke.
  • Step batching — three changes per test run; when it fails, you don't know which one did it.
  • Forward-fixing a broken refactor — "fixing" while red digs the hole deeper; revert is cheaper.
  • Bending the tests — editing assertions to match new behavior is a behavior change, not a refactor.

Read the full file on GitHub · 62 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 6d ago First seen · 62 lines · 27 tokens per session scan A b1a2f6d4b2c1

Subscribe to this mod's changes

test-preserving-refactor is a skill published in the GitHub repository yeaight7/agent-powerups (6 stars, last pushed 1mo ago), licensed Apache-2.0. It adds 27 tokens to every session and 592 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.