refactor

refactor is a command for Codex from KaelSensei/MagicAIBuilder. It costs 0 tokens per session (886 once invoked), scanned A, original, MIT.

A command for restructuring code without changing what the program does. It first loads project rules and documentation, then limits the work to organisation, naming, readability, duplication, and type safety.

In plain words
What is it for?
Use it for `/refactor` requests involving code cleanup, clearer names, reduced repetition, or safer types. It is not intended for new features, behaviour changes, or bug fixes.
Why use it?
Refactoring can accidentally introduce new behaviour or mix in unrelated bug fixes. This command defines a narrower scope and requires clarification when the request is unclear.

Command for Codex

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/kaelsensei/magicaibuilder/refactor
Clone the repo
git clone --depth 1 https://github.com/KaelSensei/MagicAIBuilder

Made for: Codex.

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 refactor

README.md
[![agentmods](https://agentmods.dev/badge/commands/kaelsensei/magicaibuilder/refactor.svg)](https://agentmods.dev/commands/kaelsensei/magicaibuilder/refactor)
Your own site
<a href="https://agentmods.dev/commands/kaelsensei/magicaibuilder/refactor"><img src="https://agentmods.dev/badge/commands/kaelsensei/magicaibuilder/refactor.svg" alt="Measured on agentmods" height="20"></a>
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 886 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.00000 $0.00886
Opus 5 $0.00000 $0.00443
Sonnet 5 $0.00000 $0.00177
Haiku 4.5 $0.00000 $0.00089

Measured 4d ago against content hash e97c4d2c2e48, 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 4d 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.

.agents/commands/refactor.md · 132 lines

How it starts

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

Refactor Command – Non-Functional Code Improvements

When /refactor <description> is invoked, immediately execute the following steps.


Step 1: Load Project Context & Follow All Rules

  1. Assume the project root as the working directory
  2. Load and strictly follow ALL Cursor rules from .cursor/rules/*.mdc:
    • security.mdc - Security requirements
    • technical-stack.mdc - React Native/SQLite patterns
    • documentation.mdc - Documentation update requirements
    • version-management.mdc - Git commit/push workflow
  3. Read relevant documentation:
    • README.md
    • Architecture documents
    • Any file directly affected by the refactor
  4. Identify the current Git branch and assume it is a refactor branch, not main

Step 2: Define Refactor Scope (Critical)

  1. Parse the description provided after /refactor
  2. Explicitly define what is allowed:
    • Code organization
    • Naming
    • Readability
    • Duplication removal
    • Type safety improvements
  3. Explicitly define what is not allowed:
    • No behavior changes
    • No logic changes
    • No feature additions
    • No bug fixes (use /fix instead)
  4. If scope is ambiguous, stop and ask before proceeding

Step 3: Security & Safety Validation

Before making changes:

  1. Assume refactors can accidentally introduce vulnerabilities
  2. Review affected code for:
    • Hidden logic changes
    • Control-flow alterations
    • Subtle semantic changes
  3. Use MCP (Model Context Protocol) or equivalent reasoning to ensure:
    • Refactor preserves behavior exactly
    • No new execution paths are introduced
    • No backdoors, dynamic execution, or unsafe patterns appear
  4. If exact equivalence cannot be guaranteed, abort and ask

Step 4: Perform the Refactor

  1. Apply minimal and mechanical transformations only
  2. Prefer:
    • Extracting small pure functions
    • Improving naming consistency
    • Reducing nesting
    • Improving typing and explicitness
  3. Avoid:
    • Reordering logic unless provably equivalent
    • Changing conditionals or loops
    • Introducing new dependencies
  4. Keep changes tightly scoped and easily reviewable

Read the full file on GitHub · 132 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. 4d ago First seen · 132 lines · 0 tokens per session scan A e97c4d2c2e48

Subscribe to this mod's changes

refactor is a command published in the GitHub repository KaelSensei/MagicAIBuilder (2 stars, last pushed 2d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 886 tokens. 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.

Related

Other commands, from other repositories