$npx -y skills add obra/superpowers-lab --skill finding-duplicate-functionsUse when auditing a codebase for semantic duplication - functions that do the same thing but have different names or implementations. Especially useful for LLM-generated codebases where new functions are often created rather than reusing existing ones.
| 1 | # Finding Duplicate-Intent Functions |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | LLM-generated codebases accumulate semantic duplicates: functions that serve the same purpose but were implemented independently. Classical copy-paste detectors (jscpd) find syntactic duplicates but miss "same intent, different implementation." |
| 6 | |
| 7 | This skill uses a two-phase approach: classical extraction followed by LLM-powered intent clustering. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Codebase has grown organically with multiple contributors (human or LLM) |
| 12 | - You suspect utility functions have been reimplemented multiple times |
| 13 | - Before major refactoring to identify consolidation opportunities |
| 14 | - After jscpd has been run and syntactic duplicates are already handled |
| 15 | |
| 16 | ## Quick Reference |
| 17 | |
| 18 | | Phase | Tool | Model | Output | |
| 19 | |-------|------|-------|--------| |
| 20 | | 1. Extract | `scripts/extract-functions.sh` | - | `catalog.json` | |
| 21 | | 2. Categorize | `scripts/categorize-prompt.md` | haiku | `categorized.json` | |
| 22 | | 3. Split | `scripts/prepare-category-analysis.sh` | - | `categories/*.json` | |
| 23 | | 4. Detect | `scripts/find-duplicates-prompt.md` | opus | `duplicates/*.json` | |
| 24 | | 5. Report | `scripts/generate-report.sh` | - | `report.md` | |
| 25 | |
| 26 | ## Process |
| 27 | |
| 28 | ```dot |
| 29 | digraph duplicate_detection { |
| 30 | rankdir=TB; |
| 31 | node [shape=box]; |
| 32 | |
| 33 | extract [label="1. Extract function catalog\n./scripts/extract-functions.sh"]; |
| 34 | categorize [label="2. Categorize by domain\n(haiku subagent)"]; |
| 35 | split [label="3. Split into categories\n./scripts/prepare-category-analysis.sh"]; |
| 36 | detect [label="4. Find duplicates per category\n(opus subagent per category)"]; |
| 37 | report [label="5. Generate report\n./scripts/generate-report.sh"]; |
| 38 | review [label="6. Human review & consolidate"]; |
| 39 | |
| 40 | extract -> categorize -> split -> detect -> report -> review; |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | ### Phase 1: Extract Function Catalog |
| 45 | |
| 46 | ```bash |
| 47 | ./scripts/extract-functions.sh src/ -o catalog.json |
| 48 | ``` |
| 49 | |
| 50 | Options: |
| 51 | - `-o FILE`: Output file (default: stdout) |
| 52 | - `-c N`: Lines of context to capture (default: 15) |
| 53 | - `-t GLOB`: File types (default: `*.ts,*.tsx,*.js,*.jsx`) |
| 54 | - `--include-tests`: Include test files (excluded by default) |
| 55 | |
| 56 | Test files (`*.test.*`, `*.spec.*`, `__tests__/**`) are excluded by default since test utilities are less likely to be consolidation candidates. |
| 57 | |
| 58 | ### Phase 2: Categorize by Domain |
| 59 | |
| 60 | Dispatch a **haiku** subagent using the prompt in `scripts/categorize-prompt.md`. |
| 61 | |
| 62 | Insert the contents of `catalog.json` where indicated in the prompt template. Save output as `categorized.json`. |
| 63 | |
| 64 | ### Phase 3: Split into Categories |
| 65 | |
| 66 | ```bash |
| 67 | ./scripts/prepare-category-analysis.sh categorized.json ./categories |
| 68 | ``` |
| 69 | |
| 70 | Creates one JSON file per category. Only categories with 3+ functions are worth analyzing. |
| 71 | |
| 72 | ### Phase 4: Find Duplicates (Per Category) |
| 73 | |
| 74 | For each category file in `./categories/`, dispatch an **opus** subagent using the prompt in `scripts/find-duplicates-prompt.md`. |
| 75 | |
| 76 | Save each output as `./duplicates/{category}.json`. |
| 77 | |
| 78 | ### Phase 5: Generate Report |
| 79 | |
| 80 | ```bash |
| 81 | ./scripts/generate-report.sh ./duplicates ./duplicates-report.md |
| 82 | ``` |
| 83 | |
| 84 | Produces a prioritized markdown report grouped by confidence level. |
| 85 | |
| 86 | ### Phase 6: Human Review |
| 87 | |
| 88 | Review the report. For HIGH confidence duplicates: |
| 89 | 1. Verify the recommended survivor has tests |
| 90 | 2. Update callers to use the survivor |
| 91 | 3. Delete the duplicates |
| 92 | 4. Run tests |
| 93 | |
| 94 | ## High-Risk Duplicate Zones |
| 95 | |
| 96 | Focus extraction on these areas first - they accumulate duplicates fastest: |
| 97 | |
| 98 | | Zone | Common Duplicates | |
| 99 | |------|-------------------| |
| 100 | | `utils/`, `helpers/`, `lib/` | General utilities reimplemented | |
| 101 | | Validation code | Same checks written multiple ways | |
| 102 | | Error formatting | Error-to-string conversions | |
| 103 | | Path manipulation | Joining, resolving, normalizing paths | |
| 104 | | String formatting | Case conversion, truncation, escaping | |
| 105 | | Date formatting | Same formats implemented repeatedly | |
| 106 | | API response shaping | Similar transformations for different endpoints | |
| 107 | |
| 108 | ## Common Mistakes |
| 109 | |
| 110 | **Extracting too much**: Focus on exported functions and public methods. Internal helpers are less likely to be duplicated across files. |
| 111 | |
| 112 | **Skipping the categorization step**: Going straight to duplicate detection on the full catalog produces noise. Categories focus the comparison. |
| 113 | |
| 114 | **Using haiku for duplicate detection**: Haiku is cost-effective for categorization but misses subtle semantic duplicates. Use Opus for the actual duplicate analysis. |
| 115 | |
| 116 | **Consolidating without tests**: Before deleting duplicates, ensure the survivor has tests covering all use cases of the deleted functions. |