Embedding-based code duplication detector
Project description
Slopo
A lightweight CLI tool for detecting non-exact code duplication using embedding models.
It focuses on the similar code that is hardest to detect and most harmful: snippets written similarly, sitting far apart in the codebase, often spread across different modules or separated within a large file. Exact copy-paste is easy to spot by other tools, and duplicates that are close together are easy to spot by humans or AI.
For more high-level description of the problem see slopo.dev.
Supported languages
Python, TypeScript, JavaScript, Java, Kotlin, C#, Go, Rust
How it works
It takes a different approach than typical duplication detection. For every code unit, it calculates an embedding, then looks for pairs whose embeddings are close. Similar code is not necessarily a duplicate, so each pair is a potential duplicate to confirm. Code doing the same thing but implemented in a completely different way produces distant embeddings and won't be detected.
The result is clusters of similar code units, ranked by similarity and by distance in the codebase. These clusters are meant as input for your AI coding agent, which can check whether a cluster is a real duplicate. Reviewed clusters can be marked as ignored or passed on for refactoring.
Quick start
Installation
uv tool install slopo
This command uses uv (installing uv), a Python package manager, to install Slopo from PyPI in an isolated virtual environment. No need to get Python separately.
Setup
Run slopo init to create a config file template containing further instructions. Only the directory with code for analysis and embedding model configuration is required.
Embedding model
Embeddings are calculated using an external provider. For best results, consider models dedicated to code, e.g. Voyage AI (it works fine with low dimensions like 512).
You can use any model provider compatible with LiteLLM, see details here.
The provider API key can be set as an environment variable for better security.
Analysis
Run slopo show-config to validate your config and show all configurable parameters, most are optional with sensible defaults.
Now you are ready to index code, calculate embeddings and generate a report:
slopo index
slopo embed
slopo analyze
Example workflow
This section demonstrates how Slopo can be used in a real development workflow.
It utilizes incremental re-indexing (update index with changed files only) and slopo.ignore.txt to discard already reviewed clusters.
- Create your first analysis and check results. You will notice
index.mdcontaining a list of all clusters and cluster details per file. - You may want to exclude some directories or file patterns, usually excluding tests is a good idea. You can also tune thresholds if the result is too big or too small.
- Once satisfied with analysis results, ask your AI coding agent to filter out clusters that are not real duplicates. This is a common case because not every similar code is a duplication to act on. Ask the AI agent to add discarded cluster hashes to
slopo.ignore.txt. - Re-run the analysis to generate a report without reviewed clusters. This is a basis for refactoring, which can be done by an AI agent.
ignorefile can be committed to your Git repository and reused cross-team. New and modified clusters will reappear in the report. A configuration file without an API key can also be committed. Don't commitslopo.db, this is your local data.
Configuration
Run slopo --help and slopo show-config to explore it by yourself anytime.
Most configuration is done with a configuration file with two exceptions:
- The location of the configuration file can be overridden with the
--configoption. - The API key can be set with the
SLOPO_EMBEDDING_API_KEYenvironment variable, also picked up from a.envfile in the current directory.
Be aware that some parameters can't be changed after first indexing. You need to remove slopo.db and index/embed from the beginning: source_dir, embedding_model, embedding_dimensions, body_node_count_threshold.
All configurable parameters
source_dir: Source directory with code to index, absolute or relative path.source_dir_exclude: .gitignore-style patterns to exclude from indexing.db_file: SQLite database file with tool data.report_dir: Output directory for analysis report.ignore_file: Text file with ignored clusters.embedding_model: Embedding model name in LiteLLM format.embedding_dimensions: Embedding dimensions compatible with the used model.embedding_api_key: API key for embedding provider. Optional if configured with an environment variable.embedding_batch_sizeandembedding_batch_chars: Requests to the embedding API are batched for performance. Defaults are fine for most cases.similarity_threshold: Controls minimal cosine similarity between embeddings.rerank_threshold: Controls minimal similarity after applying a boost reflecting distance in the codebase.body_node_count_threshold: Number of AST nodes inside the body (excluding signature and annotations). This value reflects the minimum code complexity of the included code unit, more precise than text length. Increase if you notice unwanted, too-small code units in the report.
Details
Ranking thresholds
Similar code units are filtered in two passes, each with its own configurable threshold. The pipeline is as follows:
similarity_thresholdfilters out code unit pairs whose embeddings are not similar enough. The calculated value is cosine similarity ranging from-1to1where1means the same.- Similar pairs are grouped in clusters.
- Units in clusters are reranked after applying a boost. Boost is calculated based on the number of directory hops required to reach the other file in the pair (max. 15%). If they are in the same file, the boost is calculated based on distance in number of lines (max. 10%).
rerank_thresholdfilters out clusters whose highest-scoring pair is not high enough.
Exact-copy duplicates
The main goal of this tool is to detect non-exact code duplication, but exact copies (identical code at multiple paths) are reported too, just handled a little differently from merely similar code:
- The report shows the code once, listing every path where it appears, instead of repeating identical snippets.
- The
analyzecommand reports the "similarity ratio" (the share of code units flagged as similar) in two variants: including and excluding exact copies.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slopo-0.2.0.tar.gz.
File metadata
- Download URL: slopo-0.2.0.tar.gz
- Upload date:
- Size: 35.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2914739bfd12615036356bf064559420cb58f09dc739a5d0d43c5265c8dac8d
|
|
| MD5 |
77cd7fc5833114fd5cb02c83ad14de25
|
|
| BLAKE2b-256 |
82ae1fea276979ee64fec9a1b745ce4fcd4b2120863811d440d4cd3071ddabc6
|
Provenance
The following attestation bundles were made for slopo-0.2.0.tar.gz:
Publisher:
publish.yml on rafal-qa/slopo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
slopo-0.2.0.tar.gz -
Subject digest:
d2914739bfd12615036356bf064559420cb58f09dc739a5d0d43c5265c8dac8d - Sigstore transparency entry: 1873941688
- Sigstore integration time:
-
Permalink:
rafal-qa/slopo@edac3b24f71c04fac2267564a41a9b11e621a9a7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/rafal-qa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edac3b24f71c04fac2267564a41a9b11e621a9a7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file slopo-0.2.0-py3-none-any.whl.
File metadata
- Download URL: slopo-0.2.0-py3-none-any.whl
- Upload date:
- Size: 50.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5bc3f216ef185b2a6538b9e5e402a1e02f912170a69fec16b19af819b65ba85
|
|
| MD5 |
c1f117c9b0938368672a5981b8ccfeac
|
|
| BLAKE2b-256 |
f6a84f0780689c100d1673c30cece05f6c1004999683f4a25aaa9210379a6a81
|
Provenance
The following attestation bundles were made for slopo-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on rafal-qa/slopo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
slopo-0.2.0-py3-none-any.whl -
Subject digest:
c5bc3f216ef185b2a6538b9e5e402a1e02f912170a69fec16b19af819b65ba85 - Sigstore transparency entry: 1873942109
- Sigstore integration time:
-
Permalink:
rafal-qa/slopo@edac3b24f71c04fac2267564a41a9b11e621a9a7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/rafal-qa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@edac3b24f71c04fac2267564a41a9b11e621a9a7 -
Trigger Event:
release
-
Statement type: