refactor

A command for simplifying code without changing what it does. It focuses on reducing complexity, improving names and readability, removing repetition, and making functions smaller.

In plain words
What is it for?
Use it for safe refactoring of an existing project. It detects the project’s test command, runs the relevant tests, and stops for a decision if the refactor causes failures.
Why use it?
It helps make code easier to understand and maintain while preserving the public interface. It requires existing tests to pass so accidental behavior changes are detected.

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/hamr0/liteagents/refactor
Clone the repo
git clone --depth 1 https://github.com/hamr0/liteagents
Per session 8 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 535 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.00008 $0.00535
Opus 5 $0.00004 $0.00267
Sonnet 5 $0.00002 $0.00107
Haiku 4.5 $0.00001 $0.00053

Measured yesterday against content hash 554ad426b20f, 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 yesterday.

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.

packages/ampcode/commands/refactor.md · 54 lines

What it actually says

Refactor $ARGUMENTS.

Goals

  • Reduce complexity
  • Improve readability
  • Apply DRY
  • Better naming
  • Smaller functions (single responsibility)

Constraints

  • NO behavior changes
  • Keep public API intact
  • Existing tests must pass

Explain each change.

After the refactor — verify it didn't break anything

"Existing tests must pass" is the load-bearing constraint, and the only honest way to know is to run them.

  1. Detect the project's test command (look for package.json scripts, pytest.ini / pyproject.toml, go.mod, Cargo.toml, Makefile). If none is found, stop and ask before claiming the refactor is done — silent green isn't acceptable.
  2. Run the tests. Scope to the affected area when possible (-t, --testPathPattern, pytest path/, go test ./pkg); otherwise run the suite.
  3. Report pass / fail counts and any failure's name + file:line.

Stop and ask when (HITL gates — not all the time, only here):

  • a test fails after the refactor. Don't auto-revert (destroys work-in-progress) and don't push forward (the no-behavior-change constraint is broken). Present the failure and the options: revert, patch the refactor, or update the test (with reasoning).
  • the refactor crossed a public API boundary that callers depend on — even if tests pass, downstream consumers may break.
  • the change is bigger than the user asked for (scope creep — unrelated cleanups, formatting, comment edits). Confirm before applying.

Final report:

  • refactor done, tests N pass / 0 fail — ready, OR
  • refactor done, but K tests fail — awaiting direction (revert / patch / update test).
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. yesterday First seen · 54 lines · 8 tokens per session scan A 554ad426b20f

Subscribe to this mod's changes

refactor is a command published in the GitHub repository hamr0/liteagents (22 stars, last pushed 2d ago), licensed Apache-2.0. It adds 8 tokens to every session and 535 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.