Untitled conversation
- User
[BUG][auto] duplicate-overlapping-clone-groups: same duplication emitted as two separate near-identical clone groups
Bug
pyscn's clone detector emits two separate clone groups that describe the same duplication relationship between the same two code fragments, with member windows shifted by exactly one line (the enclosing
with/header line). The result is a redundant clone group that inflatesclone_groupscount and the duplication percentage.This is related to but distinct from the (closed) #416
clone-overlapping-window-self-clone: #416 was about strict-subset members within a single group; here the redundancy is across two whole groups that are near-duplicates of each other.Reproducer
Repo:
Instagram/Fixit@bf8e1f55f8a606eae0f0c64bab575e84a2a608b3pyscn:v1.22.9curl -fsSL "https://codeload.github.com/Instagram/Fixit/tar.gz/HEAD" | tar -xz pyscn analyze --json --no-open Fixit-*/In the resulting
analyze.json,clone.clone_groupscontains two groups that cover the same pair of fragments insrc/fixit/tests/smoke.py:{ "id": 6, "type": 2, "similarity": 0.9692, "clones": [ {"location": {"file_path": ".../smoke.py", "start_line": 299, "end_line": 315}}, {"location": {"file_path": ".../smoke.py", "start_line": 318, "end_line": 334}} ] }, { "id": 14, "type": 2, "similarity": 0.9613, "clones": [ {"location": {"file_path": ".../smoke.py", "start_line": 298, "end_line": 315}}, {"location": {"file_path": ".../smoke.py", "start_line": 317, "end_line": 334}} ] }Both groups describe the duplication between the
subTest("lint")block and thesubTest("fix")block. Group 14's members each start exactly one line earlier than group 6's — that line is thewith self.subTest("lint"):/with self.subTest("fix"):header:with self.subTest("lint"): # line 298 (grp14 member start) with TemporaryDirectory() as td: # line 299 (grp6 member start) ... with self.subTest("fix"): # line 317 (grp14 member start) with TemporaryDirectory() as td: # line 318 (grp6 member start)Both groups end on the same lines (315 / 334). The two groups are the same logical clone reported twice at slightly different window boundaries.
Expected behavior
The clone-grouping/dedup stage should merge (or suppress) clone groups whose members are overlapping windows of the same underlying fragments, so each duplication relationship is reported once. As-is, the count (10 here) and the duplication score over-count by one redundant group.
- Assistant
Now let me look at how groups/pairs feed into the final output and duplication metrics.
- Assistant
Now I'll implement the cross-group dedup pass in
group_dedup.go: - Assistant
Now wire it into
groupClonesWithStrategy: - Assistant
Now add tests. I'll append to
group_dedup_test.go: - Assistant
All new and existing dedup tests pass. Now run the full analyzer package plus related packages:
- Assistant
Both pairs and groups are 0. Let me check what this looked like before my change:
- Assistant
Now remove the temporary debug helper and its call sites:
- Assistant
Now update the tests for the revised policy (keep the highest-similarity group; only near-identical windows dedupe). First check
almostEqual's epsilon: - Assistant
minInt/maxIntalready exist in the package — remove my duplicates: - Assistant
All dedup tests pass and the structural case now matches pre-change output. Remove the debug file and run the full test suite:
- Assistant
Fixed. Summary of the change:
Root cause
Block-level fragment extraction emits nested windows for nested statements — the block (298–315) and its inner body (299–315). Both windows of the "lint" block pair with the matching windows of the "fix" block, producing two clone pairs that never share a fragment. Since forbids direct pairs between the overlapping same-file windows, Union-Find has no edge to merge the two components — so two near-identical groups survive, double-counting one duplication relationship (the existing pass only collapses overlapping members a single group).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.