Refactor
Run Command Center's refactoring agent over a diff. Command Center plans the refactoring, spawns its own coding-agent sessions to apply it, and leaves the result as uncommitted edits in the working tree.
When to invoke
The user asks to refactor recent changes using Command Center — the current branch, a commit range, or uncommitted work. Examples:
- "Refactor this branch with Command Center."
- "Run the Command Center refactoring agent on my changes."
- "Deduplicate the code I just wrote using CC."
Before invoking — important
- The refactoring edits the working tree. Do not edit files in the repo while the runner is waiting, and re-read any files you have cached once it finishes (the result lists exactly which files are dirty).
- Prefer a committed baseline. If there is meaningful uncommitted work, suggest committing it first so the refactoring is cleanly reviewable (and revertible) as
git diff. Not a hard requirement —--working-treeexists for refactoring uncommitted changes deliberately. - It takes a while — don't busy-poll. Typically minutes, up to tens of minutes for large diffs, and progress only updates between workflow steps, so quiet stretches of 10+ minutes are normal. The runner emits a heartbeat status line about once a minute during those stretches as proof of life. Run it in the background if your harness supports that, check on it at multi-minute intervals, and don't narrate every empty check to the user — relay progress lines when they appear.
How to invoke
Run run.mjs — it sits in the same directory as this SKILL.md. The runner does all the work: installation detection, backend startup, auth/model/agent checks, file screening, kicking off the refactoring, and streaming progress until it finishes.
node <SKILL_DIR>/run.mjs [from..to] [--workflow=do-it-all|de-duplicate] [--files=PATTERN[,PATTERN...]]
<SKILL_DIR> is wherever the skills CLI installed this skill. Common locations:
| Surface | Path |
|---|---|
| Claude Code, project-local | ./.claude/skills/refactor |
Claude Code, global (-g) |
~/.claude/skills/refactor |
| Any other agent | ./.agents/skills/refactor |
If you loaded this SKILL.md from disk, the runner is its sibling — substitute that directory.
All arguments are optional. By default the runner refactors every changed file in the resolved range.
Ref range — [from..to]:
- Omit → defaults to
merge-base(HEAD, <base>)..HEAD, where<base>is the symbolic ref oforigin/HEAD, falling back toorigin/main/origin/master/main/master. - Pass anything
gitunderstands: SHAs, branches,HEAD~3..HEAD, etc. - Special tokens:
WORKING_TREE(uncommitted changes) andSTAGED(index). Example:HEAD..WORKING_TREE.
Workflow — --workflow=:
do-it-all(default) — the full refactoring pass Command Center's own Refactor button runs.de-duplicate— focused pass that only merges duplicated code.
Convenience flags:
--working-tree→ refactor the diff from the merge base to your current working tree (includes uncommitted edits).--staged→ refactor just what'sgit add-ed.--files=PATTERN[,PATTERN...]→ narrow to a subset of the changed files. Comma-separated repo-relative globs;*matches non-slash chars,**crosses directories, a!prefix excludes. If you pass only exclusions, the implicit include is**.--timeout-mins=<n>→ how long to wait for the refactoring to finish (default 60). On timeout the refactoring keeps running in Command Center.--port=<port>→ talk to a specific Command Center backend port. Useful when multiple CC instances run (developer environments) or when the runner can't auto-discover the right one.
Note: Command Center additionally screens the file list down to reasonably-sized code files — lockfiles, generated files, and very large files are skipped automatically.
Examples — pick whichever matches the user's intent:
| User intent | Command |
|---|---|
| Whole branch vs base (default) | (no args) |
| Last 3 commits | HEAD~3..HEAD |
| Uncommitted edits | --working-tree |
| Just deduplicate the branch | --workflow=de-duplicate |
| Whole branch, but skip tests | --files=!**/*.test.*,!**/*.spec.*,!**/__tests__/** |
Only TS files in src/ |
--files=src/**/*.ts |
Interpreting the output
The runner prints one JSON object per line on stdout. Each line has a kind field:
kind: "status"— progress update (includingpercentageDonewhile refactoring); surface a brief one-line note to the user. Lines withheartbeat: trueare periodic proof-of-life during long quiet stretches — no per-line narration needed; silence between heartbeats does not indicate a hang.kind: "result"— terminal success. The refactored code is now uncommitted in the working tree. The payload includesdirtyFiles(re-read these before further edits),filesSubmitted, and the Command CentersessionIdwhose session contains per-change explanations. Tell the user it finished and suggest reviewing withgit diff.kind: "error"— terminal failure; tell the user what went wrong using thecodeandmessagefields. Codes are stable enums (not-installed,not-running,not-logged-in,no-model,no-agent,quota,no-workspace,no-files-matched,no-eligible-files,backend-too-old,refactoring-failed,refactoring-cancelled, etc.); themessageis already in the user's language.kind: "action-required"— the user must do something before re-running (e.g. install the app, sign in, configure a model or coding agent). Surface themessageand theurlif present.
Always surface the runner's message verbatim — do not rephrase. The runner produces user-facing strings in English; backend-originated strings are already localized.
The refactoring is AI-generated, so it should be reviewed before being relied on — in Command Center, where each change comes with its explanation. If the user dislikes the result, git checkout -- <files> (or git stash) reverts it, since nothing was committed.
Cancellation
Killing the runner (SIGINT/SIGTERM) sends a best-effort cancel to Command Center so the refactoring doesn't keep editing files after you've moved on. The refactoring can also be cancelled from the Command Center app.
Non-zero exit codes
The runner exits 0 on success and a small fixed integer on failure (one code per failure kind). Do not parse the exit code — branch on the structured kind / code fields above.