refactor

A command for reorganizing existing code while keeping its behavior unchanged. It first checks the relevant files, existing tests, scope, and risk before suggesting or making improvements.

In plain words
What is it for?
Use it to refactor a file, module, feature, or recent branch changes, with extra testing when coverage is weak.
Why use it?
It makes code easier to maintain while reducing the chance that a cleanup breaks existing behavior.

Command

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 commands/rchgrav/claudebox/refactor
Clone the repo
git clone --depth 1 https://github.com/RchGrav/claudebox
Per session 9 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 855 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 $0.00009 $0.00855
Opus 5 $0.00005 $0.00428
Sonnet 5 $0.00002 $0.00171
Haiku 4.5 $0.00001 $0.00085

Measured 2d ago against content hash 059fb2e9e900, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

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 2d 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.

commands/refactor.md · 101 lines

What it actually says

You are now in refactoring mode. Focus on improving code design while keeping tests green.

Target: $ARGUMENTS

SCOPE DETECTION:

If no arguments provided:

  • On feature branch: Automatically assess changes since branching from main
  • On main branch: Ask user what they'd like refactored

If arguments provided:

  • File path: Refactor specific file (e.g., "src/auth.js")
  • Module/area: Refactor specific functionality (e.g., "authentication module")
  • Feature: Refactor specific behavior (e.g., "payment processing")

REFACTORING WORKFLOW:

  1. Assess current code:

    • Read and understand the code to be refactored
    • Identify the scope and boundaries
    • Run tests to ensure current behavior is captured
  2. Impact assessment:

    • High impact: Code changed frequently + complex logic + poor readability
    • Medium impact: Code changed occasionally + moderate complexity
    • Low impact: Stable code + simple logic + already readable
  3. Risk assessment:

    • Low risk: Well-tested code, simple changes
    • Medium risk: Some test coverage, moderate changes
    • High risk: Poor test coverage, complex changes → require characterization tests first
  4. Code smell detection:

    • Long functions (>20 lines) with multiple responsibilities
    • Deep nesting (>3 levels) indicating complex control flow
    • Magic numbers/strings without named constants
    • Feature envy (functions accessing other objects' data excessively)
    • Shotgun surgery (small changes requiring edits across many files)
  5. ROI prioritization:

    • Immediate value: Code being actively modified
    • Future value: Code likely to change soon
    • Team value: Code that confuses multiple developers
    • Maintenance value: Code with frequent bugs
  6. Context integration:

    • Review recent commits for patterns needing improvement
    • Check PR feedback for recurring refactoring suggestions
    • Identify code that slows down current feature development
  7. Make incremental changes:

    • One improvement at a time
    • Run tests after each change
    • Commit improvements that provide value

CODE QUALITY CHECKLIST:

Dead code elimination:

  • Functions or utilities that aren't called anywhere
  • Parameters that aren't used by any callers
  • Configuration options that aren't referenced
  • Helper functions added "just in case" but never used
  • Rule: Every function/utility must have demonstrated usage

Duplication elimination:

  • Multiple functions defining the same variables
  • Similar validation logic within functions
  • Repeated string constants or configuration values
  • Identical error handling patterns

Design improvements:

  • Extract common functionality into utility libraries
  • Consolidate configuration into modules
  • Improve function naming and interface design
  • Break large functions into smaller, focused ones

Maintainability enhancements:

  • Centralize configuration (paths, constants, defaults)
  • Improve error messages and user feedback
  • Add helpful code comments for complex logic
  • Simplify conditional logic and reduce nesting

BRANCH ANALYSIS COMMANDS:

  • git branch --show-current - Check current branch
  • git diff main...HEAD --name-only - See changed files since main
  • git diff main...HEAD - See all changes since main
  • git log main..HEAD --oneline - See commits since branching

REFACTORING PRINCIPLES:

  • Keep tests green throughout the process
  • Make small, focused improvements
  • Focus on code you recently wrote or are actively changing
  • Commit improvements incrementally with clear messages
  • Test behavior remains unchanged after refactoring

Focus on the specific scope. Make incremental improvements. Keep tests green.

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. 2d ago First seen · 101 lines · 9 tokens per session scan A 059fb2e9e900

Subscribe to this mod's changes

refactor is a command published in the GitHub repository RchGrav/claudebox (1,142 stars, last pushed 1y ago), licensed MIT. It adds 9 tokens to every session and 855 once invoked, about $0.0000 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-30.