# Duplicate detection
`cargo-dupes` and `jscpd` are maintainer checks for duplicate code. They overlap but catch different shapes.
- `cargo-dupes`: Rust AST duplicate detector. Finds exact and near duplicate Rust code units with structural similarity.
- `jscpd`: token/text duplicate detector. Scans Rust plus Markdown/JSON/TOML repo docs and config through `.jscpd.json`.
## Install
```sh
cargo install cargo-dupes --locked
npm install -g jscpd
```
## Run
Run from repo root:
```sh
cargo dupes stats
cargo dupes report
cargo dupes check
jscpd --config .jscpd.json .
```
Generated `jscpd` JSON reports go under `target/duplicate-reports/jscpd/`; do not commit generated reports.
## Interpret results
`cargo dupes stats` reports exact groups, near groups, duplicate units, duplicate lines, and duplicate percentages. Exact groups are copy-equivalent AST units. Near groups are structurally similar Rust units over configured `similarity_threshold`.
`cargo dupes report` lists duplicate groups and source spans. Use it to identify refactor targets before changing thresholds or ignore lists.
`cargo dupes check` enforces repo thresholds from `dupes.toml`:
- `max_exact_duplicates`
- `max_near_duplicates`
- `max_exact_percent`
- `max_near_percent`
`jscpd --config .jscpd.json .` prints clone spans and writes JSON summary. Its total percentage is duplicated lines divided by analyzed lines. Markdown code fences may be reported by their fenced language, so console rows can include formats such as `bash` even though config targets Markdown files.
## Cleanup metrics
### Round 1: P1-P5 core dedup
| cargo-dupes | Exact groups | 1,753 | 172 | -1,581 |
| cargo-dupes | Exact percent | 11.48% | 4.88% | -6.60 pp |
| cargo-dupes | Near groups | 462 | 100 | -362 |
| cargo-dupes | Near percent | 3.61% | 3.71% | +0.10 pp |
| jscpd | Duplication | 10.00% | 9.08% | -0.92 pp |
| jscpd | Cloned blocks | 1,499 | 1,508 | +9 |
### Round 2: eval providers + agent test fixtures
| cargo-dupes | Exact groups | 172 | 167 | -5 |
| cargo-dupes | Exact percent | 4.88% | 4.64% | -0.24 pp |
| cargo-dupes | Near groups | 100 | 101 | +1 |
| cargo-dupes | Near percent | 3.71% | 3.75% | +0.04 pp |
| jscpd | Duplication | 9.08% | 8.71% | -0.37 pp |
| jscpd | Cloned blocks | 1,508 | 1,458 | -50 |
| jscpd | Duplicated lines | 15,553 | 14,878 | -675 |
## Policy
Fix duplicates over ignore. Add excludes or ignore fingerprints only when duplicate code is generated, vendored, or intentionally repeated for safety/readability, and document reason in review. Do not ignore actionable source areas to make thresholds pass.