/kanso:kanso-refactor [scope: diff|path|audit-report|current-file]from kanso
Performs behaviour-preserving code cleanup: removes dilution, tightens code, deletes cruft. Use when asked to clean up, simplify, de-bloat, or refactor code. Never alters behaviour — surfaces questions if a change would.
Ships in kanso. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Behaviour-preserving cleanup. Removes dilution, tightens code, deletes cruft. Never changes behaviour. If a change would alter behaviour, the skill surfaces it as a question instead of making the change.
The principles from kanso-principles apply. The anti-dilution taxonomy is the primary target.
This skill runs in the calling chat — including when invoked as the follow-up to /kanso-audit. Never dispatch via the Agent or Task tool, never run as a subagent, never split work into a parallel runner whose output lands in a side window. Edits, summaries, and any clarifying questions all happen in the user's current transcript.
Refactoring must not alter behaviour. Behaviour changes must avoid refactoring.
Mixing the two obscures intent, breaks cherry-picking, and ruins git bisect. If a task requires both, do them in separate commits, each with a single purpose.
Before making any change, ask: would this change produce different outputs for any input the existing tests or usage patterns cover? If yes, it's not a refactor. Stop and ask the user.
Cases where the boundary is tested:
itertools.chain → refactor only if the iterator semantics are identical. Double-check laziness.When in doubt, treat it as a behaviour change and ask.
$ARGUMENTS is one of:
diff → the working tree changes (default)src/billing/invoice.ts → that file or directoryaudit-report → the user will paste or reference a prior /kanso-audit report; act on its Tier 1 and Tier 2 refactor-category findingscurrent-file → whatever file is in focusIf no scope is given and there's no obvious focus, ask.
Get the lay of the land in one turn before reading anything:
!git status --short!git diff --stat!git log -n 10 --onelineThen read surrounding code. Refactor decisions must fit the repo's existing style, not impose a new one. Check:
get_* vs fetch_*, camelCase vs snake_case)If the repo has a test suite, note how to run it — the verify step below will use it.
Attack in this order. Stop when the signal-to-cost ratio drops.
// Step 1:, // Now do X, // Add retry as requested. Always artefacts.const result = x(); return result; → return x();. Keep the variable only if it adds a meaningful name or is referenced more than once.db.query(). Should those propagate, be logged, or be wrapped?"userDataProcessingResult → something that says what it is. Only if the scope is local; exported names are API changes.flatten → itertools.chain. Hand-rolled groupBy → lodash.groupBy if already a dependency. Only if the replacement is genuinely equivalent.get/fetch/load patterns. But only within the touched scope — don't drift into unrelated files.A refactor is only behaviour-preserving if you can show it. Run the project's verification command after edits land and paste the exit code.
In Claude Code, kanso's PostToolUse hook already lint-checks each edited file as it lands. In a harness without edit hooks, that safety net did not run — the verification here is the only check, so never skip it there.
In priority order:
AGENTS.md / CLAUDE.md — explicit commands. Use these first.package.json scripts — test, typecheck, lint, check.pyproject.toml, tox.ini, Makefile — pytest, make test, make check, ruff, mypy.go.mod — go test ./..., go vet ./....Cargo.toml — cargo test, cargo check, cargo clippy.Pick the narrowest command that covers the touched files. Typecheck plus the relevant test file is usually enough; a full suite is fine when the refactor is broad.
The command may prompt for permission on first run — that's expected. Don't reroute around it.
On pass:
✓ Verified — <command>
exit 0
<last meaningful lines>
On fail:
✗ Verification failed — <command>
exit <n>
<failing output>
Failure means the refactor altered behaviour. Roll back the edit and surface the surprise to the user — don't iterate silently to make the failure go away.
⚠ No verification command found.
Checked: AGENTS.md, package.json, pyproject.toml, Makefile.
The refactor is applied but unverified — review the diff manually before committing.
Never silently skip. Either verify, or say you didn't and why.
After making edits, produce a summary like:
## Refactor summary
**Scope:** <what was refactored>
**Files touched:** <list>
### Changes made
- Deleted 14 tautological comments across `src/billing/*`
- Inlined 6 filler variables in `invoice.ts`
- Collapsed `UserProcessorFactory` into `processUser` (only one implementation existed)
- Removed dead import: `./legacy/unused-helper`
### Deferred — needs your decision
- `src/auth/session.ts:42` wraps `db.query()` in a try/catch that returns null on failure. Removing this would change behaviour. Should the error propagate, be logged, or be wrapped in a typed error?
- `src/api/user.ts:89` contains a `fetch_user` function while the rest of the module uses `get_*`. Renaming would be an API change if this is exported. Is it safe to rename?
### Verification
✓ Verified — `<command>`
exit 0
<last meaningful lines of output>
### Next steps
- Review the diff before committing
- Use `/kanso-commit` to stage and message the changes
git commit directly. That's /kanso-commit's job. This skill produces a clean working tree, nothing more.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
npx claudepluginhub blakecyze/skills --plugin kanso2 plugins ship this exact skill · first indexed Jul 9, 2026
12 skills in this plugin
/kanso-auditUse when the user asks for a code review, audit, pre-PR check, quality sweep, or pattern analysis of a diff, branch, module, or codebase. Also use when the user asks to "check" or "look over" their code for issues./kanso-commitUse when the user asks to commit changes, stage their work, write commit messages, or split the working tree into logical commits./kanso-contextUse when the user asks to update, prune, audit, or create AGENTS.md, CLAUDE.md, or other agent context files. Also use when the user says their AI agents keep making the same mistakes and they want to fix it at the context level.