Finding duplicate functions

From bobmatnyc/claude-mpm · View interactive version

Finding duplicate functions is an AI agent skill from the GitHub repository bobmatnyc/claude-mpm. Use when auditing a codebase for semantic duplication — functions that do the same thing but have different names or implementations, especially common in LLM-generated codebases

How to use this skill

Skill file

Finding Duplicate-Intent Functions

Overview

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."

This skill uses a two-phase approach: classical extraction followed by LLM-powered intent clustering.

When to Use

Quick Reference

| Phase | Tool | Model | Output |

|-------|------|-------|--------|

| 1. Extract | Extract function catalog | - | catalog.json |

| 2. Categorize | Categorize by domain | haiku model | categorized.json |

| 3. Split | Split by category | - | categories/*.json |

| 4. Detect | Find duplicates per category | opus model | duplicates/*.json |

| 5. Report | Generate prioritized report | - | report.md |

Process

Phase 1: Extract Function Catalog

Extract all functions from source files. Focus on:

For each function capture:

Phase 2: Categorize by Domain

Dispatch a haiku subagent to categorize functions by domain.

Insert the function catalog into the prompt. Save output as categorized.json with functions grouped into domains like:

Phase 3: Split into Categories

Create one JSON file per category. Only categories with 3+ functions are worth analyzing for duplicates.

Phase 4: Find Duplicates (Per Category)

For each category file, dispatch an opus subagent to find semantic duplicates.

Opus is required here (not haiku) — subtle semantic matches require stronger reasoning. The subagent should identify:

Save each output as ./duplicates/{category}.json.

Phase 5: Generate Report

Produce a prioritized markdown report grouped by confidence level.

For each duplicate group, report:

Phase 6: Human Review

Review the report. For HIGH confidence duplicates:

  1. Verify the recommended survivor has tests covering all use cases
  2. Update callers to use the survivor
  3. Delete the duplicates
  4. Run tests to confirm no regressions

High-Risk Duplicate Zones

Focus extraction on these areas first — they accumulate duplicates fastest:

| Zone | Common Duplicates |

|------|-------------------|

| utils/, helpers/, lib/ | General utilities reimplemented |

| Validation code | Same checks written multiple ways |

| Error formatting | Error-to-string conversions |

| Path manipulation | Joining, resolving, normalizing paths |

| String formatting | Case conversion, truncation, escaping |

| Date formatting | Same formats implemented repeatedly |

| API response shaping | Similar transformations for different endpoints |

Common Mistakes

Extracting too much: Focus on exported functions and public methods. Internal helpers are less likely to be duplicated across files.

Skipping the categorization step: Going straight to duplicate detection on the full catalog produces noise. Categories focus the comparison.

Using haiku for duplicate detection: Haiku is cost-effective for categorization but misses subtle semantic duplicates. Use Opus for the actual duplicate analysis.

Consolidating without tests: Before deleting duplicates, ensure the survivor has tests covering all use cases of the deleted functions.

Red Flags

Never:

Always:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.