
Defrag
- 10 installs
- 1 repo stars
- Updated July 31, 2026
- hexbee/hello-skills
Helps with ai & agent building tasks.
About
defrag is a Claude Code skill for ai & agent building. It helps solo builders move faster with AI-assisted development.
- defrag
- AI & Agent Building
- AI-coding skill
Defrag by the numbers
- 10 all-time installs (skills.sh)
- Ranked #11,937 of 16,546 AI & Agent Building skills by installs in the Skillselion catalog
- Data as of Aug 2, 2026 (Skillselion catalog sync)
npx skills add https://github.com/hexbee/hello-skills --skill defragAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 10 |
|---|---|
| repo stars | ★ 1 |
| Last updated | July 31, 2026 |
| Repository | hexbee/hello-skills ↗ |
What it does
Helps with ai & agent building tasks.
Files
Defrag
Purpose
Use this skill to inspect one or more files, identify the highest-value refactoring opportunities, and present a ranked top 10 list with concrete code-level suggestions.
This skill is analysis-first. Do not apply edits unless the user explicitly switches from analysis to implementation.
Default Behavior
1. Determine the target scope. 2. Read the relevant files or the user's current selection. 3. Inspect the code for the refactoring types listed below. 4. If git history is available, inspect the last 30 days to find change hotspots. Prefer scripts/git_hotspots.py for consistent counts. 5. Score each candidate refactor with the provided weighting model. 6. Return the top 10 opportunities sorted by value score. 7. End by asking whether the user wants any suggestion applied.
If the user did not specify files, use the most relevant local context you can discover from the conversation or repo state. If the scope is still ambiguous and a wrong guess would be misleading, ask for the file or directory to analyze.
Hard Rules
- Do not apply changes during the analysis pass.
- Do not suggest speculative architecture work that is not justified by the current code.
- Follow YAGNI. Prefer targeted, local refactors over framework churn.
- Keep constants close to usage. Do not suggest dumping values into a global constants file.
- Be specific about file path, line range, and the concrete benefit.
- Show short before/after snippets for every recommendation.
- Sort strictly by value score from highest to lowest.
Refactoring Types To Look For
Check for these opportunity types:
1. shorten_file: files over 300 lines that should be split into coherent units 2. shorten_function: functions over 50 lines that should be decomposed 3. reduce_nesting: nested conditionals that should become guard clauses or early returns 4. extract_function: repeated or complex logic that should move into a named helper 5. rename_for_clarity: unclear names, overloaded names, or names that hide intent 6. simplify_conditionals: brittle or verbose branching that can be simplified 7. extract_constants: magic strings, numbers, selectors, or thresholds that should become local constants 8. consolidate_duplicates: duplicate logic that should be merged 9. modernize_syntax: safe use of modern syntax such as destructuring or optional chaining 10. avoid_globality: move global items closer to where they are used 11. optimize_imports: remove unused imports and tighten import organization 12. remove_dead_code: identify unused code paths, stale helpers, or unreachable branches 13. add_tests: call out risky logic with weak or missing coverage 14. break_up_hotspots: inspect the last 30 days of git history and flag files changed more than 30 times
Hotspot Analysis
When git history is available:
1. Inspect the last 30 days of changes. 2. Find the 5 files with the highest change counts. 3. For any file changed more than 30 times, suggest decomposition or responsibility splits.
If git history is unavailable, say so briefly and continue with static code analysis.
To collect hotspot data, you can run:
python skills/defrag/scripts/git_hotspots.py --repo . --days 30 --limit 5Scoring Model
Score each refactoring from 0 to 100 using this weighted model:
- Readability improvement:
35% - Maintainability improvement:
30% - Bug risk reduction:
25% - Performance impact:
5% - Scope size:
5%
Use judgment, but keep scores internally consistent. High scores should correspond to changes that are both important and realistically actionable.
Analysis Workflow
Follow this order:
1. Confirm the target files, folder, or code selection. 2. Read enough surrounding context to avoid shallow suggestions. 3. Check file size, function length, nesting, duplication, naming, imports, and dead code. 4. If applicable, inspect tests near the target code and note important gaps. 5. If applicable, inspect recent git history for churn hotspots. 6. Produce more than 10 candidate opportunities if needed, then sort and keep the best 10.
Bundled Resources
scripts/git_hotspots.py: counts per-file churn from recent git history and returns a JSON summary for hotspot analysisevals/evals.json: starter prompts for validating trigger behavior and output structureagents/openai.yaml: UI-facing metadata and default invocation prompt
Output Format
Use this exact structure for each ranked item:
{rank}. {type} (Value: {score}/100)
- File:
{filepath}:{start_line}-{end_line} - Description: {one-line description}
- Rationale: {why this helps}
Before: ```{language} {current code snippet}
**After:**{refactored code snippet}
---
After the ranked list, end with:
`Found {N} refactoring opportunities. Top 10 shown with average score: {avg}`
Then ask:
`Would you like me to apply any of these? Say "apply #1" or "apply all".`
## Snippet Guidance
- Keep before/after snippets focused on the exact issue.
- Show enough context to make the refactor understandable.
- Prefer realistic after examples that fit the current code style.
- Do not fabricate unrelated helper layers or abstractions.
## Review Mindset
Optimize for the highest-value improvements, not the largest number of comments.
A strong result:
- catches structural issues before cosmetic ones
- points to the exact lines that matter
- proposes changes that reduce future editing cost
- avoids vague advice such as "clean this up" or "improve readability"
## Failure Modes To Avoid
Do not:
- recommend massive rewrites without evidence
- flood the list with trivial rename suggestions when there are larger structural problems
- suggest moving everything into shared utils or managers
- present style-only changes as high-value refactors
- apply code edits unless the user explicitly asks for implementation
interface:
display_name: "Defrag"
short_description: "Rank the highest-value refactoring opportunities in code"
default_prompt: "Use $defrag to analyze this codebase or file, rank the top refactoring opportunities, and show before/after examples without applying changes."
policy:
allow_implicit_invocation: true
{
"skill_name": "defrag",
"evals": [
{
"id": 1,
"prompt": "Use $defrag on this TypeScript file and identify the top 10 refactoring opportunities. Focus on long functions, duplicated logic, and missing tests.",
"expected_output": "A ranked analysis-only report with value scores, exact file line references, before/after snippets, and a final prompt asking whether to apply suggestions.",
"files": []
},
{
"id": 2,
"prompt": "Run $defrag on the selected React component and tell me the highest-value cleanup opportunities. Include hotspot analysis from the last 30 days if git history is available.",
"expected_output": "A ranked list that includes structural refactors before cosmetic ones, mentions hotspot analysis behavior, and does not modify code.",
"files": []
},
{
"id": 3,
"prompt": "Refactor review request: use $defrag on this Python module and show the most valuable 10 improvements with concise before and after code examples.",
"expected_output": "A top-10 refactoring review with scoring, rationale, before/after code blocks, and an explicit analysis-only stance.",
"files": []
}
]
}
#!/usr/bin/env python3
"""Report the most frequently touched files in recent git history."""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from collections import Counter
from pathlib import Path
def run_git_log(repo: Path, days: int) -> str:
cmd = [
"git",
"log",
f"--since={days}.days",
"--name-only",
"--pretty=format:",
"--no-merges",
]
result = subprocess.run(
cmd,
cwd=repo,
capture_output=True,
text=True,
encoding="utf-8",
)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip() or "git log failed")
return result.stdout
def collect_hotspots(log_output: str, limit: int) -> list[dict[str, int | str]]:
counts: Counter[str] = Counter()
for line in log_output.splitlines():
path = line.strip()
if not path:
continue
counts[path] += 1
return [
{"path": path, "change_count": count}
for path, count in counts.most_common(limit)
]
def main() -> int:
parser = argparse.ArgumentParser(
description="Find recently touched git files by change frequency."
)
parser.add_argument(
"--repo",
default=".",
help="Path to the git repository. Defaults to the current directory.",
)
parser.add_argument(
"--days",
type=int,
default=30,
help="How many days of history to inspect. Defaults to 30.",
)
parser.add_argument(
"--limit",
type=int,
default=5,
help="How many files to return. Defaults to 5.",
)
parser.add_argument(
"--output",
help="Optional path to save the JSON result.",
)
args = parser.parse_args()
repo = Path(args.repo).resolve()
try:
log_output = run_git_log(repo, args.days)
hotspots = collect_hotspots(log_output, args.limit)
except Exception as exc: # pragma: no cover
print(json.dumps({"error": str(exc)}), file=sys.stderr)
return 1
payload = {
"repo": str(repo),
"days": args.days,
"limit": args.limit,
"files": hotspots,
}
output_text = json.dumps(payload, indent=2)
if args.output:
Path(args.output).write_text(output_text + "\n", encoding="utf-8")
else:
print(output_text)
return 0
if __name__ == "__main__":
raise SystemExit(main())