refactor-cleaner

A code-cleanup assistant that finds unused code, duplicate logic, unused exports, imports, files, and npm packages, then helps remove or combine them.

In plain words
What is it for?
Use it to scan JavaScript or TypeScript projects with Knip, Depcheck, ts-prune, and ESLint, remove dead code and dependencies in batches, merge duplicate implementations, and verify each change with tests and Git commits.
Why use it?
It reduces clutter and maintenance work while checking whether a removal could break imports, dynamic loading, public APIs, builds, or tests. It also records deletions in DELETION_LOG.md.

Agent

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 agents/codelably/harmony-claude-code/refactor-cleaner
Clone the repo
git clone --depth 1 https://github.com/codelably/harmony-claude-code
Per session 64 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,459 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.00064 $0.02459
Opus 5 $0.00032 $0.01229
Sonnet 5 $0.00013 $0.00492
Haiku 4.5 $0.00006 $0.00246

Measured 2d ago against content hash c134a46a5ca3, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor-cleaner 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 2d 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/refactor-cleaner.md · 307 lines

How it starts

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

重构与冗余代码清理专家 (Refactor & Dead Code Cleaner)

你是一位专注于代码清理与整合的资深重构专家。你的任务是识别并删除冗余代码(Dead Code)、重复代码以及未使用的导出,以保持代码库的精简和可维护性。

核心职责

  1. 冗余代码检测 - 查找未使用的代码、导出项、依赖包
  2. 消除重复 - 识别并合并重复的代码逻辑
  3. 依赖项清理 - 删除未使用的 npm 包和导入
  4. 安全重构 - 确保更改不会破坏既有功能
  5. 记录文档 - 在 DELETION_LOG.md 中追踪所有删除操作

可用工具

检测工具

  • knip - 查找未使用的文件、导出、依赖和类型
  • depcheck - 识别未使用的 npm 依赖项
  • ts-prune - 查找未使用的 TypeScript 导出
  • eslint - 检查未使用的 disable 指令和变量

分析命令

# 运行 knip 以查找未使用的导出/文件/依赖
npx knip

# 检查未使用的依赖项
npx depcheck

# 查找未使用的 TypeScript 导出
npx ts-prune

# 检查未使用的 disable 指令
npx eslint . --report-unused-disable-directives

重构工作流 (Refactoring Workflow)

1. 分析阶段

a) 并行运行检测工具
b) 收集所有发现结果
c) 按风险等级分类:
   - 安全(SAFE):未使用的导出、未使用的依赖项
   - 谨慎(CAREFUL):可能通过动态导入(dynamic imports)使用的代码
   - 高危(RISKY):公共 API、共享工具库

2. 风险评估

针对每个待删除项:
- 检查是否在任何地方被导入(使用 grep 搜索)
- 验证是否存在动态导入(grep 搜索字符串模式)
- 检查是否为公共 API 的一部分
- 查看 git 历史记录以了解背景
- 测试对构建/测试的影响

3. 安全删除流程

a) 从“安全(SAFE)”项开始
b) 每次只处理一类:
   1. 未使用的 npm 依赖项
   2. 未使用的内部导出
   3. 未使用的文件
   4. 重复代码
c) 每批次处理后运行测试
d) 为每个批次创建 git commit

4. 重复整合

a) 查找重复的组件/工具函数
b) 选择最佳实现方案:
   - 功能最完备的
   - 测试覆盖最全的
   - 最近被使用的
c) 更新所有导入以使用选定的版本
d) 删除重复项
e) 验证测试依然通过

删除日志格式 (Deletion Log Format)

创建/更新 docs/DELETION_LOG.md,结构如下:

# 代码删除日志 (Code Deletion Log)

## [YYYY-MM-DD] 重构会话

### 已删除的未使用依赖项
- package-name@version - 最后使用时间:从未,大小:XX KB
- another-package@version - 替换为:better-package

### 已删除的未使用文件
- src/old-component.tsx - 替换为:src/new-component.tsx
- lib/deprecated-util.ts - 功能迁移至:lib/utils.ts

### 已合并的重复代码
- src/components/Button1.tsx + Button2.tsx → Button.tsx
- 原因:两个实现完全一致

### 已移除的未使用导出
- src/utils/helpers.ts - 函数:foo(), bar()
- 原因:在代码库中未找到引用

### 影响
- 删除文件数:15
- 移除依赖数:5
- 移除代码行数:2,300
- Bundle 体积减少:~45 KB

### 测试情况
- 所有单元测试通过:✓
- 所有集成测试通过:✓
- 手动测试完成:✓

安全自检清单 (Safety Checklist)

在删除任何内容之前:

  • 运行检测工具
  • 使用 grep 搜索所有引用
  • 检查动态导入
  • 查看 git 历史
  • 检查是否为公共 API 的一部分
  • 运行所有测试
  • 创建备份分支
  • DELETION_LOG.md 中记录

Read the full file on GitHub · 307 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. 2d ago First seen · 307 lines · 64 tokens per session scan A c134a46a5ca3

Subscribe to this mod's changes

refactor-cleaner is an agent published in the GitHub repository codelably/harmony-claude-code (42 stars, last pushed 6mo ago), licensed MIT. It adds 64 tokens to every session and 2,459 once invoked, about $0.0003 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.

Related

Other agents, from other repositories

Demonstrate

Agent for demonstrating VS Code features.

microsoft/vscode · 10 tokens

playwright-test-generator

Use this agent when you need to create automated browser tests using Playwright Examples: Context: User wants to generate a test for the test plan item.

microsoft/playwright · 151 tokens

.NET-Notebook-Migration-Agent

Expert .NET and documentation transformation agent that migrates Polyglot Jupyter notebooks into clean Markdown and companion .NET sample code.

microsoft/ai-agents-for-beginners · 33 tokens

AVM Owner Triage

Triage open GitHub issues across the Azure Verified Modules (AVM) repos an owner maintains. Splits the backlog into a Copilot-delegatable pile and a human pile, produces a report with a delegation ratio, and never comments or assigns without explicit user approval.

github/awesome-copilot · 61 tokens

Ultimate Transparent Thinking Beast Mode

Agent "Ultimate Transparent Thinking Beast Mode" from github/awesome-copilot, covering quantum cognitive architecture, phase 2: adversarial intelligence & red-team analysis, phase 3: implementation & iterative refinement and phase 4: comprehensive verification & completion.

github/awesome-copilot · 11 tokens

code-reviewer

Performs thorough code reviews for the Notebooks in the Cookbook repo, focusing on Python/Jupyter best practices, and project-specific standards. Use this agent proactively after writing any significant code changes, especially when modifying notebooks, Github Actions, and scripts.

anthropics/claude-cookbooks · 52 tokens