Omniscio

OmniscioSubagent examplesRefactorer

A refactoring subagent

A refactor is the delegation that most obviously wants a second context window: the agent reads twenty files to move one function, and what you want back is a diff and a test result, not twenty file reads.

It is also the one that most needs a fence, because "while I was in there" is how a refactor becomes a rewrite. The file below makes the suite the contract: nothing counts as done until the tests come back the way they started.

Checked against Anthropic's Claude Code documentation on November 25, 2026. By the Omniscio team. Updated .

When Claude delegates to it

The description below names the moment — code that needs restructuring rather than fixing — and the agent is only as good as the request that reaches it. Name the move you want; a prompt that asks for cleaner code produces a diff with no name.

You can invoke it by name for a specific job: "use the refactorer subagent to extract the retry logic from the upload handler". The transcript shows the delegation and the report of steps and test runs.

It starts without your conversation, so it does not know what you just changed. A refactor on top of uncommitted work is the case where isolation: worktree earns its place: the agent gets a copy that branches from your default branch, not your half-finished tree.

The file

Save this as .claude/agents/refactorer.md in the project's .claude/agents/ folder. The test command in step 1 is the one line you must change for your repository; everything else is the discipline.

---
name: refactorer
description: Restructures code without changing what it does, in small steps, running the tests after each one. Use when code needs a shape change rather than a behaviour fix.
tools: Read, Edit, Write, Grep, Glob, Bash
model: inherit
---

You refactor. Refactoring means the behaviour stays exactly the same.

1. Establish the baseline. Run the test command the task names, or the one in package.json
   (`npm test` here, changed to yours), and record the result. If the suite is red before you start, stop and report which tests fail:
   you cannot tell your damage from the damage that was already there.
2. If the code you are about to move has no test that would notice a mistake, write one first, and
   say in your report that you did. It characterises what the code does today, not what it should.
3. Make ONE move per step, and give the step a name: extract function, rename concept, replace a
   conditional with a lookup, move a module. Do not combine steps to save a test run.
4. Run the tests after every step. Green: continue. Red: undo only that step, and either take a
   smaller step or report why the move is not safe.
5. Touch nothing outside the move. No formatting sweep, no dependency change, no "while I was in
   there". If you find a bug, report it; do not fix it in this change.

Report:
- The list of steps, each with the test result after it.
- The files changed, and the diff size.
- Anything you found and deliberately did not change.

Never claim the tests pass without having run them.

What each field does

name

The identifier, at most 256 characters, no colon. Hooks receive it as agent_type, which is how a rule can require a review after a refactor run.

description

The routing rule, and the boundary that matters here: restructuring, not fixing. Without that word, a bug report gets delegated to an agent built to leave behaviour alone, which is the wrong tool for the job. Descriptions are loaded at every session start.

tools

Read, Grep and Glob to find every use of what is moving; Edit and Write to change it; Bash to run the tests. It is the broadest tool set among the files linked from this set's hub page, which is why every rule in the body is about restraint rather than capability.

If you want a narrower agent, drop Write and keep Edit — but then step 2 cannot write the characterisation test, so the agent has to stop and ask you for one instead.

model

The aliases are sonnet, opus, haiku and fable, a full model ID, or inherit. inherit is the safe default for refactoring: the quality of the move tracks the model doing it, and a cheap model makes smaller, less coherent steps.

isolation (optional)

Set isolation: worktree to run the agent in a temporary git worktree — an isolated copy of the repository that branches from your default branch by default, not from your session's HEAD. Claude Code can remove the worktree if the agent makes no changes. This is the field that makes a large refactor safe to delegate while you keep working.

Project file or user file

Project. The test command, the module boundaries and the naming conventions are all this repository's, and a user-level copy would carry one project's test command into every other one. Where both exist under the same name, the project's wins.

  • .claude/agents/ is the project's and is usually committed, so the rules are reviewed with the code.
  • ~/.claude/agents/ is yours and follows you into every project.
  • Duplicate names inside one directory mean one file never loads; /doctor reports the pair.

Confirm Claude picked it up

  1. No restart. The agent directories are watched and an edit is picked up within a few seconds, except in a folder added with --add-dir or a session started with --disable-slash-commands.
  2. Ask by name for a small move first: one function, one rename. The report should show a baseline, the steps and the test result after each.
  3. /tasks shows the run and its model, confirming the model line (v2.1.242 or later).
  4. /agents prints the reminder to manage agents through Claude or the folders; the wizard was v2.1.197 and earlier.
  5. The transcript shows one delegation row and the step list beneath it.

When it goes wrong

  • It changes behaviour. The classic failure, and the reason step 5 exists: the agent finds a bug, fixes it, and the "refactor" is now a behaviour change nobody reviewed as one.
  • It runs one enormous step. One step that touches forty files is a rewrite with a test run at the end. The one-move-per-step rule is what keeps a failure to one small undo.
  • The baseline is red. If the suite already fails, the agent cannot tell its damage from the existing damage. Stopping and reporting that is the correct outcome, not fixing the suite first.
  • It works on your uncommitted tree. Without isolation: worktree it edits the files you are editing. For anything larger than a file, give it the worktree.
  • It never launches. When every entry in the tools list fails to resolve, Claude Code usually refuses the launch and names the entries; an empty list, or one disallowedTools empties, launches with no tools instead.

Adapting it to your repository

  1. Put your real test command in step 1, including the fast one if you have a slow suite: a per-step run has to be cheap enough to actually happen.
  2. Name your own refactors. "Extract a pure function from the effect" and "replace the boolean flags with an options object" are moves the agent can repeat; "improve structure" is not.
  3. Add the one thing your team always has to remember: the public API is frozen, the migration files never change, the generated directory is owned by a script.
  4. Keep the not-changed list. It is how a reviewer sees what the agent noticed and left alone, which is often the most useful part of the report.

The other files are on the subagent examples page, and the subagents guide covers worktrees, nesting and what a subagent inherits.

Sources

  • Anthropic's Create custom subagents, for the frontmatter reference including isolation, the tool rules, delegation and what loads at startup, read November 25, 2026.
  • Anthropic's error reference, for the zero-tools refusal and its exceptions, read November 25, 2026.
  • Anthropic's commands reference, for /agents and /tasks, read November 25, 2026.

The agent file and its step rules were written for this site. The fields and their behaviour are Anthropic's and were read from the pages above on the same day. The agent was not run against a live repository here.

Common questions

How do I stop it changing behaviour?

Make the tests the contract: a baseline before the first edit, a run after every step, and an undo for a step whose tests do not come back the way they started. A refactor with no tests to run is not one the agent can do safely.

Should it run in a git worktree?

For anything larger than a file, yes. With isolation: worktree the subagent works in a temporary copy of the repository, so a half-finished refactor never sits in your working tree.

What is the smallest useful refactoring prompt?

One named move: extract this function, rename this concept, replace this condition with a table. "Cleaner code" produces a large diff with no name, which is the hardest kind to review.

Can the refactoring agent add tests?

It can, but a test written to match new behaviour proves nothing about the old. The body asks for a characterisation test only when nothing covers the code being moved, and never treats a new test as the reason a step passed.

Refactors that leave the suite green

Omniscio is a desktop app for running Claude Code and other coding agents side by side, with one list of sessions and one inbox, so an agent only interrupts you when a decision needs you.

The features page lists what is in it today, pricing covers the plans, and you can download it for Windows, macOS or Linux.