refactor

A command for reorganising code while preserving its behaviour. It identifies common code-design problems, plans small refactoring steps, and checks each step with tests.

In plain words
What is it for?
Use it to diagnose issues such as overly long functions or duplicated changes, plan named refactoring steps, and identify or add tests that protect the existing behaviour.
Why use it?
It helps improve difficult code without combining the cleanup with new features or changing what the software does.

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/lugassawan/swe-workbench/refactor
Clone the repo
git clone --depth 1 https://github.com/lugassawan/swe-workbench
Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 438 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.00014 $0.00438
Opus 5 $0.00007 $0.00219
Sonnet 5 $0.00003 $0.00088
Haiku 4.5 $0.00001 $0.00044

Measured 2d ago against content hash 6ecea24ad1ad, 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 · 20 lines

What it actually says

Target: $ARGUMENTS

If $ARGUMENTS contains a ticket reference, invoke swe-workbench:ticket-context first and prepend its structured summary to the delegation context below. Skip if $ARGUMENTS is free-text with no recognizable ref. (Trigger patterns are defined in that skill's "When to invoke" section.)

Delegate to the swe-workbench:refactorer subagent. Its output must include:

  1. Diagnosis — which smell is present (Long Method, Feature Envy, Primitive Obsession, Shotgun Surgery, Divergent Change, etc.) and why it hurts.
  2. Target state — the shape of the code after refactoring, referenced to Fowler's catalog.
  3. Step plan — ordered steps, each behavior-preserving and independently testable, each named from the catalog (Extract Function, Move Function, Replace Conditional with Polymorphism, Introduce Parameter Object…).
  4. Verification — which tests protect each step; write characterization tests first if coverage is missing.

Absolute rule: no feature changes during refactoring.

Plan output: If you (the orchestrator) author a plan based on the subagent's response and that plan modifies the codebase (fix / make / implement) — whether saved to a plan file or passed to ExitPlanMode — first activate swe-workbench:workflow-development in Mode A and embed the rendered ## Workflow section in the plan per skills/workflow-development/templates/plan-workflow-section.md. Run the skill's project-detection (git branch -a, git log --oneline -20, Makefile grep, PR-template lookup) so the placeholders are substituted from this repo, not left as [[detect:…]]. Skip Mode A if the plan is pure analysis, design recommendation, or any output that does not introduce file edits — the Workflow section's phases (Branch / Verify / Deliver) do not apply.

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 · 20 lines · 14 tokens per session scan A 6ecea24ad1ad

Subscribe to this mod's changes

refactor is a command published in the GitHub repository lugassawan/swe-workbench (2 stars, last pushed 2d ago), licensed MIT. It adds 14 tokens to every session and 438 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.