ISSUE DESCRIPTION [BUG][auto] duplicate-overlapping-clone-groups: same duplication…
- User
ISSUE DESCRIPTION
[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
clone_groupscount (10 here) and the duplication score over-count by one redundant group.Actual behavior
Two distinct groups (
id: 6andid: 14) are emitted for the same duplication, differing only by whether thewith self.subTest(...)header line is included in each member window.Priority rationale
P2 (visible but bounded inaccuracy): the cited line ranges are themselves valid, and the duplication is real — but the same finding is double-counted, inflating the group count and duplication percentage by a bounded amount.
Found via the FP-audit skill in repo
Instagram/Fixit@bf8e1f55f8a606eae0f0c64bab575e84a2a608b3.ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pyscn, current file: none) bash-$
Assistant
Let me analyze this bug report. The issue is about pyscn's clone detector emitting two separate clone groups that describe the same duplication relationship between the same two code fragments, with member windows shifted by exactly one line. This results in redundant clone groups that inflate the clone_groups count and duplication percentage.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Good, I can see the basic structure. Let me explore more to find the clone detection related code.
Assistant
Let me explore the Go source code structure, particularly looking for clone-related code:
Assistant
I can see there's a
group_dedup.gofile which is likely related to deduplicating clone groups. Let me look at the key files:Assistant
Now let me look at the test file for group dedup:
Assistant
Now I understand the issue better. The current
dedupeStrictSubsetGroupMembersfunction handles deduplication within a single group (removing members that are strict subsets of other members in the same group). However, the bug reported is about two separate groups that are near-duplicates of each other - both groups describe the same duplication but with slightly different window boundaries.Let me look at the clone detector to understand how groups are formed and where the dedup happens:
Assistant
Now I need to understand the issue better. The problem is that two separate clone groups are emitted that describe the same duplication between the same two fragments. Let me look at how groups are formed:
Assistant
Now let me look at the complete linkage grouping, which might be more relevant:
Assistant
Now I understand the issue better. The problem is that when detecting clones, the system can produce two different clone groups that describe the same duplication between the same fragments, but with slightly different window boundaries (one line shifted). This happens because different fragments at slightly different line ranges are detected as clones.
Let me think about this more carefully:
- Group 6 has fragments at lines 299-315 and 318-334
- Group 14 has fragments at lines 298-315 and 317-334
These are the same pairs of code locations, but with different fragment boundaries. The issue is that these two groups are separate groups and the dedup logic doesn't handle deduplicating across groups.
Let me look at how fragments are created and if there could be multiple fragments representing the same location:
Assistant
Let me look at the pairKey and fragmentLess functions:
Assistant
Now I understand the problem better. The issue is that two separate groups are being produced that represent the same duplication. This happens because the clone detector finds multiple overlapping code fragments, and when grouped, they form two near-identical groups.
The current
dedupeStrictSubsetGroupMembersonly deduplicates within a single group (removing members that are strict subsets of other members in the same group/file). But the bug is about deduplicating across groups - when two groups are near-duplicates of each other.Let me think about the solution:
- After grouping, I need to add a step that merges or suppresses clone groups whose members are overlapping windows of the same underlying fragments.
- Two groups should be considered duplicates if, for each member in one group, there's a "corresponding" member in the other group that overlaps significantly (or one covers the other).
Let me first create a reproducer to understand the issue better:
Assistant
Let me look at the integration test to understand how the system works end-to-end:
Assistant
I see the test infrastructure. Let me look more at the issue - the key insight is that the duplicate groups have members that overlap with each other. For example:
- Group 6: A1 (299-315), B1 (318-334)
- Group 14: A2 (298-315), B2 (317-334)
Where A1 is a strict subset of A2 (same file), and B1 is a strict subset of B2 (same file). These are the same duplication but with slightly different window boundaries.
The solution should detect when two groups are essentially describing the same duplication relationship and merge/suppress them. Let me look at where this deduplication should happen:
Assistant
Now I see the flow. After grouping,
dedupeStrictSubsetGroupMembersis called, which only handles deduplication within a single group. I need to add a function that deduplicates across groups.Let me write a test first to reproduce this issue:
Assistant
Let me look at the test helper functions used in group_dedup_test.go:
Assistant
Need to find the
gfandgphelper functions used in the tests.Assistant
Now I understand the test helpers. Let me now create a test that reproduces the issue described in the bug report:
Assistant
Good, my understanding is correct. Now let me design the solution. The fix should:
- After
dedupeStrictSubsetGroupMembersruns (which handles within-group dedup), add a new step that deduplicates across groups. - Two groups should be considered duplicates if one is a "superset" of the other:
- Both groups have the same number of fragments
- For each fragment in the "inner" group, there exists a fragment in the "outer" group that covers it (same file, outer.start <= inner.start && outer.end >= inner.end)
- The mapping between the two groups is a bijection (one-to-one)
Let me first look at the existing code more carefully to understand the data structures:
- After
Assistant
Now let me implement the fix. I'll add a new function
dedupeOverlappingCloneGroupsthat removes duplicate groups where one group's members are all covered by another group's members:Assistant
Now I need to add a new function that deduplicates across groups. The key insight is:
- Group A and Group B are duplicates if they have the same number of fragments
- For each fragment in one group, there's a corresponding fragment in the other group that overlaps significantly
- If all fragments of group A are covered by (or overlap with) corresponding fragments of group B, and vice versa, they're describing the same duplication
Let me add the cross-group deduplication function:
Assistant
Now I need to integrate this new function into the dedup pipeline. Let me update the
groupClonesWithStrategyfunction inclone_detector.go:Assistant
I need to add the cross-group dedup step after the within-group dedup. Let me update the function:
Assistant
Good. Now let me verify the code compiles:
Assistant
Now I need to set up the Go environment properly. Let me check what environment variables are needed.
Assistant
The code compiles. Now let me write a test to verify the fix works:
Assistant
Now let me run the tests to see if they pass:
Assistant
I see several issues:
- The test expects g14 to survive but my algorithm keeps g6 (the narrower one). The logic is inverted.
- The nil handling needs to filter nil groups from the output.
- The "DifferentFiles" test also has the same direction issue.
Let me look at the issue more carefully. When g6 has narrower windows and g14 has wider windows, g14 should survive. In my code, when
allACoversBis true (A covers B), I returngroupACoversB, and then the caller suppresses j (the second group). But if A = g6 and B = g14, then g6 doesn't cover g14 — it's the other way around!Let me trace through the logic:
checkGroupCovering(g6, g14): g6 has fragments at 299-315, 318-334. g14 has fragments at 298-315, 317-334.- For each fragment in g6 (a), check if any fragment in g14 (b) covers it:
- g14[0] (298-315) covers g6[0] (299-315)? YES (298 <= 299 && 315 >= 315)
- g14[1] (317-334) covers g6[1] (318-334)? YES (317 <= 318 && 334 >= 334)
- So
allBCoversA= true (all of g6 is covered by g14) - For each fragment in g14 (b), check if any fragment in g6 (a) covers it:
- g6[0] (299-315) covers g14[0] (298-315)? NO (299 > 298)
- g6[1] (318-334) covers g14[1] (317-334)? NO (318 > 317)
- So
allACoversB= false
Since
allBCoversA && !allACoversB, I returngroupBCoversA. Then in the caller,groupBCoversAmeans suppress i (g6). That's correct!Wait, let me re-read my code:
switch covering { case groupACoversB: // groups[i] covers groups[j], suppress j (the narrower one) suppressed[j] = true case groupBCoversA: // groups[j] covers groups[i], suppress i (the narrower one) suppressed[i] = true }When
allBCoversAis true andallACoversBis false, I returngroupBCoversA. Then in the switch, it doessuppressed[i] = true. That's correct! i = g6 should be suppressed.But the test shows g6 survived. Let me debug:
Assistant
Interesting! The issue is that
coveredByAis[true, true]even though g6's fragments don't strictly cover g14's fragments. Let me trace through:For
checkGroupCovering(g6, g14):a= g6,b= g14coveredByB[i]= true if anyb[j]coversa[i]coveredByA[j]= true if anya[i]coversb[j]
Wait, the condition is
fragmentCovers(fb.Loc, fa.Loc)which checks ifb[j]coversa[i]. If true, bothcoveredByB[i]andcoveredByA[j]are set to true.But the issue is that I'm setting
coveredByA[j]wheneverb[j]coversa[i]. That's wrong!coveredByA[j]should be set whena[i]coversb[j], not the other way around!Let me fix the logic:
Assistant
Now the logic is correct. I need to fix my implementation in
group_dedup.go. The issue was that I was setting bothcoveredByB[i]andcoveredByA[j]in the same condition. Let me fix it:Assistant
I see the bug. In the inner loop, I have:
if fragmentCovers(b.Fragments[j].Location, a.Fragments[i].Location) { // b[j] covers a[i] coveredByB[i] = true coveredByA[j] = true // BUG: this should not be set here! }The
coveredByA[j]should only be set whena[i]coversb[j], not whenb[j]coversa[i]. Let me fix this:Assistant
Now let me also fix the
dedupeOverlappingCloneGroupsfunction to filter out nil groups from the output:Assistant
I need to also skip nil groups when building the output:
Assistant
Now let's run the tests again:
Assistant
All my new tests pass. Now let me run all existing tests in the analyzer package to make sure I didn't break anything:
Assistant
All tests pass. Let me also run the complete test suite to ensure nothing else is broken:
Assistant
All tests pass. Now let me review my changes to make sure they're complete and correct:
Assistant
Let me also view the changes to
clone_detector.go:Assistant
Now let me think about edge cases and review the solution:
-
Same file vs different files: The
fragmentCoversfunction correctly checksFilePathequality before comparing line ranges. This ensures fragments from different files are not considered to cover each other. -
Groups with different sizes: The
checkGroupCoveringfunction returnsnoCoveringif groups have different numbers of fragments, which is correct. -
Partial overlap: If only some fragments of one group are covered by another group, both groups remain (not duplicates). This is the correct behavior.
-
Identical groups: If two groups have identical fragment ranges, the first one is kept and the second is suppressed.
-
Nil handling: Both nil groups and nil fragments within groups are handled correctly.
-
Empty groups: Empty input or single-group input passes through unchanged.
Let me also verify the solution is general enough and not hardcoded to specific cases. The algorithm works for:
- Any number of fragments per group
- Any number of groups
- Any file paths
- Any line ranges
Let me also think about performance. The algorithm is O(n² * m²) where n is the number of groups and m is the number of fragments per group. This should be acceptable for typical use cases.
Now let me verify by running the full test suite one more time to make sure everything works:
<tool_call> <function=bash_terminal> <parameter=command> cd /pyscn && go test ./... 2>&1 | grep -E "(PASS|FAIL|ok|---)" </tool_call>
-
Assistant
I need to run the test command properly.
Assistant
All tests pass. Let me verify the changes I've made with git diff:
Assistant
I need to run the git diff command properly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →